2016-08-19 87 views
0

我正在使用BigNerdRanch庫作爲我的回收站視圖。除了突出顯示行之外,我已修復了所有內容。在LongClick 上,特定的卡片不會突出顯示。不知道爲什麼。 OnClick的作品,但在撤銷選擇期間突出顯示的原始顏色(在我的情況下是白色的)不起作用。任何想法或建議。CardView行在RecyclerView中突出顯示

這是我對RecyclerView佈局:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/frame_layout_holder"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/card_holder" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:cardCornerRadius="0dp"> 

     <RelativeLayout 
      android:id="@+id/card_linear_holder" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <ImageView 
       android:id="@+id/avatar_holder" 
       android:layout_width="@dimen/profile_pic_size" 
       android:layout_height="@dimen/profile_pic_size" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_centerVertical="true" 
       android:layout_marginLeft="@dimen/activity_horizontal_margin" 
       android:layout_marginRight="@dimen/activity_horizontal_margin" 
       android:contentDescription="Olla Image" /> 

      <LinearLayout 
       android:id="@+id/text_holder_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_toEndOf="@+id/avatar_holder" 
       android:layout_toLeftOf="@+id/checkbox_holder" 
       android:layout_toRightOf="@+id/avatar_holder" 
       android:layout_toStartOf="@+id/checkbox_holder" 
       android:orientation="vertical" 
       android:paddingBottom="@dimen/activity_vertical_margin" 
       android:paddingEnd="@dimen/activity_horizontal_margin" 
       android:paddingLeft="0dp" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:paddingStart="0dp" 
       android:paddingTop="@dimen/activity_vertical_margin"> 

       <TextView 
        android:id="@+id/text_view1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textColor="#ff00ff" 
        android:textSize="@dimen/list_item_1" /> 

       <TextView 
        android:id="@+id/text_view2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textColor="#ff00ff" 
        android:textSize="@dimen/list_item_2" /> 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/checkbox_holder" 
       android:layout_width="@dimen/check_box_holder_width" 
       android:layout_height="match_parent" 
       android:layout_alignParentEnd="true" 
       android:layout_alignParentRight="true" 
       android:gravity="center" 
       android:orientation="vertical" 
       android:visibility="gone"> 

       <CheckBox 
        android:id="@+id/checkbox" 
        android:layout_width="@dimen/check_box_width" 
        android:layout_height="@dimen/check_box_width" /> 

       <ImageView 
        android:id="@+id/delete_row" 
        android:layout_width="@dimen/check_box_width" 
        android:layout_height="@dimen/check_box_width" 
        android:contentDescription="Delete Image" 
        android:src="@android:drawable/ic_menu_close_clear_cancel" 
        android:visibility="invisible" /> 
      </LinearLayout> 

     </RelativeLayout> 

    </android.support.v7.widget.CardView> 
</FrameLayout> 

這是我實現我的點擊這是持有人裏面:

public class CustomRecyclerViewHolder extends SwappingHolder implements View.OnClickListener, View.OnLongClickListener { 

     private TextView mMsg1, mMsg2; 
     //private ImageView mAvatarView; 
     private CheckBox mCheckBox; 
     private LinearLayout checkboxHolder; 
     private ImageView mDeleteRow; 
     private CardView cardView; 
     private Category category; 
     private CustomRecyclerViewHolder customRecyclerViewHolder; 
     private int i = 0; 
     private RelativeLayout relativeLayoutInCardView; 
     /** 
     * Initializes all the views of a ViewHolder 
     * 
     * @param itemView parent view in which all the List Item views are present 
     */ 
     public CustomRecyclerViewHolder(View itemView) { 
      super(itemView, mycategoryFragment.mMultiSelector); 
      mMsg1 = (TextView) itemView.findViewById(R.id.text_view1); 
      mMsg2 = (TextView) itemView.findViewById(R.id.text_view2); 
      //mAvatarView = (ImageView)itemView.findViewById(R.id.avatar_holder); 
      mCheckBox = (CheckBox) itemView.findViewById(R.id.checkbox); 
      checkboxHolder = (LinearLayout) itemView.findViewById(R.id.checkbox_holder); 
      mDeleteRow = (ImageView) itemView.findViewById(R.id.delete_row); 
      cardView = (CardView) itemView.findViewById(R.id.card_holder); 
      relativeLayoutInCardView = (RelativeLayout) itemView.findViewById(R.id.card_linear_holder); 
      itemView.setOnClickListener(this); 
      itemView.setLongClickable(true); 
      itemView.setOnLongClickListener(this); 
     } 

