使用 RecyclerView 显示联系人列表

Posted

技术标签:

【中文标题】使用 RecyclerView 显示联系人列表【英文标题】:Showing Contactlist using RecyclerView 【发布时间】:2015-09-27 09:45:32 【问题描述】:

我正在使用下面的代码在列表视图中显示来自手机的联系人,这工作正常。

public class ContactList extends ListActivity 
    ListView contactlist;
    Cursor cursor1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_list);

        cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor1);

        String [] from = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID;
        int [] to =  android.R.id.text1,android.R.id.text2;

        SimpleCursorAdapter listadAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, cursor1, from, to);
        setListAdapter(listadAdapter);
        contactlist = getListView();
        contactlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        contactlist.setItemsCanFocus(false);
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contact_list, menu);
        return true;
    

    @Override
    public long getSelectedItemId() 
        // TODO Auto-generated method stub
        return super.getSelectedItemId();
    

    @Override
    public int getSelectedItemPosition() 
        // TODO Auto-generated method stub
        return super.getSelectedItemPosition();
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) 
            return true;
        
        return super.onOptionsItemSelected(item);
    

现在我想使用 recyclerview 显示它,直到现在我正在使用下面的代码向我的 recyclerview 添加一些字符串

public class MainActivity extends ActionBarActivity 
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private ArrayList<String> myDataset ;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        myDataset = new ArrayList<String>();
        myDataset.add("A");
        myDataset.add("B");
        myDataset.add("C");
        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    ...

这显示我如下

任何人都可以指导,推荐和真正的方式来完成这项任务。

thnx

【问题讨论】:

使用与Cursor 配合使用的RecyclerView.Adapter,请问谷歌叔叔 @pskink 你能给我一些链接到这个或任何这样的问题吗 你问谷歌大叔了吗? 【参考方案1】:

编辑 1: 遵循这些步骤: 1. 创建活动布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="$relativePackage.$activityClass" >

<!-- A RecyclerView with some commonly used attributes -->

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_
    android:layout_
    android:scrollbars="vertical" />

    为每个项目视图创建布局

    <ImageView
        android:id="@+id/icon"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher" />
    
    <TextView
        android:id="@+id/secondLine"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Description"
        android:textSize="12sp" />
    
    <TextView
        android:id="@+id/firstLine"
        android:layout_
        android:layout_
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />
    

    像其他适配器一样创建适配器

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>          
    
        private ArrayList<String> mDataset;
        // Provide a reference to the views for each data item   
        // Complex data items may need more than one view per item, and you provide 
        //access to all the views for a data item in a view holder   
        public class ViewHolder extends RecyclerView.ViewHolder 
        // each data item is just a string in this case
        public TextView txtHeader;
        public TextView txtFooter;
    
        public ViewHolder(View v) 
            super(v);
            txtHeader = (TextView) v.findViewById(R.id.firstLine);
            txtFooter = (TextView) v.findViewById(R.id.secondLine);
           
    
        public void add(int position, String item) 
            mDataset.add(position, item);
            notifyItemInserted(position);   
        
    
        public void remove(String item) 
            int position = mDataset.indexOf(item);
            mDataset.remove(position);
            notifyItemRemoved(position);   
        
        // Provide a suitable constructor (depends on the kind of dataset)  
        public MyAdapter(ArrayList<String> myDataset) 
            mDataset = myDataset;   
        
    
        // Create new views (invoked by the layout manager)   
        @Override  
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) 
            // create a new view
            View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout,parent, false);
            // set the view's size, margins, paddings and layout parameters
            ViewHolder vh = new ViewHolder(v);
            return vh;   
        
    
        // Replace the contents of a view (invoked by the layout manager)  
        @Override   
        public void onBindViewHolder(ViewHolder holder, int position) 
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            final String name = mDataset.get(position);
            holder.txtHeader.setText(mDataset.get(position));
            holder.txtHeader.setOnClickListener(new OnClickListener() 
                @Override
                public void onClick(View v) 
                        remove(name);
                    
                );
    
                holder.txtFooter.setText("Footer: " + mDataset.get(position)); 
        
    
        // Return the size of your dataset (invoked by the layout manager)  
        @Override   
        public int getItemCount() 
            return mDataset.size();   
        
    
    

    创建活动以将它们链接在一起

    公共类 MyActivity 扩展 Activity 私有 RecyclerView mRecyclerView; 私有 RecyclerView.Adapter mAdapter; 私有 RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);
    
        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
    
        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    
    

见this tutorial。这就是你所需要的!

【讨论】:

以上是关于使用 RecyclerView 显示联系人列表的主要内容,如果未能解决你的问题,请参考以下文章

Android教程2020 - RecyclerView使用入门

即时聊天APP - 联系人和会话

实战 | 认识 RecyclerView

如何在 Recyclerview 中显示图像 - Recyclerview 图像显示错误

使用 RecyclerView 在屏幕上显示我的列表的特定数量

如何制作一个按字母滚动的列表?