2015-06-13 84 views
1

我已使用Recyclelerview與swipe to delete undo library。它工作正常,但回收查看項目選擇不起作用。 我試圖改變選定列表項的背景顏色,但它不起作用。 如何正確顯示選定的項目而不影響刷卡刪除選項?我在我的下面的代碼上犯了什麼錯誤?Recyclerview選定的商品突出顯示不起作用

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

嘗試android:state_pressed而不是android:state_activated在你的list_selector.xml中 –

+0

@MarkusRubey嘗試過 但沒有任何反應。也嘗試通過設置mRecyclerView.setSelected(true); mRecyclerView.setPressed(true);但不起作用 – Ramprasad

+0

似乎是你使用的庫中的一個[bug](https://github.com/hudomju/android-swipe-to-dismiss-undo/issues/6)。 –

回答

0

使全局變量來存儲位置和處理單擊ViewHolder聽衆。項目的onclick,這個代碼改變全球位置值一樣

textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { globalPosition=getAdapterPosition(); notifyDataSetChanged(); } });

然後在onBindViewHolder

if(postion==globalPosition) 
{ 
    //change color like 
    textview.setTextColor(Color.RED); 
} 
else 
{ 
    //revert back to regular color 
    textview.setTextColor(Color.WHITE); 
} 

你點擊獲得紅色的項目和其他所有wiil在白色