我有这个自定义列表适配器,由于某种原因我可以让txtTitle与侦听器一起工作,但按钮总是返回一个null对象。有没有一种特定的方法在recyclelerView中添加按钮?我实现它的方式与我实现其他回收器适配器的方式相同,只是它们没有按钮。
public class resource_list_adapter extends RecyclerView.Adapter<resource_list_adapter.ViewHolder> {
private List<resource_list> listItems;
private Context context;
resource_list_adapter.OnItemClickListener onItemClickListener;
public resource_list_adapter(List<resource_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public resource_list_adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.resource_list, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
resource_list listItem = listItems.get(position);
Double resource = listItem.getCurrent().doubleValue()/listItem.getMax().doubleValue();
holder.txtTitle.setText(listItem.getTxtTitle() + ": " + listItem.getCurrent().toString() + "/" + listItem.getMax().toString() + " (" + String.valueOf((int)(100*resource)) + "%)");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.pgResource.setProgressTintList(ColorStateList.valueOf(Color.rgb(listItem.getRed(),listItem.getGreen(),listItem.getBlue())));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
holder.pgResource.setProgress((int)(100*resource), true);
}
holder.btnAddResource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onResourceClick(position);
}
});
holder.btnSubtractResource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onResourceClick(position);
}
});
holder.btnAddResource.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onItemClickListener.onLongResourceClick(position);
return true;
}
});
holder.btnSubtractResource.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onItemClickListener.onLongResourceClick(position);
return true;
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtTitle;
public ProgressBar pgResource;
public Button btnAddResource;
public Button btnSubtractResource;
public ViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
btnAddResource = (Button) itemView.findViewById(R.id.btnAdditionResource);
btnSubtractResource = (Button) itemView.findViewById(R.id.btnSubtractResource);
pgResource = (ProgressBar) itemView.findViewById(R.id.pgResource);
}
}
public void setOnItemClickListener(resource_list_adapter.OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener{
void onResourceClick(int position);
void onLongResourceClick(int position);
}
}
这就是我在Activity上实现它的方法。
我在我的活动上实施implements resource_list_adapter.OnItemClickListener
。
声明变量:
private RecyclerView recyclerResource;
private resource_list_adapter resourceAdapter;
private List<resource_list> resourceList;
private List<String> resourceKeyList;
这就是我调用方法的方法 - 我从其他适配器调用所有其他方法的地方。
@Override
public void onResourceClick(int position) {
Toast.makeText(getContext(), "Test", Toast.LENGTH_SHORT).show();
/*Intent intent = new Intent(getActivity(), AttributeDescription.class);
intent.putExtra("SelectedCharacter", Home_Tab.activeCharacter);
intent.putExtra("name", attributeKeyList.get(position).toString());
intent.putExtra("category", "Attribute");
if (character.Edit.equals("True")){
intent.putExtra("mode", "Edit");
}
else{
intent.putExtra("mode", "View");
}
startActivity(intent);*/
}
@Override
public void onLongResourceClick(final int position) {
/*if (character.Edit.equals("True")){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Remove Attribute");
builder.setPositiveButton("Remove", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
character.removeAttribute(attributeKeyList.get(position).toString(), "Attribute");
attributeKeyList.remove(position);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
AlertDialog dialog = builder.create();
dialog.show();
}*/
}
这是我在init()方法中将侦听器设置为适配器的方式(与其余方法相同)。
resourceAdapter = new resource_list_adapter(resourceList, getContext());
resourceAdapter.setOnItemClickListener(this);
答案 0 :(得分:1)
您必须在不设置点击侦听器的情况下制作新的RecyclerViewAdapter。例如,您调用notifyDataSetChanged,当您获得新数据时,请不要创建一个全新的适配器。
如果你总是想强迫自己实现一个点击监听器(因为你没有空检查),那么在构造函数中显式传递它
final resource_list_adapter.OnItemClickListener onItemClickListener;
public resource_list_adapter(List<resource_list> listItems, Context context, resource_list_adapter.OnItemClickListener listener) {
this.listItems = listItems;
this.context = context;
onItemClickListener = listener;
}