在RecyclerView中使用图标显示字符串的最简单方法

时间:2015-07-22 19:27:12

标签: android listview android-recyclerview

在<{1}}中显示字符串和图标列表的最简单的方法是什么?

对于RecyclerView,我使用以下代码(此处为a full project at GitHub,无需单独的适配器:

app screenshot

ListView

我想知道如何以同样简单的方式将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(); } }); ArrayAdapterRecyclerView一起使用,即没有自定义布局和适配器。

2 个答案:

答案 0 :(得分:24)

Buddy这是最简单的方式

  1. 首先在 build.gradle 文件

    中添加依赖项
    compile 'com.android.support:recyclerview-v7:21.0.+'
    
  2. 在MainActivity中定义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 LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
    
        adapter = new PlanetAdapter(planetList,getApplicationContext());
        recyclerView.setAdapter(adapter);
    }
    
  3. 主要的Buzz杀手 PlanetAdapter ,您必须定义PlanetAdapter,它扩展了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. 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. 最后但并非最不重要的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>
      

答案 1 :(得分:10)

我已经在MainActivity.java中用几行简化了L-X的解决方案(谢谢!):

首先是一个带有点击监听器的ViewHolder类:

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

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

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

然后适配器覆盖3个抽象方法并使用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;
        }
    });

上面的代码将字符串mPlanets的数组显示为:

app screenshot