Recyclerview内にこれらのアイテムを表示したいのですが、まったく表示されず、エラーが表示されません。たぶんあなたたちは私を助けることができます。
MainActivity.Java
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rec);
List<MenuData> list = new ArrayList<>();
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
MenuRecAdapter menuRecAdapter = new MenuRecAdapter(list);
recyclerView.setAdapter(menuRecAdapter);
RecyclerViewアダプター:
public class MenuRecAdapter extends RecyclerView.Adapter<RecViewHolder>{
private List<MenuData> mList;
Activity context;
public MenuRecAdapter(List<MenuData> mList){
this.mList = mList;
}
public int getItemCount(){
return mList.size();
}
public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
RecViewHolder pvh = new RecViewHolder(v);
return pvh;
}
public void onBindViewHolder(RecViewHolder holder, int i){
holder.menuTeXT.setText(mList.get(i).text);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
アイテムのViewHolder:
public class RecViewHolder extends RecyclerView.ViewHolder {
public TextView menuTeXT;
public RecViewHolder(View itemView){
super(itemView);
menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT);
}
}
そして、recyclerviewに入れたいデータ(表示されないもの):
class MenuData {
String text;
MenuData(String text){
this.text = text;
}
private List<MenuData> list;
private void initializeData(){
list = new ArrayList<>();
list.add(new MenuData("Featured"));
list.add(new MenuData("Categories"));
list.add(new MenuData("Sell"));
list.add(new MenuData("Settings"));
list.add(new MenuData("Logout"));
}
}
前もって感謝します
MainActivityで
ArrayList<String> list = new ArrayList<>();
list.add("something1");
list.add("something2");
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rec);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
MenuRecAdapter menuRecAdapter = new MenuRecAdapter(list);
recyclerView.setAdapter(menuRecAdapter);
RecyclerViewアダプター
public class MenuRecAdapter extends RecyclerView.Adapter<RecViewHolder>{
private ArrayList<String> mList = new ArrayList<>();
Activity context;
public MenuRecAdapter(ArrayList<String> mList){
this.mList = mList;
}
public int getItemCount(){
return mList.size();
}
public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
RecViewHolder pvh = new RecViewHolder(v);
return pvh;
}
public void onBindViewHolder(RecViewHolder holder, int i){
holder.menuTeXT.setText(mList.get(i));
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
そして、ViewHolderは同じままです...
public class RecViewHolder extends RecyclerView.ViewHolder {
public TextView menuTeXT;
public RecViewHolder(View itemView){
super(itemView);
menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT);
}
}
menuDataクラスも削除します。上記のコードは正常に動作するはずです。
データを取得していないので、リストをアダプタに渡すときに問題が発生すると思います。リストのコピーを作成していないので、リストの参照を直接アダプターに渡しているようです。 MenuRecAdapter menuRecAdapter = new MenuRecAdapter(new Arraylist(list));
を試すことをお勧めします