如何在可扩展的recyclerview 上实现onclicklistener
How to implement onclicklistener on expandable recyclerview
我有一个回收站视图,该视图已在实现后得到扩展 here。我想在回收站视图的子视图上实现 onClickListener,这样当我单击该子视图时,我将收到一条 Toast 消息,显示子文本视图中包含的文本。
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final Item item = data.get(position);
switch (item.type) {
case HEADER:
final MyViewHolder itemController = (MyViewHolder) holder;
itemController.referalItem = item;
itemController.alphabet.setText(item.text.toString());
if (item.invisibleChildren == null) { itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_add);
} else { itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_remove);
}
itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (item.invisibleChildren == null) {
item.invisibleChildren = new ArrayList<Item>();
int count = 0;
int pos = data.indexOf(itemController.referalItem);
while (data.size() > (pos + 1) && data.get(pos + 1).type == CHILD) {
item.invisibleChildren.add(data.remove(pos + 1));
count++;
}
notifyItemRangeRemoved(pos + 1, count);
itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_add);
} else {
int pos = data.indexOf(itemController.referalItem);
int index = pos + 1;
for (Item i : item.invisibleChildren) {
data.add(index, i);
index++;
}
notifyItemRangeInserted(pos + 1, index - pos - 1);
itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_remove);
item.invisibleChildren = null;
}
}
});
break;
case CHILD:
final TextView itemTextView = (TextView) holder.itemView;
try {
itemTextView.setText(data.get(position).text.toString());
itemTextView.setOnClickListener(new View.OnClickListener() { //setting the onclicklistener
@Override
public void onClick(View v) throws NullPointerException {
Toast.makeText(context, itemTextView.getText().toString(), Toast.LENGTH_SHORT).show(); //Gives nullpointer error
}
});
} catch (NullPointerException e){
}
break;
但是每当我在 recyclerview.Any 帮助中单击子项时,我都会收到 NullPointerException,将不胜感激。谢谢。
修改适配器代码
public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//-----add
interface OnItemClickListener{
void OnItemClick(int position);
}
OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
//------
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final Item item = data.get(position);
switch (item.type) {
case HEADER:
final ListHeaderViewHolder itemController = (ListHeaderViewHolder) holder;
itemController.refferalItem = item;
itemController.header_title.setText(item.text);
if (item.invisibleChildren == null) {
itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
} else {
itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
}
itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (item.invisibleChildren == null) {
item.invisibleChildren = new ArrayList<Item>();
int count = 0;
int pos = data.indexOf(itemController.refferalItem);
while (data.size() > pos + 1 && data.get(pos + 1).type == CHILD) {
item.invisibleChildren.add(data.remove(pos + 1));
count++;
}
notifyItemRangeRemoved(pos + 1, count);
itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
} else {
int pos = data.indexOf(itemController.refferalItem);
int index = pos + 1;
for (Item i : item.invisibleChildren) {
data.add(index, i);
index++;
}
notifyItemRangeInserted(pos + 1, index - pos - 1);
itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
item.invisibleChildren = null;
}
}
});
break;
case CHILD:
TextView itemTextView = (TextView) holder.itemView;
itemTextView.setText(data.get(position).text);
//-----add
itemTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onItemClickListener!=null){
onItemClickListener.OnItemClick(position);
}
}
});
//------
break;
}
}
使用喜欢
ExpandableListAdapter adapter = new ExpandableListAdapter(data);
recyclerview.setAdapter(adapter);
adapter.setOnItemClickListener(new ExpandableListAdapter.OnItemClickListener() {
@Override
public void OnItemClick(int position) {
Toast.makeText(MainActivity.this, data.get(position).text,Toast.LENGTH_SHORT).show();
}
});
只需转到 GenreAdapter 中的 onBindChildViewHolder 并添加:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mactive,artist.getName(),Toast.LENGTH_SHORT).show();
}
});
顺便说一下,mactive 是构造函数
传入的activity
我有一个回收站视图,该视图已在实现后得到扩展 here。我想在回收站视图的子视图上实现 onClickListener,这样当我单击该子视图时,我将收到一条 Toast 消息,显示子文本视图中包含的文本。
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final Item item = data.get(position);
switch (item.type) {
case HEADER:
final MyViewHolder itemController = (MyViewHolder) holder;
itemController.referalItem = item;
itemController.alphabet.setText(item.text.toString());
if (item.invisibleChildren == null) { itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_add);
} else { itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_remove);
}
itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (item.invisibleChildren == null) {
item.invisibleChildren = new ArrayList<Item>();
int count = 0;
int pos = data.indexOf(itemController.referalItem);
while (data.size() > (pos + 1) && data.get(pos + 1).type == CHILD) {
item.invisibleChildren.add(data.remove(pos + 1));
count++;
}
notifyItemRangeRemoved(pos + 1, count);
itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_add);
} else {
int pos = data.indexOf(itemController.referalItem);
int index = pos + 1;
for (Item i : item.invisibleChildren) {
data.add(index, i);
index++;
}
notifyItemRangeInserted(pos + 1, index - pos - 1);
itemController.btn_expand_toggle.setImageResource(R.mipmap.knob_remove);
item.invisibleChildren = null;
}
}
});
break;
case CHILD:
final TextView itemTextView = (TextView) holder.itemView;
try {
itemTextView.setText(data.get(position).text.toString());
itemTextView.setOnClickListener(new View.OnClickListener() { //setting the onclicklistener
@Override
public void onClick(View v) throws NullPointerException {
Toast.makeText(context, itemTextView.getText().toString(), Toast.LENGTH_SHORT).show(); //Gives nullpointer error
}
});
} catch (NullPointerException e){
}
break;
但是每当我在 recyclerview.Any 帮助中单击子项时,我都会收到 NullPointerException,将不胜感激。谢谢。
修改适配器代码
public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//-----add
interface OnItemClickListener{
void OnItemClick(int position);
}
OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
//------
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final Item item = data.get(position);
switch (item.type) {
case HEADER:
final ListHeaderViewHolder itemController = (ListHeaderViewHolder) holder;
itemController.refferalItem = item;
itemController.header_title.setText(item.text);
if (item.invisibleChildren == null) {
itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
} else {
itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
}
itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (item.invisibleChildren == null) {
item.invisibleChildren = new ArrayList<Item>();
int count = 0;
int pos = data.indexOf(itemController.refferalItem);
while (data.size() > pos + 1 && data.get(pos + 1).type == CHILD) {
item.invisibleChildren.add(data.remove(pos + 1));
count++;
}
notifyItemRangeRemoved(pos + 1, count);
itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
} else {
int pos = data.indexOf(itemController.refferalItem);
int index = pos + 1;
for (Item i : item.invisibleChildren) {
data.add(index, i);
index++;
}
notifyItemRangeInserted(pos + 1, index - pos - 1);
itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
item.invisibleChildren = null;
}
}
});
break;
case CHILD:
TextView itemTextView = (TextView) holder.itemView;
itemTextView.setText(data.get(position).text);
//-----add
itemTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onItemClickListener!=null){
onItemClickListener.OnItemClick(position);
}
}
});
//------
break;
}
}
使用喜欢
ExpandableListAdapter adapter = new ExpandableListAdapter(data);
recyclerview.setAdapter(adapter);
adapter.setOnItemClickListener(new ExpandableListAdapter.OnItemClickListener() {
@Override
public void OnItemClick(int position) {
Toast.makeText(MainActivity.this, data.get(position).text,Toast.LENGTH_SHORT).show();
}
});
只需转到 GenreAdapter 中的 onBindChildViewHolder 并添加:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mactive,artist.getName(),Toast.LENGTH_SHORT).show();
}
});
顺便说一下,mactive 是构造函数
传入的activity