0
我有一個自定義主題適配器,每行3個項目。我設法用 填充它們,使用3 *位置,3 *位置+1和... 但是爲了添加項目點擊監聽器,在我點擊一行的每一個項目中,我都得到一個相同的位置。請幫忙。Android RecyclerView itemclick
我的主題:
<?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) {
}
}
是數據集一個列表包含每個圖像的鏈接?或者,您是否可以詳細說明您要在RecyclerView的行中渲染的模型? – pamobo0609
我在問,因爲你可以有一個帶有3個鏈接的模型作爲屬性,並且渲染1對象每行有3個,不使用「竅門」 – pamobo0609