2016-08-08 56 views
0

我有一个信用卡recyclerview,如果您点击一个我想要它更改该背景资源,并使其他所有其他回到默认值,出于某种原因我一直在为此奋斗了大约5个小时,并且无法正确理解。recyclerview,单击突出显示并使其他人不突出

这里是失败类

public class CheckOutCardRecycler extends RecyclerView.Adapter<CheckOutCardRecycler.CardViewHolder>{ 

    private List<Card> checkOutCard; 
    private int screenWidth; 
    private int selected; 






    public CheckOutCardRecycler(Activity activity, List<Card> checkOutCard){ 


     Display display = activity.getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     this.screenWidth = size.x; 
     this.checkOutCard = checkOutCard; 
     selected=-1; 


    } 



    public static class CardViewHolder extends RecyclerView.ViewHolder { 
     CardView card; 
     LinearLayout row; 
     static TextView etCardNumber; 
     static TextView etName; 
     static TextView etDate; 
     static LinearLayout creditCard; 



     CardViewHolder(View itemView) { 
      super(itemView); 
      card = (CardView)itemView.findViewById(R.id.card_view); 
      etCardNumber = (TextView)itemView.findViewById(R.id.etCardNumber); 
      etName = (TextView)itemView.findViewById(R.id.etName); 
      etDate = (TextView)itemView.findViewById(R.id.etDate); 
      creditCard = (LinearLayout) itemView.findViewById(R.id.creditCard); 

     } 
    } 

    @Override 
    public int getItemCount() { 
     return checkOutCard.size(); 
    } 

    public int getSelected() { 
     return selected; 
    } 

    public int getPaymentMethod(int i) { 
     return Integer.parseInt(checkOutCard.get(i).getId()); 
    } 


    @Override 
    public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_checkout_cards, viewGroup, false); 
     CardViewHolder pvh = new CardViewHolder(v); 

     return pvh; 
    } 

    @Override 
    public void onBindViewHolder(final CardViewHolder cardViewHolder, final int i) { 
     CardViewHolder.etCardNumber.setText("**** **** **** " + checkOutCard.get(i).getLast_four_digits()); 
     CardViewHolder.etName.setText(checkOutCard.get(i).getName()); 
     CardViewHolder.etDate.setText(checkOutCard.get(i).getExpiry_month() +"/"+checkOutCard.get(i).getExpiry_year().substring(2,4)); 
     if (selected<0) 
      if (checkOutCard.get(i).getDefaultcard()==1){ 
       selected=i; 
      } 

     CardViewHolder.creditCard.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 

       if (i!=selected) { 

        checkOutCard.get(selected).setDefaultcard(0); 
        checkOutCard.get(i).setDefaultcard(1); 
        selected=-1; 

        notifyDataSetChanged(); 




       } 
      } 
     }); 


     if (selected==i){ 
      if (checkOutCard.get(i).getBrand().toLowerCase().equals("visa")) 
       CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenvisaselected); 
      else if (checkOutCard.get(i).getBrand().toLowerCase().equals("mastercard")) 
       CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenmastercardselected); 
      else 
       CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenselected); 
     } 
     else { 

      if (checkOutCard.get(i).getBrand().toLowerCase().equals("visa")) 
       CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenvisa); 
      else if (checkOutCard.get(i).getBrand().toLowerCase().equals("mastercard")) 
       CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreenmastercard); 
      else 
       CardViewHolder.creditCard.setBackgroundResource(R.drawable.paymentscreen); 

     } 


    } 

    @Override 
    public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
     super.onAttachedToRecyclerView(recyclerView); 

    } 




} 
+0

作出长时间的答案,你可以一次突出多个? – JCDecary

+0

没有一个。它实质上是选定的卡 –

+0

尽管您可以在ViewHolder的构造函数中执行相同操作,但不要在onBindViewHolder中附加clicklistener。这样你就不需要随着用户的滚动而连接一个监听器。我在答案中增加了一个工作解决方案。希望你觉得它有用。 – user1841702

回答

0

感谢大家的回答,终于在尝试了所有解决方案之后,没有一个人工作,也没有研究出为什么我终于明白了,我有一个愚蠢的语法问题,让我头痛。

我所要做的只是在onBindViewHolder中,我不得不将所有CardViewHolder都更改为cardViewHolder。我想这使得这个问题对于其他人来说毫无意义,所以我的apolgies,再次感谢所有努力,其他人在

1

