我想在用户界面中创建动态RecyclerView

时间:2019-04-08 14:44:58

标签: java user-interface dynamic android-recyclerview

用户输入多少个列表。我给了他1-10个列表之间进行选择的选项。我想动态地创建它们。怎么做?? ,我已经为RecyclerView创建了adpter。是应该在adpter中还是在fragment中创建我创建的唯一的recyclerView?

public void initRecycerView(ArrayList<Player> list) {
       myadapter = new Myadapter(list);
       recyclerviewTeam1.setAdapter(myadapter);
       recyclerviewTeam1.setLayoutManager(new LinearLayoutManager(getContext()));
       recyclerviewTeam1.setHasFixedSize(true);
       myadapter.notifyDataSetChanged(); }

这是我的适应者课程

class Myadapter extends RecyclerView.Adapter<Myadapter.MyHolder> {

private ArrayList<Player> adpterList;
private OnItemClickListener mlistener;

public interface OnItemClickListener {
    void onItemClick(int position);
    void onDeleteClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
    this.mlistener = listener;
}

public Myadapter( ArrayList<Player> adpterList) {
    this.adpterList = adpterList;
}


public static class MyHolder extends RecyclerView.ViewHolder {

    public TextView textView;
    public ImageView image_delete;

    public MyHolder(@NonNull View view, OnItemClickListener listener) {
        super(view);
        textView = view.findViewById(R.id.textItemHolder);
        image_delete = view.findViewById(R.id.image_delete);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION){
                        listener.onItemClick(position);
                    }
                }

            }
        });
        image_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION){
                        listener.onDeleteClick(position);
                    }
                }
            }
        });
    }
}
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view =  LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_list, viewGroup, false);
    MyHolder holder = new MyHolder(view , mlistener);
    return holder;
}

public void onBindViewHolder(@NonNull MyHolder myHolder, int position) {

    Player player = adpterList.get(position);
    myHolder.textView.setText(" " + player.toString());
    myHolder.image_delete.setImageResource(R.drawable.ic_delete);
}

public int getItemCount() {
    return adpterList.size();
}

}

0 个答案:

没有答案