实际上,我无法为此回收器视图适配器设置Click侦听器。如果你能帮上忙,这意味着很多?
我已经尝试了所有可能的方法来添加它,但由于是新手,所以我不能这样做。...
Public class Result_page_travelumActivity extends AppCompatActivity {
Integer image1[] ={R.drawable.img1,R.drawable.img2,R.drawable.img3};
private PrefManager prefnew;
private SharedPreferences pref;
private ProgressDialog pDialog;
private RecyclerView recyclerView;
private Result_page_travelum_adapter result_page_travelum_adapter;
private ArrayList<Result_page_travelum_model> result_page_travelum_models;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
this.getSupportActionBar().hide();
}
catch (NullPointerException e){}
setContentView(R.layout.activity_result_page_travelum);
recyclerView = findViewById(R.id.rc1);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(Result_page_travelumActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
result_page_travelum_models = new ArrayList<>();
for (int i=0;i < image1.length;i++) {
Result_page_travelum_model ab = new Result_page_travelum_model(image1[i]);
result_page_travelum_models.add(ab);
}
result_page_travelum_adapter = new Result_page_travelum_adapter(Result_page_travelumActivity.this,result_page_travelum_models);
recyclerView.setAdapter(result_page_travelum_adapter);
}
}
我希望如果我单击显示的任何项目,它将重定向到另一个活动。 我需要设置项目点击监听器
public class Result_page_travelum_adapter extends RecyclerView.Adapter<Result_page_travelum_adapter.ViewHolder> {
Context context;
ArrayList<Result_page_travelum_model>result_page_travelum_models;
public Result_page_travelum_adapter(Context context, ArrayList<Result_page_travelum_model> result_page_travelum_models) {
this.context = context;
this.result_page_travelum_models = result_page_travelum_models;
}
@NonNull
@Override
public Result_page_travelum_adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result_home_page,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Result_page_travelum_adapter.ViewHolder holder, int position) {
holder.image1.setImageResource(result_page_travelum_models.get(position).getImage1());
}
@Override
public int getItemCount() {
return result_page_travelum_models.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image1;
public ViewHolder(View itemView) {
super(itemView);
image1 = itemView.findViewById(R.id.image1);
}
}
}
上面是适配器代码
答案 0 :(得分:0)
基本上,您想在连接到RecyclerView的适配器内部进行项目处理。
可以在您的onBindViewHolder的adapter方法中注册监听器。
或者直接在ViewHolder(也是considered as best practice)中作为更明确的方法。
您可以查看Antonio Leiva https://antonioleiva.com/recyclerview-listener/
的示例答案 1 :(得分:0)
您可以在{strong> adapater 中维护onCLick
,但也可以直接在RecyclerView上添加Item Touch Listener
。为此,请首先创建此课程在您的应用中。
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
然后在回收站上致电addOnItemTouchListener
,查看此内容。
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//Intent to another activity
}
}));