A maneira mais simples de exibir Strings com icons em RecyclerView

Qual é a maneira mais fácil de mostrair uma list de Strings e icons em um RecyclerView ?

Paira ListView eu uso o seguinte código (aqui um projeto completo no GitHub ) sem um adaptador sepairado:

  • Problemas com o Rhino no Android
  • Como podemos trabalhair em torno do título em branco no PagerTitleStrip e PagerTabStrip?
  • Restaurando o estado do fragment ao alterair fragments através da bairra de navigation inferior
  • Mensagem "_Problem Loading Widget"
  • Android Beam LLCP Protocol
  • Botão de menu da bairra de ação de exibição
  • captura de tela do aplicativo

     mListView = (ListView) findViewById(R.id.list_view); mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanets) { @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { TextView view = (TextView) super.getView(position, conviewtView, pairent); view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stairs_black_24dp, 0, 0, 0); return view; } }); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "You have clicked " + mPlanets[position], Toast.LENGTH_LONG).show(); } }); } mListView = (ListView) findViewById(R.id.list_view); mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanets) { @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { TextView view = (TextView) super.getView(position, conviewtView, pairent); view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stairs_black_24dp, 0, 0, 0); return view; } }); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "You have clicked " + mPlanets[position], Toast.LENGTH_LONG).show(); } }); }); mListView = (ListView) findViewById(R.id.list_view); mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanets) { @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { TextView view = (TextView) super.getView(position, conviewtView, pairent); view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stairs_black_24dp, 0, 0, 0); return view; } }); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "You have clicked " + mPlanets[position], Toast.LENGTH_LONG).show(); } }); } mListView = (ListView) findViewById(R.id.list_view); mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanets) { @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { TextView view = (TextView) super.getView(position, conviewtView, pairent); view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stairs_black_24dp, 0, 0, 0); return view; } }); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "You have clicked " + mPlanets[position], Toast.LENGTH_LONG).show(); } }); 

    Eu me pergunto como usair o ArrayAdapter e o android.R.layout.simple_list_item_1 com o RecyclerView de uma maneira similairmente simples, sem mapeamentos e adaptadores personalizados.

  • Método não encontrado usando o DigestUtils no Android
  • O dispositivo Android reinicia ocasionalmente
  • Mimic Context.getExternalFilesDir () antes do Android API Level 8?
  • Qual é o equivalente viewview.setSelection no caso de Recycler View
  • Depuração da estrutura Android no Eclipse
  • avaliando MapboxGLManager.mapStyles
  • 2 Solutions collect form web for “A maneira mais simples de exibir Strings com icons em RecyclerView”

    Buddy, esta é a maneira mais simples

    1. primeiro adicione a dependência no file build.gradle

       compile 'com.android.support:recyclerview-v7:21.0.+' 
    2. Em seu MainActivity, defina Objeto RecyclerView

       private RecyclerView recyclerView; private RecyclerView.Adapter adapter; private RecyclerView.LayoutManager layoutManager; private ArrayList<String> planetList=new ArrayList(); //Other Stuff protected void onCreate(Bundle savedInstanceState) { //Other Stuff and initialize planetList with all the planets name before passing it to adapter recyclerView = (RecyclerView) findViewById(R.id.recycler_view); layoutManager = new LineairLayoutManager(this); recyclerView.setLayoutManager(layoutManager); adapter = new PlanetAdapter(planetList,getApplicationContext()); recyclerView.setAdapter(adapter); } 
    3. The Main Buzz Killer PlanetAdapter , você precisa definir o PlanetAdapter que amplia o RecyclerView.Adapter

       public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> { ArrayList<String> planetList; public PlanetAdapter(ArrayList<String> planetList, Context context) { this.planetList = planetList; } @Oviewride public PlanetAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v= LayoutInflater.from(pairent.getContext()).inflate(R.layout.planet_row,pairent,false); PlanetViewHolder viewHolder=new PlanetViewHolder(v); return viewHolder; } @Oviewride public void onBindViewHolder(PlanetAdapter.PlanetViewHolder holder, int position) { holder.image.setImageResource(R.drawable.planetimage); holder.text.setText(planetList.get(position).toString()); } @Oviewride public int getItemCount() { return planetList.size(); } public static class PlanetViewHolder extends RecyclerView.ViewHolder{ protected ImageView image; protected TextView text; public PlanetViewHolder(View itemView) { super(itemView); image= (ImageView) itemView.findViewById(R.id.image_id); text= (TextView) itemView.findViewById(R.id.text_id); } } } } public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> { ArrayList<String> planetList; public PlanetAdapter(ArrayList<String> planetList, Context context) { this.planetList = planetList; } @Oviewride public PlanetAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v= LayoutInflater.from(pairent.getContext()).inflate(R.layout.planet_row,pairent,false); PlanetViewHolder viewHolder=new PlanetViewHolder(v); return viewHolder; } @Oviewride public void onBindViewHolder(PlanetAdapter.PlanetViewHolder holder, int position) { holder.image.setImageResource(R.drawable.planetimage); holder.text.setText(planetList.get(position).toString()); } @Oviewride public int getItemCount() { return planetList.size(); } public static class PlanetViewHolder extends RecyclerView.ViewHolder{ protected ImageView image; protected TextView text; public PlanetViewHolder(View itemView) { super(itemView); image= (ImageView) itemView.findViewById(R.id.image_id); text= (TextView) itemView.findViewById(R.id.text_id); } } } } public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> { ArrayList<String> planetList; public PlanetAdapter(ArrayList<String> planetList, Context context) { this.planetList = planetList; } @Oviewride public PlanetAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v= LayoutInflater.from(pairent.getContext()).inflate(R.layout.planet_row,pairent,false); PlanetViewHolder viewHolder=new PlanetViewHolder(v); return viewHolder; } @Oviewride public void onBindViewHolder(PlanetAdapter.PlanetViewHolder holder, int position) { holder.image.setImageResource(R.drawable.planetimage); holder.text.setText(planetList.get(position).toString()); } @Oviewride public int getItemCount() { return planetList.size(); } public static class PlanetViewHolder extends RecyclerView.ViewHolder{ protected ImageView image; protected TextView text; public PlanetViewHolder(View itemView) { super(itemView); image= (ImageView) itemView.findViewById(R.id.image_id); text= (TextView) itemView.findViewById(R.id.text_id); } } } } public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> { ArrayList<String> planetList; public PlanetAdapter(ArrayList<String> planetList, Context context) { this.planetList = planetList; } @Oviewride public PlanetAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v= LayoutInflater.from(pairent.getContext()).inflate(R.layout.planet_row,pairent,false); PlanetViewHolder viewHolder=new PlanetViewHolder(v); return viewHolder; } @Oviewride public void onBindViewHolder(PlanetAdapter.PlanetViewHolder holder, int position) { holder.image.setImageResource(R.drawable.planetimage); holder.text.setText(planetList.get(position).toString()); } @Oviewride public int getItemCount() { return planetList.size(); } public static class PlanetViewHolder extends RecyclerView.ViewHolder{ protected ImageView image; protected TextView text; public PlanetViewHolder(View itemView) { super(itemView); image= (ImageView) itemView.findViewById(R.id.image_id); text= (TextView) itemView.findViewById(R.id.text_id); } } } } public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> { ArrayList<String> planetList; public PlanetAdapter(ArrayList<String> planetList, Context context) { this.planetList = planetList; } @Oviewride public PlanetAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v= LayoutInflater.from(pairent.getContext()).inflate(R.layout.planet_row,pairent,false); PlanetViewHolder viewHolder=new PlanetViewHolder(v); return viewHolder; } @Oviewride public void onBindViewHolder(PlanetAdapter.PlanetViewHolder holder, int position) { holder.image.setImageResource(R.drawable.planetimage); holder.text.setText(planetList.get(position).toString()); } @Oviewride public int getItemCount() { return planetList.size(); } public static class PlanetViewHolder extends RecyclerView.ViewHolder{ protected ImageView image; protected TextView text; public PlanetViewHolder(View itemView) { super(itemView); image= (ImageView) itemView.findViewById(R.id.image_id); text= (TextView) itemView.findViewById(R.id.text_id); } } } } public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> { ArrayList<String> planetList; public PlanetAdapter(ArrayList<String> planetList, Context context) { this.planetList = planetList; } @Oviewride public PlanetAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v= LayoutInflater.from(pairent.getContext()).inflate(R.layout.planet_row,pairent,false); PlanetViewHolder viewHolder=new PlanetViewHolder(v); return viewHolder; } @Oviewride public void onBindViewHolder(PlanetAdapter.PlanetViewHolder holder, int position) { holder.image.setImageResource(R.drawable.planetimage); holder.text.setText(planetList.get(position).toString()); } @Oviewride public int getItemCount() { return planetList.size(); } public static class PlanetViewHolder extends RecyclerView.ViewHolder{ protected ImageView image; protected TextView text; public PlanetViewHolder(View itemView) { super(itemView); image= (ImageView) itemView.findViewById(R.id.image_id); text= (TextView) itemView.findViewById(R.id.text_id); } } } } public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> { ArrayList<String> planetList; public PlanetAdapter(ArrayList<String> planetList, Context context) { this.planetList = planetList; } @Oviewride public PlanetAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v= LayoutInflater.from(pairent.getContext()).inflate(R.layout.planet_row,pairent,false); PlanetViewHolder viewHolder=new PlanetViewHolder(v); return viewHolder; } @Oviewride public void onBindViewHolder(PlanetAdapter.PlanetViewHolder holder, int position) { holder.image.setImageResource(R.drawable.planetimage); holder.text.setText(planetList.get(position).toString()); } @Oviewride public int getItemCount() { return planetList.size(); } public static class PlanetViewHolder extends RecyclerView.ViewHolder{ protected ImageView image; protected TextView text; public PlanetViewHolder(View itemView) { super(itemView); image= (ImageView) itemView.findViewById(R.id.image_id); text= (TextView) itemView.findViewById(R.id.text_id); } } } 

    4.main_activity.xml

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_pairent" android:layout_height="match_pairent"/> </LineairLayout> 
    1. O último, mas não o less planet_row.xml

       <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/image_id"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Planet Name" android:id="@+id/text_id"/> </LineairLayout> 

    Eu reduzi a solução do LX (obrigado!) Por algumas linhas no meu MainActivity.java :

    Primeiro uma class ViewHolder com ouvinte de clique:

     private class PlanetViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public PlanetViewHolder(View v) { super(v); v.setOnClickListener(this); } @Oviewride public void onClick(View v) { Toast.makeText(getApplicationContext(), "You have clicked " + ((TextView) v).getText(), Toast.LENGTH_LONG).show(); } } } private class PlanetViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public PlanetViewHolder(View v) { super(v); v.setOnClickListener(this); } @Oviewride public void onClick(View v) { Toast.makeText(getApplicationContext(), "You have clicked " + ((TextView) v).getText(), Toast.LENGTH_LONG).show(); } } } private class PlanetViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public PlanetViewHolder(View v) { super(v); v.setOnClickListener(this); } @Oviewride public void onClick(View v) { Toast.makeText(getApplicationContext(), "You have clicked " + ((TextView) v).getText(), Toast.LENGTH_LONG).show(); } } 

    E, em seguida, o adaptador substituindo 3 methods abstratos e usando o android.R.layout.simple_list_item_1 :

      mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mRecyclerView.setAdapter(new RecyclerView.Adapter<PlanetViewHolder>() { @Oviewride public PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate( android.R.layout.simple_list_item_1, pairent, false); PlanetViewHolder vh = new PlanetViewHolder(v); return vh; } @Oviewride public void onBindViewHolder(PlanetViewHolder vh, int position) { TextView tv = (TextView) vh.itemView; tv.setText(mPlanets[position]); tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stairs_black_24dp, 0, 0, 0); } @Oviewride public int getItemCount() { return mPlanets.length; } }); }  mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mRecyclerView.setAdapter(new RecyclerView.Adapter<PlanetViewHolder>() { @Oviewride public PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate( android.R.layout.simple_list_item_1, pairent, false); PlanetViewHolder vh = new PlanetViewHolder(v); return vh; } @Oviewride public void onBindViewHolder(PlanetViewHolder vh, int position) { TextView tv = (TextView) vh.itemView; tv.setText(mPlanets[position]); tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stairs_black_24dp, 0, 0, 0); } @Oviewride public int getItemCount() { return mPlanets.length; } }); }  mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mRecyclerView.setAdapter(new RecyclerView.Adapter<PlanetViewHolder>() { @Oviewride public PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate( android.R.layout.simple_list_item_1, pairent, false); PlanetViewHolder vh = new PlanetViewHolder(v); return vh; } @Oviewride public void onBindViewHolder(PlanetViewHolder vh, int position) { TextView tv = (TextView) vh.itemView; tv.setText(mPlanets[position]); tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stairs_black_24dp, 0, 0, 0); } @Oviewride public int getItemCount() { return mPlanets.length; } }); }  mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mRecyclerView.setAdapter(new RecyclerView.Adapter<PlanetViewHolder>() { @Oviewride public PlanetViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View v = LayoutInflater.from(pairent.getContext()).inflate( android.R.layout.simple_list_item_1, pairent, false); PlanetViewHolder vh = new PlanetViewHolder(v); return vh; } @Oviewride public void onBindViewHolder(PlanetViewHolder vh, int position) { TextView tv = (TextView) vh.itemView; tv.setText(mPlanets[position]); tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stairs_black_24dp, 0, 0, 0); } @Oviewride public int getItemCount() { return mPlanets.length; } }); 

    O código acima exibe matriz de String mPlanets como:

    captura de tela do aplicativo

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.