ruffly适配器应该是这个样子:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> { 
    private Context context; 
    private List<Card> checkOutCard; 
    private int screenWidth; // I really think you shouldn't calculate your width in the adapter but before your create it. 
    private Card selected; 

    public CardAdapter(Context context, List<Card> checkOutCard, int screenWidth) { 
     this.context = context; 
     this.screenWidth = screenWidth; 
     this.checkOutCard = checkOutCard; 
    } 

    protected Context getContext() { 
     return context; 
    } 

    protected Card getItem(int position){ 

    } 

    public Card getSelected() { 
     return selected; 
    } 

    public void setSelected(Card selected) { 
     this.selected = selected; 
    } 

    @Override 
    public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return new CardViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.row_checkout_cards, parent, false)); 
    } 

    @Override 
    public void onBindViewHolder(CardViewHolder holder, int position) { 
     Card currentCard = getItem(position); 
     holder.bind(currentCard, currentCard == selected); 

    } 

    @Override 
    public int getItemCount() { 
     return 0; 
    } 

    public static class CardViewHolder extends RecyclerView.ViewHolder { 
     CardView card; 
     LinearLayout row; 
     static TextView etCardNumber; 
     static TextView etName; 
     static TextView etDate; 
     static LinearLayout creditCard; 

     CardViewHolder(View itemView) { 
      super(itemView); 
      card = (CardView)itemView.findViewById(R.id.card_view); 
      etCardNumber = (TextView)itemView.findViewById(R.id.etCardNumber); 
      etName = (TextView)itemView.findViewById(R.id.etName); 
      etDate = (TextView)itemView.findViewById(R.id.etDate); 
      creditCard = (LinearLayout) itemView.findViewById(R.id.creditCard); 

     } 

     public void bind(Card card, boolean isSelected){ 
      etCardNumber.setText("**** **** **** " + card.getLast_four_digits()); 
      etName.setText(card.getName()); 
      etDate.setText(card.getExpiry_month() +"/"+card.getExpiry_year().substring(2,4)); 
      if (isSelected) 
       if (card.getDefaultcard()==1){ 
        selected=i; 
       } 

      creditCard.setOnClickListener(new View.OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 

        if (i!=selected) { 

         checkOutCard.get(selected).setDefaultcard(0); 
         card.setDefaultcard(1); 
         selected=-1; 

         notifyDataSetChanged(); 




        } 
       } 
      }); 


      if (selected==i){ 
       if (card.getBrand().toLowerCase().equals("visa")) 
        creditCard.setBackgroundResource(R.drawable.paymentscreenvisaselected); 
       else if (card.getBrand().toLowerCase().equals("mastercard")) 
        creditCard.setBackgroundResource(R.drawable.paymentscreenmastercardselected); 
       else 
        creditCard.setBackgroundResource(R.drawable.paymentscreenselected); 
      } 
      else { 

       if (card.getBrand().toLowerCase().equals("visa")) 
        creditCard.setBackgroundResource(R.drawable.paymentscreenvisa); 
       else if (card.getBrand().toLowerCase().equals("mastercard")) 
        creditCard.setBackgroundResource(R.drawable.paymentscreenmastercard); 
       else 
        creditCard.setBackgroundResource(R.drawable.paymentscreen); 

      } 
     } 
    } 
} 
1

这里是一个工作示例:

适配器:

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.TestViewHolder> { 


    private List<String> mData; 
    Context mContext; 
    RecyclerView mRecyclerView; 


    public class TestViewHolder extends RecyclerView.ViewHolder{ 

     LinearLayout mListItemLayout; 
     TextView mTextView; 
     int mPosition; 

     public TestViewHolder(final View itemView) { 
      super(itemView); 
      mListItemLayout = (LinearLayout) itemView.findViewById(R.id.list_item); 
      mTextView = (TextView) itemView.findViewById(R.id.list_item_text); 
      mListItemLayout.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        itemView.setBackgroundColor(Color.RED); 
        grayBackgroundOfOtherItems(); 
       } 
      }); 

     } 

     public void setPosition(int position){ 
      mPosition = position; 
     } 

     private void grayBackgroundOfOtherItems(){ 

      int size = mData.size(); 
      for(int i=0; i<size; i++){ 
       if(i != mPosition){ 
        TestViewHolder viewHolder = (TestViewHolder) mRecyclerView.findViewHolderForAdapterPosition(i); 
        ((TestViewHolder) mRecyclerView.findViewHolderForAdapterPosition(i)). 
              mListItemLayout.setBackgroundColor(Color.LTGRAY); 
       } 
      } 
     } 

    } 

    public TestAdapter(Context context, RecyclerView recyclerView){ 

     this.mContext = context; 
     this.mRecyclerView = recyclerView; 
     initData(); 
    } 

    private void initData(){ 

     mData = new ArrayList<String>(); 
     mData.add("Africa"); 
     mData.add("Antartica"); 
     mData.add("Asia"); 
     mData.add("Australia"); 
     mData.add("Europe"); 
     mData.add("North America"); 
     mData.add("South America"); 
    } 

    @Override 
    public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false); 
     TestViewHolder viewHolder = new TestViewHolder(view); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(TestViewHolder holder, int position) { 

     holder.mTextView.setText(mData.get(position)); 
     holder.setPosition(position); 
    } 

    @Override 
    public int getItemCount() { 
     return mData.size(); 
    } 
} 

活动:

public class CardViewActivity extends AppCompatActivity{ 

    RecyclerView mRecyclerView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_card_view); 

     mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     mRecyclerView.setAdapter(new TestAdapter(this,mRecyclerView)); 
    } 
} 

该活动的布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".stack_overflow_cardview.CardViewActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </android.support.v7.widget.RecyclerView> 

</RelativeLayout> 

而且每个项目的布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id = "@+id/list_item" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_marginTop="10dp"> 

    <TextView 
     android:id = "@+id/list_item_text" 
     android:layout_width = "wrap_content" 
     android:layout_height = "wrap_content" /> 

</LinearLayout> 

而且截图:

enter image description here

希望这很有用。

编辑:如果您不喜欢通过在适配器中引用它来引用RecyclerView,那么您可以使用Activity/Fragment必须实现的接口,并将该接口传递给接口方法。