Android RecyclerView项目点击

时间:2017-07-17 14:22:02

标签: android android-recyclerview

我有一个自定义主题适配器,每行有3个项目。我设法填充它们  使用一些技巧,如3 *位置,3 *位置+ 1和... 但是为了添加项目点击监听器,在我单击一行的每个项目中我得到相同的位置。请帮忙。

我的主题:

<?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"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/linear">

        <ImageView
            android:id="@+id/img3"
            android:scaleType="fitXY"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:layout_margin="5dp"
            />

        <ImageView
            android:layout_height="100dp"
            android:id="@+id/img2"
            android:scaleType="fitXY"
            android:layout_width="100dp"
            android:layout_weight="1"
            android:layout_margin="5dp"
            />

        <ImageView
            android:layout_height="100dp"
            android:id="@+id/img1"
            android:scaleType="fitXY"
            android:layout_width="100dp"
            android:layout_weight="1"
            android:layout_margin="5dp"/>

    </LinearLayout>

我的适应者:

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

if(mPhotos.size()>(3*position)){
  Photo item = mPhotos.get(3*position);

  Picasso.with(MBase.getContext())
    .load(item.getMedia())
    .placeholder(android.R.drawable.btn_star)
    .into(holder.img1);
}else {
  return;
}
if(mPhotos.size()>(((3*position)+1))){
  Photo item=mPhotos.get(((3*position)+1));

  Picasso.with(MBase.getContext())
    .load(item.getMedia())
    .placeholder(android.R.drawable.btn_star)
    .into(holder.img2);
}
else {
  return;
}
if(mPhotos.size()>(((3*position)+2))){
  Photo item=mPhotos.get(((3*position)+2));
  Picasso.with(MBase.getContext())
    .load(item .getMedia())
    .placeholder(android.R.drawable.btn_star)
    .into(holder.img3);
}else {
  return;
}

}

我的项目clicklistener:

public class RecyclerItemListener implements RecyclerView.OnItemTouchListener {
  public static interface ClickListener{
    public void onClick(View view,int position);
    public void onLongClick(View view, int position);
  }
  private ClickListener clicklistener;
  private GestureDetector gestureDetector;
  public RecyclerItemListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener){
    this.clicklistener=clicklistener;
    gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
      @Override
      public boolean onSingleTapUp(MotionEvent e) {
        return true;
      }
      @Override
      public void onLongPress(MotionEvent e) {
        View child=recycleView.findChildViewUnder(e.getX(),e.getY());

        if(child!=null && clicklistener!=null){
          clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child));
        }
      }
    });
  }
  @Override
  public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    View child=rv.findChildViewUnder(e.getX(),e.getY());
    if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){
      clicklistener.onClick(child,rv.getChildLayoutPosition(child));
    }
    return false;
  }
  @Override
  public void onTouchEvent(RecyclerView rv, MotionEvent e) {
  }
  @Override
  public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

  }
}

1 个答案:

答案 0 :(得分:0)

最后我修好了。 我将标记设置为自定义主题视图,然后使用触摸侦听器传递它们。 那太复杂了

相关问题