gpt4 book ai didi

android recyclerview 项目点击

转载 作者:太空狗 更新时间:2023-10-29 16:33:46 25 4
gpt4 key购买 nike

我在我的 android 项目中创建了一个 recyclerView。我有 11 个项目。每个项目都包含一个 ImageView 和两个 TextView 。当我单击任何项​​目时,它应该转到它们相应的 Activity 。我如何完成它。下面是我的 recyclerView 适配器的代码。请帮助我。

回收适配器.java:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
* Created by Sabudaniel61 on 2015-10-15.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
HotelData[] hoteldata;View view;
RecyclerAdapter(HotelData[] hoteldata){
this.hoteldata=hoteldata;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout,null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
viewHolder.textView1.setText(hoteldata[i].getHotel());
viewHolder.textView2.setText(hoteldata[i].getPlace());


}

@Override
public int getItemCount() {
return hoteldata.length;
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView textView1,textView2;ImageView imageView;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
textView1=(TextView) itemLayoutView.findViewById(R.id.textView1);
textView2=(TextView) itemLayoutView.findViewById(R.id.textView2);
imageView=(ImageView) itemLayoutView.findViewById(R.id.imageView1);


}

}
}

最佳答案

在您的 RecyclerAdapter 中创建一个 onclicklistener。

 private final OnClickListener mOnClickListener = new MyOnClickListener();

@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int position) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout,null);
view.setOnClickListener(mOnClickListener);
return new MyViewHolder(view);
}

点击方法:

@Override
public void onClick(final View view) {
int itemPosition = mRecyclerView.getChildPosition(view);
String item = mList.get(itemPosition);//if you want to pass the data
//Navigate to your desired activity.
}

这是最简单的方法。

关于android recyclerview 项目点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com