使用RecyclerView显示联系人列表

时间:2015-07-09 07:04:38

标签: android android-contacts android-recyclerview

我正在使用下面的代码在listview中显示来自手机的联系人,这很正常。

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);
    }...
}

显示如下 enter image description here

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

日Thnx

1 个答案:

答案 0 :(得分:0)

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

<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}" >

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

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

  1. 为每个项目视图创建布局

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        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_width="fill_parent"
        android:layout_height="26dip"
        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_width="fill_parent"
        android:layout_height="wrap_content"
        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" />
    

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

    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();   
        }
    }
    
  3. 创建活动以将它们全部链接在一起

    公共类MyActivity扩展了Activity {     private RecyclerView mRecyclerView;     private RecyclerView.Adapter mAdapter;     private 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);
    }
    

    }

  4. this tutorial。那就是你所需要的一切!