RecyclerView,从自定义的Recycler View适配器继承

时间:2018-11-09 17:21:44

标签: java android

我有这两个类似的行为回收器视图适配器,它们之间的唯一区别是onclick方法和传递给它们的对象。因此,我正在考虑设计要从回收器视图适配器继承的类B,这样我就可以更改构造函数以执行我想要的操作,而不是创建两个非常相似的类。有人可以给我一些提示和见解如何实现这个想法吗?

    public class GroupRecycleAdapter extends RecyclerView.Adapter<GroupRecycleAdapter.GroupViewHolder>{
private List<Group> groupList;
private Context context;

public class GroupViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView showingTextView, nameTextview;
    ImageView icon;

    public GroupViewHolder(View itemView){
        super(itemView);

        showingTextView = itemView.findViewById(R.id.group_children_edittext);
        nameTextview = itemView.findViewById(R.id.postMemberName);
        icon = itemView.findViewById(R.id.group_children_image);

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClicked " + getAdapterPosition());
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(R.layout.member_input_popup);
        builder.setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.setNegativeButton("CONFIRM", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

public GroupRecycleAdapter(List<Group> groupList, Context context){
    this.groupList = groupList;
    this.context = context;
}
public GroupRecycleAdapter(Context context){
    this.context = context;
}

@NonNull
@Override
public GroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.member_input_name_layout,parent,false);

    return new GroupViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull GroupViewHolder holder, int position) {
    String member = groupList.get(position).name;
    holder.showingTextView.setVisibility(View.GONE);
    holder.icon.setVisibility(View.GONE);
    holder.nameTextview.setText(member);
}

@Override
public int getItemCount() {
    return groupList.size();
}

public void setGroup(List<Group> group) {
    groupList = group;
    notifyDataSetChanged();
}
}

1 个答案:

答案 0 :(得分:1)

您可以为GroupRecycleAdapter创建一个接口,并在使用此适配器的片段/活动中实现该接口。您可以通过适配器的构造函数传递该接口的实现。

适配器样本

public class GroupRecycleAdapter extends RecyclerView.Adapter<GroupRecycleAdapter.GroupViewHolder>{
  @NonNull
  private Link link;

  public GroupRecyclerAdapter(@NonNull Link link){
    this.link = link;
  }


  public class GroupViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView showingTextView, nameTextview;
    ImageView icon;

    public GroupViewHolder(View itemView){
        super(itemView);

        showingTextView = itemView.findViewById(R.id.group_children_edittext);
        nameTextview = itemView.findViewById(R.id.postMemberName);
        icon = itemView.findViewById(R.id.group_children_image);

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClicked " + getAdapterPosition());
        Group group = groupList.get(getLayoutPosition());
        link.onItemClicked(v,group);
        //Shift this code to fragment/activity where Link interface is implemented
    }
  }


  public interface Link{
    void onItemClicked(View v,Group group);
  }
}

在片段/活动中

您可以使对象实现如下所示的Link接口,也可以使片段/活动实现Link接口

GroupRecycleAdapter.Link link = new GroupRecycleAdapter.Link(){
  public void onItemClicked(View v,Group group){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setView(R.layout.member_input_popup);
            builder.setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.setNegativeButton("CONFIRM", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    AlertDialog dialog = builder.create();
    dialog.show();
  }
} 

根据需要在上述onItemClicked函数中更改代码


适配器构造器

GroupRecyclerAdapter adapter = new GroupRecyclerAdapter(link);