2016-03-13 51 views
6

RecyclerView與ListView不同,因爲它不提供onItemCLickListener類來處理點擊事件。在活動中定義RecyclerView的onCLickListener

通過在ViewHolder中定義一個onClickListener,可以解決這個問題,如果沒有很多情況發生在幕後,點擊一個項目。

如果文本或任何必須傳遞給下一個Activity的東西真的不存在於ViewHolder有權訪問的視圖中,但是它是具有RecyclerView的Activity的一部分?

在這種情況下,onClickListener必須在活動內部實現,以便文本可以被轉發。有可能的。

  1. 一種方法是添加一個不可見視圖來保存該文本,然後執行之前已完成的操作;在適配器中實現onClickListener。

  2. 將文本以某種方式傳遞給適配器。

「2.」實施?

+0

[RecyclerView的onClick](可能的重複https://stackoverflow.com/questions/ 24471109/recyclerview-onclick) – Nexen

回答

8

寫公共職能在您的活動,如:

public void onClickCalled(String anyValue) { 
    // Call another acitivty here and pass some arguments to it. 
} 

現在,在您的適配器的onClick功能可按

@Override 
public void onClick(View view) { 
    ((YouActivityClass) context).onClickCalled("your argument here"); 
} 
+0

請將它作爲答案,如果它解決了您的問題 –

+0

給我一個錯誤說應用程序不能被轉換爲類 –

+0

發送您的活動上下文到適配器,然後使用此適配器來調用活動 –

1

這可以通過創建自己的監聽器,並把它傳遞給適配器來解決。

public interface PositionClickListener 
{ 
    void itemClicked(int position); 
} 

Activity可以實現這個接口,或者實例化一個匿名的內部類。所以,你的適配器可能看起來像這樣:

public class MyRecyclerAdapter extends RecyclerView.Adapter 
{ 
    public final PositionClickListener listener; 

    public MyRecyclerAdapter(PositionClickListener listener) 
    { 
     this.listener = listener; 
    } 

    private void createView(int position) 
    { 
     View v = new View(); 
     v.setOnClickListener(new OnClickListener(){ 
      listener.itemClicked(position); 
     }); 
    } 
} 
0

凡的ListView有onItemClickListener,RecyclerView有addOnItemTouchListener

您可以將偵聽器提供給活動內的回收器視圖,因此範圍將會使偵聽器能夠引用文本。

你可以使用類似於OnItemTouchListener的實現,它也可以捕獲左右滑動。

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { 

    private static final String TAG = "RecyclerTouchListener"; 

    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
    private static final int SWIPE_MAX_OFF_PATH = 250; 

    private OnTouchActionListener mOnTouchActionListener; 
    private GestureDetectorCompat mGestureDetector; 

    public static interface OnTouchActionListener { 
     public void onLeftSwipe(View view, int position); 
     public void onRightSwipe(View view, int position); 
     public void onClick(View view, int position); 
    } 

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, 
          OnTouchActionListener onTouchActionListener){ 

     mOnTouchActionListener = onTouchActionListener; 
     mGestureDetector = new GestureDetectorCompat(context,new GestureDetector.SimpleOnGestureListener(){ 

     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) { 
      Log.d(TAG, "On Single Tap Confirmed"); 
      // Find the item view that was swiped based on the coordinates 
      View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); 
      int childPosition = recyclerView.getChildPosition(child); 
      mOnTouchActionListener.onClick(child, childPosition); 
      return false; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, 
           float velocityX, float velocityY) { 
      Log.d(TAG, "onFling: " + e1.toString() + e2.toString()); 

      try { 
       if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) { 
        return false; 
       } 

       // Find the item view that was swiped based on the coordinates 
       View child = recyclerView.findChildViewUnder(e1.getX(), e1.getY()); 
       int childPosition = recyclerView.getChildPosition(child); 

       // right to left swipe 
       if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 

        Log.d(TAG, "Left Swipe"); 
        if (mOnTouchActionListener != null && child != null) { 
         mOnTouchActionListener.onLeftSwipe(child, childPosition); 
        } 

       } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
        Log.d(TAG, "Right Swipe"); 
        if (mOnTouchActionListener != null && child != null) { 
         mOnTouchActionListener.onRightSwipe(child, childPosition); 
        } 
       } 
      } catch (Exception e) { 
       // nothing 
      } 

      return false; 
     } 
    }); 
} 

    @Override 
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 
     mGestureDetector.onTouchEvent(e); 
     return false; 
    } 

    @Override 
    public void onTouchEvent(RecyclerView rv, MotionEvent e) { 
    } 

    @Override 
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 
     // do nothing 
    } 

} 

然後在你的活動添加你的聽衆一起的

mRecyclerView.addOnItemTouchListener(
     new RecyclerTouchListener(getActivity(), mRecyclerView, 
      new RecyclerTouchListener.OnTouchActionListener() { 
       @Override 
       public void onClick(View view, int position) { 
       } 
       @Override 
       public void onRightSwipe(View view, int position) { 
       } 
       @Override 
       public void onLeftSwipe(View view, int position) { 
       } 
      })) 
14

線參見下面的例子創建RecyclerViewOnClick

示例代碼:

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

public interface OnItemClickListener { 
    void onItemClick(ContentItem item); 
} 

private final List<ContentItem> items; 
private final OnItemClickListener listener; 

public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) { 
    this.items = items; 
    this.listener = listener; 
} 

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

@Override public void onBindViewHolder(ViewHolder holder, int position) { 
    holder.bind(items.get(position), listener); 
} 

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

static class ViewHolder extends RecyclerView.ViewHolder { 

    private TextView name; 
    private ImageView image; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     name = (TextView) itemView.findViewById(R.id.name); 
     image = (ImageView) itemView.findViewById(R.id.image); 
    } 

    public void bind(final ContentItem item, final OnItemClickListener listener) { 
     name.setText(item.name); 
     Picasso.with(itemView.getContext()).load(item.imageUrl).into(image); 
     itemView.setOnClickListener(new View.OnClickListener() { 
      @Override public void onClick(View v) { 
       listener.onItemClick(item); 
      } 
     }); 
    } 
} 
} 

和使用RecyclerView適配器使用下面的代碼:

recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() { 
@Override public void onItemClick(ContentItem item) { 
    Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show(); 
} 
})); 

Set a click listener to a RecyclerView

希望它是幫助你。

0

它很容易,我們沒有創建界面看看我的代碼只是把這個小的代碼,我們

@Override 
    public void onBindViewHolder(MyViewHolder holder, final int position) { 
     holder.img.setImageDrawable(img[position%img.length]); 
     holder.bodyPartName.setText(bodyPart[position]); 
     if(position==0) { 
      holder.itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Context c = v.getContext(); 
        Intent intent = new Intent(c, BodyPartExcercise.class); 
        c.startActivity(intent); 
       } 
      }); 
     } 
    } 
相關問題