studio recyclerview personalizado listas example ejemplo dinamicas cardview android android-fragments android-adapter android-recyclerview android-cardview

personalizado - recyclerview android studio ejemplo



"RecyclerView: sin adaptador conectado; omitiendo el diseƱo "para recyclerview en fragmento (3)

Esta pregunta ya tiene una respuesta aquí:

Recibo este error, pero no sé qué lo está causando ... quizás algo tiene que ver con el hecho de que esto se está inicializando en un fragmento y no en la actividad en sí. Editar: código StatsAdapter incluido

public class StatsFragment extends Fragment { private RecyclerView mRecyclerView; private LinearLayoutManager mLinearLayoutManager; private RecyclerView.Adapter mAdapter; private String[] myDataset = new String[]{"hello", "world", "yolo"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_stats, parent, false); mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList); mRecyclerView.setHasFixedSize(true); mAdapter = new StatsAdapter(myDataset); mRecyclerView.setAdapter(mAdapter); mLinearLayoutManager = new LinearLayoutManager(getActivity()); mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLinearLayoutManager); return v; } } public class StatsAdapter extends RecyclerView.Adapter<StatsAdapter.ViewHolder> { private String[] mDataset; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(View v) { super(v); mTextView = (TextView) v.findViewById(R.id.title); } } public StatsAdapter(String[] myDataset) { mDataset = myDataset; } @Override public StatsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_stats, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } @Override public int getItemCount() { return mDataset.length; } }


Cree el adaptador y los objetos relacionados "sin vista" en onCreate . Luego use su adaptador para RecyclerView .

Además, limpie su código iniciando los objetos en un orden claro en función de su uso y el ciclo de vida de la Activity .

public class StatsFragment extends Fragment { private RecyclerView mRecyclerView; private LinearLayoutManager mLinearLayoutManager; private StatsAdapter mAdapter; // was RecyclerView.Adapter mAdapter; private String[] myDataset = new String[]{"hello", "world", "yolo"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 1. mAdapter = new StatsAdapter(myDataset); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_stats, parent, false); // 2. mLinearLayoutManager = new LinearLayoutManager(getActivity()); mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); // 3. mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(mLinearLayoutManager); mRecyclerView.setAdapter(mAdapter); return v; } }


Mueva su línea setLayoutManager antes de setAdapter


Una forma de solucionar este problema es adjuntar un adaptador vacío a RecyclerView

void initializeRecyclerView() { recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); recyclerView.setAdapter(new SampleAdapter(getCurrentActivity())); recyclerView.setLayoutManager(new LinearLayoutManager(getCurrentActivity())); recyclerView.setHasFixedSize(true); }