Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The simplest way to display Strings with icons in a RecyclerView

What is please the easiest way to show a list of Strings and icons in a RecyclerView?

For ListView I use the following code (here a full project at GitHub) without a separate Adapter:

app screenshot

mListView = (ListView) findViewById(R.id.list_view);

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

I wonder how to use ArrayAdapter and android.R.layout.simple_list_item_1 with RecyclerView in a similarly simple way, that is without custom layouts and adapters.

like image 292
Alexander Farber Avatar asked Jul 22 '15 19:07

Alexander Farber


People also ask

How do you load all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.

What is ViewType?

ViewType is defined by you. Its basically an enumeration. If all of your items have the same view, you can just ignore it and return 1 for getItemViewType. If you have different views for different items, you just have to return a unique value for each view.

What is LayoutManager in RecyclerView?

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.


2 Answers

Buddy this is the most simplest way

  1. first add the dependency in build.gradle file

    compile 'com.android.support:recyclerview-v7:21.0.+'
    
  2. In your MainActivity define RecyclerView Object

    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 LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
    
        adapter = new PlanetAdapter(planetList,getApplicationContext());
        recyclerView.setAdapter(adapter);
    }
    
  3. The Main Buzz Killer PlanetAdapter,You have to define PlanetAdapter which extends RecyclerView.Adapter

    public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.PlanetViewHolder> {
    
        ArrayList<String> planetList;
    
        public PlanetAdapter(ArrayList<String> planetList, Context context) {
            this.planetList = planetList;
        }
    
        @Override
        public PlanetAdapter.PlanetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.planet_row,parent,false);
            PlanetViewHolder viewHolder=new PlanetViewHolder(v);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(PlanetAdapter.PlanetViewHolder holder, int position) {
            holder.image.setImageResource(R.drawable.planetimage);
            holder.text.setText(planetList.get(position).toString());
        }
    
        @Override
        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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <android.support.v7.widget.RecyclerView
           android:id="@+id/recycler_view"
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>
</LinearLayout>
  1. The last But not the least planet_row.xml

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         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"/>
    </LinearLayout>
    
like image 141
Sanjeev Avatar answered Sep 20 '22 11:09

Sanjeev


I have reduced L-X's solution (thanks!) by few lines in my MainActivity.java:

First a ViewHolder class with click listener:

private class PlanetViewHolder
        extends RecyclerView.ViewHolder
        implements View.OnClickListener {

    public PlanetViewHolder(View v) {
        super(v);
        v.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // also possible: v.getContext()
        Toast.makeText(getApplicationContext(),
                "You have clicked " + ((TextView) v).getText(),
                Toast.LENGTH_LONG).show();
    }
}

And then the adapter overriding 3 abstract methods and using android.R.layout.simple_list_item_1:

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setAdapter(new RecyclerView.Adapter<PlanetViewHolder>() {

        @Override
        public PlanetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    android.R.layout.simple_list_item_1,
                    parent,
                    false);
            PlanetViewHolder vh = new PlanetViewHolder(v);
            return vh;
        }

        @Override
        public void onBindViewHolder(PlanetViewHolder vh, int position) {
            TextView tv = (TextView) vh.itemView;
            tv.setText(mPlanets[position]);
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stars_black_24dp, 0, 0, 0);
        }

        @Override
        public int getItemCount() {
            return mPlanets.length;
        }
    });

The above code displays array of String mPlanets as:

app screenshot

like image 30
Alexander Farber Avatar answered Sep 17 '22 11:09

Alexander Farber