我有600个项目的数组。我已将所有数组项放在RecyclerView中,并且它们滚动得很好。但是,当我尝试访问onClick监听器时,它没有被调用。我还尝试在我的ViewHolder类中实现View.OnClickListener和Override onClick,但它无法正常工作。
当用户点击视图中的某个项目时,我想要向列表中的点击项目干燥数据
适配器代码
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private String mItem;
private TextView mTextView;
public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mTextView = (TextView) view.findViewById(R.id.item_title);
}
public void setItem(String item) {
mItem = item;
mTextView.setText(item);
}
@Override
public void onClick(View view) {
Log.d("TAG", "onClick " + getPosition() + " " + mItem);
}
}
private String[] mDataset;
public MyAdapter(String[] dataset) {
mDataset = dataset;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setItem(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
}
答案 0 :(得分:3)
Try this..
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder
{
private String mItem;
private TextView mTextView;
public ViewHolder(View view) {
super(view);
mTextView = (TextView) view.findViewById(R.id.item_title);
}
public void setItem(String item) {
mItem = item;
mTextView.setText(item);
}
private String[] mDataset;
Context context;
public MyAdapter(Context context,String[] dataset) {
this.context=context;
mDataset = dataset;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setOnClickListener(new ItemClicklistener(position);
holder.setItem(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
class ItemClicklistener implements View.OnClickListener {
private int position;
ItemClicklistener(int position) {
this.position = position;
}
@Override
public void onClick(View view) {
Toast.makeText(context,"OnClick :"+position,Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:0)
public class MyAdapter extends RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<MyModel> list;
View v1;
RecyclerView.ViewHolder vh;
public MyAdapter(List<MyModel> lists) {
list = lists;
}
@Override
public int getItemViewType(int position) {
return list.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
if (viewType == VIEW_ITEM) {
v1 = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item, parent, false);
vh = new MyViewHolder(v1);
return vh;
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
final MyModel mymodel= (MyModel) list.get(position);
((MyViewHolder) holder).mTextView.setText(mymodel.getItem());
((MyViewHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("TAG", "onClick " + Integer.toString(position) + " " + mymodel.getItem());
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView;
public MyViewHolder(View view) {
super(view);
mTextView = (TextView) view.findViewById(R.id.item_title);
}
}
}
// List Item Stored
public class MyModel extends Application implements Serializable {
private static final long serialVersionUID = 1L;
private String item,
public MyModel() {}
public MyModel(String itemtext) {
this.item = itemtext;
}
public String getItem() {return item;}
public void setItem(String id){ this.item = item;}
}
//use this code in main activity
List<MyModel> Listitem = new ArrayList<CarListGetterSetter>();
for(int i=0;i<600;i++){
Listitem.add(new MyModel("hi"+i);
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
MyAdapter mAdapter = new MyAdapter(Listitem);
recyclerView.setAdapter(mAdapter);
答案 2 :(得分:0)
您可以通过这种方式实现RecyclerView适配器以支持onItemCLick和OnItemLongClick
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder>{
ArrayList<Person> data;
Context context;
HashMap<String, Integer> screenDetails;
private static ClickListener clickListener;
public interface ClickListener {
void onItemClick(int position, View v);
void onItemLongClick(int position, View v);
}
public void setOnItemClickListener(ClickListener clickListener) {
PersonAdapter.clickListener = clickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
public View view;
TextView full_name;
public ViewHolder(View v) {
super(v);
view = v;
full_name = (TextView)view.findViewById(R.id.first_name);
v.setOnClickListener(this);
v.setOnLongClickListener(this);
}
@Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);
}
@Override
public boolean onLongClick(View v) {
clickListener.onItemLongClick(getAdapterPosition(), v);
return false;
}
}
public PersonAdapter(Context context, ArrayList<Person> data) {
this.context = context;
this.data = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View cardView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_chatroom_item, parent, false);
return new ViewHolder(cardView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Person person = data.get(position);
}
@Override
public int getItemCount() {
return data.size();
}
}
在您的活动中以这种方式实施
PersonAdapter personAdapter = new PersonAdapter(getApplicationContext(),persons);
personAdapter.setOnItemClickListener(new PersonAdapter.ClickListener() {
@Override
public void onItemClick(int position, View v) {
}
@Override
public void onItemLongClick(int position, View v) {
}
});
recyclerView.setAdapter(PersonAdapter);