Skip to content

Commit

Permalink
fix onItemClick, OnItemLongClick position wrong when use RecyclerView…
Browse files Browse the repository at this point in the history
… animation to remove or insert item
  • Loading branch information
ThirtyDegreesRay committed Nov 30, 2017
1 parent 0c431c6 commit cb1bd0e
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
* Created by ThirtyDegreesRay on 2017/11/22 15:59:45
Expand Down Expand Up @@ -81,13 +80,14 @@ public void loadBookmarks(int page) {
public void removeBookmark(int position) {
removedBookmark = bookmarks.remove(position);
removedPosition = position;
daoSession.getBookmarkDao().deleteByKey(removedBookmark.getId());
rxDBExecute(() -> daoSession.getBookmarkDao().deleteByKey(removedBookmark.getId()));
}

@Override
public void undoRemoveBookmark() {
bookmarks.add(removedPosition, removedBookmark);
daoSession.getBookmarkDao().insert(removedBookmark);
mView.notifyItemAdded(removedPosition);
rxDBExecute(() -> daoSession.getBookmarkDao().insert(removedBookmark));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ public void loadTraceList(int page) {
public void removeTrace(int position) {
removedTrace = traceList.remove(position);
removedPosition = position;
daoSession.getTraceDao().deleteByKey(removedTrace.getId());
rxDBExecute(() -> daoSession.getTraceDao().deleteByKey(removedTrace.getId()));
}

@Override
public void undoRemoveTrace() {
traceList.add(removedPosition, removedTrace);
daoSession.getTraceDao().insert(removedTrace);
mView.notifyItemAdded(removedPosition);
rxDBExecute(() -> daoSession.getTraceDao().insert(removedTrace));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ private boolean checkIsUnauthorized(Throwable error){
return false;
}

public void rxDBExecute(@NonNull Runnable runnable){
daoSession.rxTx().run(runnable).subscribe();
}


@NonNull
protected String getLoadTip() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.thirtydegreesray.openhub.mvp.presenter.RepositoryPresenter;
import com.thirtydegreesray.openhub.ui.activity.base.PagerActivity;
import com.thirtydegreesray.openhub.ui.adapter.BranchesAdapter;
import com.thirtydegreesray.openhub.ui.adapter.base.BaseAdapter;
import com.thirtydegreesray.openhub.ui.adapter.base.BaseViewHolder;
import com.thirtydegreesray.openhub.ui.adapter.base.FragmentPagerModel;
import com.thirtydegreesray.openhub.ui.fragment.ActivityFragment;
import com.thirtydegreesray.openhub.ui.fragment.CommitsFragment;
Expand Down Expand Up @@ -240,7 +240,7 @@ public void onClick(DialogInterface dialog, int which) {
})
.show();

branchesAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
branchesAdapter.setOnItemClickListener(new BaseViewHolder.OnItemClickListener() {
@Override
public void onItemClick(int position, @NonNull View view) {
Branch branch = list.get(position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.thirtydegreesray.openhub.R;
import com.thirtydegreesray.openhub.mvp.contract.base.IBaseContract;
import com.thirtydegreesray.openhub.ui.adapter.base.BaseAdapter;
import com.thirtydegreesray.openhub.ui.adapter.base.BaseViewHolder;

import javax.inject.Inject;

Expand All @@ -24,8 +25,8 @@

public abstract class ListActivity <P extends IBaseContract.Presenter, A extends BaseAdapter>
extends BaseActivity<P> implements IBaseContract.View,
BaseAdapter.OnItemClickListener,
BaseAdapter.OnItemLongClickListener {
BaseViewHolder.OnItemClickListener,
BaseViewHolder.OnItemLongClickListener {

@BindView(R.id.recycler_view) protected RecyclerView recyclerView;
@Inject protected A adapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

Expand All @@ -29,7 +28,7 @@
* Created by ThirtyDegreesRay on 2017/10/18 15:09:19
*/

public class CommitFilesAdapter extends BaseAdapter<RecyclerView.ViewHolder,
public class CommitFilesAdapter extends BaseAdapter<BaseViewHolder,
DoubleTypesModel<CommitFilesPathModel, CommitFile>> {

@Inject
Expand All @@ -47,7 +46,7 @@ protected int getLayoutId(int viewType) {
}

@Override
protected RecyclerView.ViewHolder getViewHolder(View itemView, int viewType) {
protected BaseViewHolder getViewHolder(View itemView, int viewType) {
if(viewType == 0){
return new PathViewHolder(itemView);
}else{
Expand All @@ -61,7 +60,7 @@ public int getItemViewType(int position) {
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
public void onBindViewHolder(@NonNull BaseViewHolder viewHolder, int position) {
super.onBindViewHolder(viewHolder, position);
if (viewHolder instanceof PathViewHolder) {
PathViewHolder holder = (PathViewHolder) viewHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Created by ThirtyDegreesRay on 2017/11/6 20:05:32
*/

public class NotificationsAdapter extends BaseAdapter<RecyclerView.ViewHolder,
public class NotificationsAdapter extends BaseAdapter<BaseViewHolder,
DoubleTypesModel<Repository, Notification>> {

@Inject
Expand All @@ -54,7 +54,7 @@ protected int getLayoutId(int viewType) {
}

@Override
protected RecyclerView.ViewHolder getViewHolder(View itemView, int viewType) {
protected BaseViewHolder getViewHolder(View itemView, int viewType) {
if(viewType == 0){
return new RepoViewHolder(itemView);
} else {
Expand All @@ -68,7 +68,7 @@ public int getItemViewType(int position) {
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
public void onBindViewHolder(@NonNull BaseViewHolder viewHolder, int position) {
super.onBindViewHolder(viewHolder, position);
if(viewHolder instanceof RepoViewHolder){
RepoViewHolder holder = (RepoViewHolder) viewHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class SettingsAdapter extends BaseAdapter<SettingsAdapter.ViewHolder, SettingModel> {

private ItemEventListener itemEventListener;
private final int TAG_POSITION = R.id.position_tag;

@Inject
public SettingsAdapter(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* Created by ThirtyDegreesRay on 2017/11/23 10:35:13
*/

public class TraceAdapter extends BaseAdapter<RecyclerView.ViewHolder, TraceExt>
public class TraceAdapter extends BaseAdapter<BaseViewHolder, TraceExt>
implements StickyRecyclerHeadersAdapter<TraceAdapter.HeadViewHolder>{

@Inject
Expand All @@ -52,7 +52,7 @@ protected int getLayoutId(int viewType) {
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
TraceExt model = data.get(position);
if(getItemViewType(position) == 0){
Expand Down Expand Up @@ -91,7 +91,7 @@ public int getItemViewType(int position) {
}

@Override
protected RecyclerView.ViewHolder getViewHolder(View itemView, int viewType) {
protected BaseViewHolder getViewHolder(View itemView, int viewType) {
if(viewType == 0){
return new UserViewHolder(itemView);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.view.ViewGroup;
import android.widget.Toast;

import com.thirtydegreesray.openhub.R;
import com.thirtydegreesray.openhub.ui.fragment.base.BaseFragment;

import java.util.ArrayList;
Expand All @@ -21,15 +20,15 @@
* 适配器基类
* Created by ThirtyDegreesRay on 2016/7/27 19:49
*/
public abstract class BaseAdapter<VH extends RecyclerView.ViewHolder, D extends Object>
public abstract class BaseAdapter<VH extends BaseViewHolder, D extends Object>
extends RecyclerView.Adapter<VH>
implements View.OnClickListener, View.OnLongClickListener{
implements BaseViewHolder.OnItemClickListener, BaseViewHolder.OnItemLongClickListener{

//item点击回调
private OnItemClickListener mOnItemClickListener;
private BaseViewHolder.OnItemClickListener mOnItemClickListener;

//item长按回调
private OnItemLongClickListener mOnItemLongClickListener;
private BaseViewHolder.OnItemLongClickListener mOnItemLongClickListener;

/**
* 数据列表
Expand All @@ -42,8 +41,6 @@ public abstract class BaseAdapter<VH extends RecyclerView.ViewHolder, D extends
protected Context context;
protected BaseFragment fragment;

protected final int TAG_POSITION = R.id.position_tag;

public BaseAdapter(Context context){
this.context = context;
}
Expand All @@ -69,15 +66,15 @@ public ArrayList<D> getData() {
* 设置item点击事件
* @param onItemClickListener
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
public void setOnItemClickListener(BaseViewHolder.OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}

/**
* 设置item长按事件
* @param onItemLongClickListener
*/
public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
public void setOnItemLongClickListener(BaseViewHolder.OnItemLongClickListener onItemLongClickListener) {
this.mOnItemLongClickListener = onItemLongClickListener;
}

Expand All @@ -92,14 +89,12 @@ public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@Override
public void onBindViewHolder(@NonNull VH holder, final int position) {

holder.itemView.setTag(TAG_POSITION, position);

if(mOnItemClickListener != null){
holder.itemView.setOnClickListener(this);
holder.setOnItemClickListener(this);
}

if(mOnItemLongClickListener != null){
holder.itemView.setOnLongClickListener(this);
holder.setOnItemLongClickListener(this);
}

}
Expand All @@ -124,45 +119,20 @@ public int getItemCount() {
*/
protected abstract VH getViewHolder(View itemView, int viewType);

/**
* RecyclerView item点击监听
*/
public interface OnItemClickListener{
/**
* RecyclerView item点击
* @param position 位置
*/
void onItemClick(int position, @NonNull View view);
}

/**
* RecyclerView item长按监听
*/
public interface OnItemLongClickListener{
/**
* RecyclerView item长按
* @param position 位置
* @return
*/
boolean onItemLongClick(int position, @NonNull View view);
}

protected void showShortToast(String msg){
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(getPositionByView(v), v);
public void onItemClick(int position, @NonNull View view) {
mOnItemClickListener.onItemClick(position, view);
}

@Override
public boolean onLongClick(View v) {
return mOnItemLongClickListener.onItemLongClick(getPositionByView(v), v);
}

protected int getPositionByView(View view){
return (int) view.getTag(TAG_POSITION);
public boolean onItemLongClick(int position, @NonNull View view) {
return mOnItemLongClickListener.onItemLongClick(position, view);
}

@NonNull protected String getString(@StringRes int resId){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,66 @@
* BasicViewHolder
* Created by ThirtyDegreesRay on 2016/7/27 20:20
*/
public class BaseViewHolder extends RecyclerView.ViewHolder{
public class BaseViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener{

//item点击回调
private OnItemClickListener mOnItemClickListener;

//item长按回调
private OnItemLongClickListener mOnItemLongClickListener;

public BaseViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
itemView.setOnClickListener(this);
}

public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
this.mOnItemLongClickListener = onItemLongClickListener;
itemView.setOnLongClickListener(this);
}

@Override
public void onClick(View v) {
if(mOnItemClickListener != null && getAdapterPosition() != RecyclerView.NO_POSITION){
mOnItemClickListener.onItemClick(getAdapterPosition(), v);
}
}

@Override
public boolean onLongClick(View v) {
if(mOnItemLongClickListener != null && getAdapterPosition() != RecyclerView.NO_POSITION){
return mOnItemLongClickListener.onItemLongClick(getAdapterPosition(), v);
}
return false;
}

/**
* RecyclerView item点击监听
*/
public interface OnItemClickListener{
/**
* RecyclerView item点击
* @param position 位置
*/
void onItemClick(int position, @NonNull View view);
}

/**
* RecyclerView item长按监听
*/
public interface OnItemLongClickListener{
/**
* RecyclerView item长按
* @param position 位置
* @return
*/
boolean onItemLongClick(int position, @NonNull View view);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.thirtydegreesray.openhub.ui.activity.RepoListActivity;
import com.thirtydegreesray.openhub.ui.activity.UserListActivity;
import com.thirtydegreesray.openhub.ui.adapter.UsersAdapter;
import com.thirtydegreesray.openhub.ui.adapter.base.BaseAdapter;
import com.thirtydegreesray.openhub.ui.adapter.base.BaseViewHolder;
import com.thirtydegreesray.openhub.ui.fragment.base.BaseFragment;
import com.thirtydegreesray.openhub.util.AppOpener;
import com.thirtydegreesray.openhub.util.BundleHelper;
Expand All @@ -44,7 +44,7 @@

public class ProfileInfoFragment extends BaseFragment<ProfileInfoPresenter>
implements IProfileInfoContract.View,
BaseAdapter.OnItemClickListener{
BaseViewHolder.OnItemClickListener{

@BindView(R.id.name) TextView name;
@BindView(R.id.bio) TextView bio;
Expand Down
Loading

0 comments on commit cb1bd0e

Please sign in to comment.