     public void bindcategory(Category category) { 
      this.category = category; 
      mMsg1.setText(category.getMedicineName()); 
      mMsg2.setText(category.getDescriptionName()); 
      System.out.println("86969696978"); 
      if (mycategoryFragment.mMultiSelector.getSelectedPositions().size()==0) { 
       relativeLayoutInCardView.setBackgroundColor(Color.BLACK); 
      } 
     } 

     @Override 
     public void onClick(View v) { 

      if (category == null) { 
       return; 
      } 
      if (mycategoryFragment.mMultiSelector.tapSelection(this)) { 
       for (int i = categoryArrayList.size(); i >= 0; i--) { 
        if (mycategoryFragment.mMultiSelector.isSelected(i, 0)) { 
         System.out.println("I am at position in OnClick at " + i); 
         relativeLayoutInCardView.setBackgroundColor(Color.BLACK); 
        } 
        if (!mycategoryFragment.mMultiSelector.isSelected(i, 0)) { 
         System.out.println("I am at position in OnClick at " + i); 
         relativeLayoutInCardView.setBackgroundColor(Color.WHITE); 
        } 
       } 
       if (mycategoryFragment.mMultiSelector.getSelectedPositions().size()==0) { 
         isOnLongPressReceived = false; 
         System.out.println("I am at end"); 
       } 
      } 
     } 


     @Override 
     public boolean onLongClick(View v) { 

      if(!isOnLongPressReceived) { 
       ((AppCompatActivity) mContext).startSupportActionMode(mycategoryFragment.mDeleteMode); 
       mycategoryFragment.mMultiSelector.setSelected(this, true); 
       for (int i = categoryArrayList.size(); i >= 0; i--) { 
        if (mycategoryFragment.mMultiSelector.isSelected(i, 0)) { 
         System.out.println("I am at position in OnLong at " + i); 
         cardView.setBackgroundColor(Color.BLACK); 
        } 
       } 
       isOnLongPressReceived = true; 
      } 
      return true; 
     } 

     @Override 
     public void setSelectable(boolean isSelectable) { 
      super.setSelectable(isSelectable); 
      //relativeLayoutInCardView.setBackgroundColor(Color.BLACK); 
     } 

     @Override 
     public void setActivated(boolean isActivated) { 
      super.setActivated(isActivated); 
      relativeLayoutInCardView.setBackgroundColor(Color.WHITE); 
     } 
    } 
+0

你可以嗎你的完整GitHub回購,如果這可以與你? –

+0

是的,當然給我一些時間 –

+0

@Protino花了我一段時間來弄清楚如何在GitHub上上傳東西。在這裏完成noob。對延遲抱歉。無論如何這裏是回購。 https://github.com/kk1429/SampleCategoryApp –

回答

0

有一個很難理解,因爲的代碼不規範的代碼約定。儘管如此,高亮顯示了一張卡片在黑色時被取消並在取消選擇時回到白色是固定的。我在你的GitHub倉庫中創建了一個pull請求。您可以在這裏看到更改 - https://github.com/kk1429/SampleCategoryApp/pull/1/files

雖然存在刪除問題,但它不涉及Multiselect庫,請使用您的自定義數據庫類中的刪除方法和自定義適配器檢查該問題。

1

我不知道爲什麼,但背景不在裏面CardView我正在改變前景。

所以我排的佈局是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    card_view:cardUseCompatPadding="true" 
    android:clickable="true" 
    android:id="@+id/row_note_cardview" 
    card_view:cardCornerRadius="4dp" 
    android:foreground="?attr/selectableItemBackgroundBorderless" 
    card_view:cardElevation="4dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/row_note_frame" 
    android:foreground="@drawable/state"> 
</FrameLayout> 

    --- other layouts --- 
</android.support.v7.widget.CardView> 

state.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 

<item android:state_selected="true" android:drawable="@null"> 
    <shape android:shape="rectangle" > 
     <solid android:color="@color/colorSelected" /> 
     <stroke android:width="2dp" android:color="@color/colorSelectedDark" /> 
    </shape> 
</item> 

<item android:state_selected="false" > 
    <shape > 
     <solid android:color="@android:color/transparent" /> 
    </shape> 
</item> 

</selector> 

然後選擇項目就是這樣:viewHolder.mFrameLayout.setSelected(false/true);

如果你想看到它的模樣結帳我的問題:binary-xml-file-line-error-inflating-class-unknown