2016-01-02 43 views
0

我試圖捕獲在我的RecyclerView中的項目單擊事件,但它不工作。我的研究(SO)告訴我把聽者放在適配器中,所以我做了。代碼如下:RecylcerView項目單擊收聽器

public class PoliticianPagerAdapter extends RecyclerView.Adapter<PoliticianPagerAdapter.ViewHolder> { 
     private Politician[] mDataset; 

     // Provide a reference to the views for each data item 
     // Complex data items may need more than one view per item, and 
     // you provide access to all the views for a data item in a view holder 
     public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
      // each data item is just a string in this case 
      public TextView name; 
      public TextView term; 
      public TextView position; 
      public ImageView image; 
      public CardView card ; 
      public ViewHolder(View layout) { 
       super(layout); 
       name = (TextView) layout.findViewById(R.id.politicianNameAndParty); 
       term = (TextView) layout.findViewById(R.id.politicianTerm); 
       position = (TextView) layout.findViewById(R.id.politicianPosition); 
       image = (ImageView) layout.findViewById(R.id.politicianImage); 
       card = (CardView) layout.findViewById(R.id.politicianCard); 
      } 

      @Override 
      public void onClick(View view) 
      { 

       Intent intent = new Intent(getActivity(),PoliticianPage.class); 
       startActivity(intent); 


      } 
     } 

     public PoliticianPagerAdapter(Politician[] myDataset) { 
      mDataset = myDataset; 
     } 

     @Override 
     public PoliticianPagerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
                 int viewType) { 
      View v = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.fragment_home_politician_block, parent, false); 

      return new ViewHolder(v); 

     } 

     // Replace the contents of a view (invoked by the layout manager) 
     @Override 
     public void onBindViewHolder(ViewHolder holder, int position) { 
      // - get element from your dataset at this position 
      // - replace the contents of the view with that element 
      holder.name.setText(mDataset[position].name + " (" + mDataset[position].party + ")"); 
      holder.term.setText(Integer.toString(mDataset[position].termBegin) + " - " + Integer.toString(mDataset[position].termEnd)); 
      holder.term.setText(mDataset[position].position); 
      holder.image.setImageResource(mDataset[position].image); 


     } 

     // Return the size of your dataset (invoked by the layout manager) 
     @Override 
     public int getItemCount() { 
      return mDataset.length; 
     } 
    } 

回答

1

使用CardView中的View來攔截click事件。

item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_margin="4dp" 
     android:elevation="3dp" 
     card_view:cardCornerRadius="4dp"> 

     <!-- Your card --> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/clickable_view_card" 
      android:clickable="true"/> 


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

適配器:

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> { 

     public class ViewHolder extends RecyclerView.ViewHolder { 
      public RelativeLayout clickable_layout; 



      public ViewHolder(View itemView) { 
       super(itemView); 
       clickable_layout = (RelativeLayout) itemView.findViewById(R.id.clickable_view_card); 


      } 
     } 

     @Override 
     public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View v = LayoutInflater.from(parent.getContext()) 
          .inflate(R.layout.card_view, parent, false); 
      return new ViewHolder(v); 
     } 

     @Override 
     public void onBindViewHolder(final Adapter.ViewHolder holder, final int position) { 

      holder.clickable_layout.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Log.d("OnClick", "position clicked: "+ position); 
       } 
      }); 

     } 

     @Override 
     public int getItemCount() { 
      return list.size(); 
     } 
    } 
+0

你的救星:)你能解釋一下這個工作的原因,但我已經看到了在其他地方沒有按網上的解決方案「T?我假設我的案件與其他案件不同,但是是什麼讓它如此呢?無論如何,非常感謝! @VictorBG –

+0

大多數解決方案是使用MotionEvent捕獲點擊事件,但是當您的視圖與卡片之外可點擊時,這不起作用,因爲您捕獲了所有卡片中的點擊事件。在CardView中使用一個視圖(在這種情況下爲RelativeLayout),並將其設置爲可點擊的,您可以在按鈕中添加其可點擊方法中的其他視圖並單獨截獲點擊,但不能將可點擊視圖放置在另一個可點擊的視圖中。 – VictorBG

+0

好吧,非常感謝@VictorBG –

相關問題