2016-07-29 76 views
1

使用以下代碼爲cardview設置onClickListener。Cardview position is always -1

@Override 
    public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.album_card, parent, false); 
     final CardView card = (CardView)itemView.findViewById(R.id.card_view); 
     final MyViewHolder holder = new MyViewHolder(itemView); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      StateListAnimator stateListAnimator = AnimatorInflater 
        .loadStateListAnimator(mContext, R.anim.lift_on_touch); 
      card.setStateListAnimator(stateListAnimator); 
     } 
     card.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       final int position = holder.getAdapterPosition(); 
      Log.d("Position",""+position); 
       if (position == 2) { 
        Intent intent = new Intent(mContext, ExperienceActivity.class); 
        mContext.startActivity(intent); 
       } 
      } 
     }); 

     return new MyViewHolder(itemView); 
    } 

OnClickListener工作正常,但爲什麼該位置總是返回-1?

+0

holder.getAdapterPosition()您正在調用。但是,你在哪裏設置持有人的位置? –

+0

林不知道。你應該在哪裏設置它..你可以澄清 – Learner

+0

最終CardView卡=(CardView)itemView.findViewById(R.id.card_view); final MyViewHolder holder = new MyViewHolder(itemView); holder.setPosition(YOUR_POSITION); –

回答

3

我看到的問題是你在'Oncreateviewholde'中返回一個新的視圖持有者,而不是你已經創建的視圖持有者。其他

@Override 
     public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { 
      View itemView = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.album_card, parent, false); 
      final CardView card = (CardView)itemView.findViewById(R.id.card_view); 
      final MyViewHolder holder = new MyViewHolder(itemView); 
    // 
    // your code 

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

        final int position = holder.getAdapterPosition(); 
       } 
      }); 

      return new MyViewHolder(itemView); // problem, remove it 
      return holder; // use this 
     } 

的是它是一個很好的做法,如果你設置的聽衆和初始化你持有人的構造函數中的觀點是這樣

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 


    public TextView title; 
    public ImageView image; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     title = (TextView) itemView.findViewById(R.id.title); 
     image = (ImageView) itemView.findViewById(R.id.thumbnail); 
     image.setOnClickListener(this); 
    } 
    @Override 
    public void onClick(View view) { 
     Log.d("pos",""+getAdapterPosition()); 
     removeAt(getAdapterPosition());  
    } 
} 
public void removeAt(int position) { 
    list.remove(position); 
    notifyDataSetChanged(); 
} 
1

用戶標籤的概念得到位置。

@Override 
     public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { 
      View itemView = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.album_card, parent, false); 
      final CardView card = (CardView)itemView.findViewById(R.id.card_view); 
      final MyViewHolder holder = new MyViewHolder(itemView); 
      holder.setTag(YOUR_POSTION); 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       StateListAnimator stateListAnimator = AnimatorInflater 
         .loadStateListAnimator(mContext, R.anim.lift_on_touch); 
       card.setStateListAnimator(stateListAnimator); 
      } 
      card.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        final int position = (int)holder.getTag(); 
       Log.d("Position",""+position); 
        if (position == 2) { 
         Intent intent = new Intent(mContext, ExperienceActivity.class); 
         mContext.startActivity(intent); 
        } 
       } 
      }); 

      return new MyViewHolder(itemView); 
     } 
+0

你的位置是什麼? – Learner