我已将Recyclerview与swipe to delete undo library一起使用。它工作正常,但回收站视图项目选择不起作用。 我试图改变所选列表项的背景颜色。但它不起作用。 如何正确显示所选项目而不影响滑动删除选项?我在下面的代码中犯了什么错误?
MyActivity.java:
myListAdapter = new MyListAdapter(this,null);
mRecyclerView.setAdapter(myListAdapter);
final SwipeToDismissTouchListener touchListener = new SwipeToDismissTouchListener( new RecyclerViewAdapter(mRecyclerView),new SwipeToDismissTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(ViewAdapter viewAdapter, int position) {
//mRecyclerView.removeViewAt(position);
}
});
mRecyclerView.setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during ListView scrolling,
// we don't look for swipes.
mRecyclerView.setOnScrollListener((RecyclerView.OnScrollListener) touchListener.makeScrollListener());
//mRecyclerView.setActivated(true);
//mRecyclerView.setBackgroundResource(R.drawable.list_selector);
mRecyclerView.addOnItemTouchListener(new SwipeableItemClickListener(this,new com.hudomju.swipe.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.i(TAG, "selected position -> " + position);
view.setActivated(true);
view.setEnabled(true);
view.setSelected(true);
view.setPressed(true);
//view.setBackgroundColor(R.drawable.list_selector);
if(view.getId() == R.id.list_item_backside_delete_button){
// done operations on delete button click
touchListener.processPendingDismisses();
}
else if(view.getId() == R.id.list_item_backside_cancel_button){
// done operations on cancel button click
touchListener.undoPendingDismiss();
}
}
}));
MyListAdapter.java:
public class MyListAdapter extends CursorRecyclerViewAdapter<MyListAdapter.ViewHolder>{
public MyListAdapter(Context context,Cursor cursor){
super(context,cursor);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView1;
public ViewHolder(View view) {
super(view);
mTextView1 = (TextView) view.findViewById(R.id.textview_name);
/*view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
}
});*/
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_view, parent, false);
ViewHolder vh = new ViewHolder(itemView);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {
if(cursor != null && cursor.getCount() > 0){
String selectedID = cursor.getString(cursor.getColumnIndex(DBContract.Data._ID));
String name = cursor.getString(cursor.getColumnIndex(DBContract.Data.COLUMN_NAME_NAME));
viewHolder.mTextView1.setText(name);
}
}
}
list_item_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="@drawable/list_selector"
>
</FrameLayout>
list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="false" android:drawable="@android:color/transparent" />
<item android:state_activated="true" android:drawable="@color/accent" />
<item
android:drawable="@android:color/transparent" />
</selector>
答案 0 :(得分:0)
使全局变量存储位置并在ViewHolder中处理单击侦听器。单击项目,更改全局位置值,如
find dir/ -type f -print0 | xargs --null file --mime-type -b | wc -l
然后在onBindViewHolder
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
globalPosition=getAdapterPosition();
notifyDataSetChanged();
}
});
使用此代码,您单击的项目将显示为红色,而其他所有项目均为白色