2017-07-17 9 views
0

私は、各行に3つのアイテムを持つカスタムテーマアダプターを持っています。 3 *位置、3 *位置+1、および... のようないくつかのトリックを使ってそれらを埋めることができました。クリックリスナー、私は1つの行をクリックしたすべての項目で私は同じ位置を取得します。助けてください。Android RecyclerView itemclick

MYテーマ:

<?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> 

MYアダプタ:

@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) { 

    } 
} 
+0

データセットはすべての画像のリンクを含むリストですか?または、あなたはRecyclerViewの行でレンダリングしようとしているモデルについて詳しく調べることができますか? – pamobo0609

+0

3つのリンクを持つモデルを属性として持つことができ、3つの行で1つのオブジェクトをレンダリングすると「トリック」を使用しないため、 – pamobo0609

答えて

0

は、最後に私はそれを修正しました。 カスタムテーマビューにタグを設定し、タッチリスナーを使用してそれらを渡します。 よくthats複雑