1. 程式人生 > >ListView模擬微信好友功能

ListView模擬微信好友功能

blog 容器 gif rate example findview position emc hat

ListView模擬微信好友功能

效果圖:

技術分享技術分享

分析:

技術分享

1、創建listView

2、創建數據

3、創建適配器

  將數據放到呈現數據的容器裏面。

  將這個容器(帶數據)連接適配器。

4、ListView設置適配器

  

代碼:

技術分享
 1 package fry;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import com.example.weChatFriends.R;
 7 
 8 import android.app.Activity;
 9 import android.os.Bundle;
10 import android.util.Log; 11 import android.view.View; 12 import android.widget.AdapterView; 13 import android.widget.AdapterView.OnItemClickListener; 14 import android.widget.AdapterView.OnItemSelectedListener; 15 import android.widget.ArrayAdapter; 16 import android.widget.ListView; 17 import android.widget.RelativeLayout;
18 import android.widget.Toast; 19 20 public class Activity01 extends Activity implements OnItemSelectedListener,OnItemClickListener{ 21 private FriendModel friend; 22 private ListView listView; 23 private List<FriendModel> list; 24 private weChatListAdapter adapter; 25 //存資源圖片ID
26 private int[] imageID=new int[]{R.drawable.image1,R.drawable.image2, 27 R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6, 28 R.drawable.image7,R.drawable.image8,R.drawable.image9,R.drawable.image10, 29 R.drawable.image11}; 30 //存昵稱 31 private String[] nickName=new String[]{"張三","吳京","戰狼","神煩xp","木魚" 32 ,"水心","系大大","電影","血怒","創奇","講故事" 33 }; 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 // TODO Auto-generated method stub 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.activity01); 39 init(); 40 setData(); 41 42 43 } 44 private void setData() { 45 //這裏要是寫成for(int i:imageID),那麽i就是資源id,例如2130837505 46 for(int i=0;i<imageID.length;i++){ 47 FriendModel friend1=new FriendModel(); 48 //System.out.println(i); 49 friend1.setImageNum(imageID[i]); 50 friend1.setNickName(nickName[i]); 51 friend1.setSignature("我要做比海賊王還強大的人"); 52 list.add(friend1); 53 } 54 adapter=new weChatListAdapter(list, this); 55 listView.setAdapter(adapter); 56 57 } 58 private void init() { 59 listView=(ListView) findViewById(R.id.listView); 60 listView.setOnItemSelectedListener(this); 61 listView.setOnItemClickListener(this); 62 friend=new FriendModel(); 63 list=new ArrayList<FriendModel>(); 64 65 66 } 67 /* 68 * Callback method to be invoked when an item in this view has been selected. This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.(non-Javadoc) 69 * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView, android.view.View, int, long) 70 */ 71 @Override 72 public void onItemSelected(AdapterView<?> parent, View view, int position, 73 long id) { 74 75 } 76 @Override 77 public void onNothingSelected(AdapterView<?> parent) { 78 // TODO Auto-generated method stub 79 80 } 81 @Override 82 public void onItemClick(AdapterView<?> parent, View view, int position, 83 long id) { 84 FriendModel friendItem=(FriendModel) parent.getItemAtPosition(position); 85 String s=friendItem.getNickName(); 86 Log.d("onItemClick","s"); 87 Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); 88 89 } 90 91 92 93 }
主界面 技術分享
 1 package fry;
 2 
 3 import java.util.List;
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 import com.example.weChatFriends.R;
14 
15 import android.content.Context;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.widget.BaseAdapter;
19 import android.widget.ImageView;
20 import android.widget.TextView;
21 
22 public class weChatListAdapter extends BaseAdapter{
23     private List<FriendModel> myData;
24     private Context mContext;
25     private ImageView avator;
26     private TextView nickName1;
27     private TextView signature1;
28     private FriendModel friend;
29     
30     
31     public weChatListAdapter(List<FriendModel> data, Context mContext) {
32         super();
33         this.myData = data;
34         this.mContext = mContext;
35     }
36     
37     //How many items are in the data set represented by this Adapter.
38     @Override
39     public int getCount() {
40         // TODO Auto-generated method stub
41         return this.myData.size();
42     }
43     
44     //Get the data item associated with the specified position in the data set.
45     @Override
46     public Object getItem(int position) {
47         // TODO Auto-generated method stub
48         return this.myData.get(position);
49     }
50     
51     //Get the row id associated with the specified position in the list.
52     @Override
53     public long getItemId(int position) {
54         // TODO Auto-generated method stub
55         return position;
56     }
57     
58     //Get a View that displays the data at the specified position in the data set. 
59     @Override
60     public View getView(int position, View convertView, ViewGroup parent) {
61         // TODO Auto-generated method stub
62         View view=View.inflate(mContext, com.example.weChatFriends.R.layout.item_friend, null);
63         //System.out.println(position);
64         friend=myData.get(position);
65         int ImageID=friend.getImageNum();
66         String nickName=friend.getNickName();
67         String signature=friend.getSignature();
68         avator=(ImageView) view.findViewById(R.id.iv_avator);
69         nickName1=(TextView)view.findViewById(R.id.tv_nickname);
70         signature1=(TextView)view.findViewById(R.id.tv_signature);
71         avator.setImageResource(ImageID);
72         nickName1.setText(nickName);
73         signature1.setText(signature);
74         return view;
75     }
76 
77 }
自己創建的適配器 技術分享
 1 package fry;
 2 
 3 public class FriendModel {
 4     //頭像的圖片id
 5     private int imageNum;
 6     //昵稱
 7     private String nickName;
 8     //個性簽名
 9     private String signature;
10     
11     
12     
13     public int getImageNum() {
14         return imageNum;
15     }
16     public void setImageNum(int imageNum) {
17         this.imageNum = imageNum;
18     }
19     public String getNickName() {
20         return this.nickName;
21     }
22     public void setNickName(String nickName) {
23         this.nickName = nickName;
24     }
25     public String getSignature() {
26         return signature;
27     }
28     public void setSignature(String signature) {
29         this.signature = signature;
30     }
31     
32     
33 }
列表中聯系人數據的封裝 技術分享
1 <?xml version="1.0" encoding="utf-8"?>
2 <ListView xmlns:android="http://schemas.android.com/apk/res/android"
3     android:id="@+id/listView"
4     android:layout_width="match_parent"
5     android:layout_height="wrap_content" >
6 
7 </ListView>
ListView 技術分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content" >
 5     
 6     <ImageView 
 7         android:id="@+id/iv_avator"
 8         android:layout_width="70dp"
 9         android:layout_height="70dp"
10         android:src="@drawable/image1"
11         />
12     <TextView 
13         android:id="@+id/tv_nickname"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_toRightOf="@+id/iv_avator"
17         android:layout_centerVertical="true"
18         android:layout_marginLeft="20dp"
19         android:text="張三"
20         />
21     <TextView 
22         android:id="@+id/tv_signature"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:layout_centerVertical="true"
26         android:layout_alignParentRight="true"
27         android:text="我要做比海賊王更強大的男人"
28         />
29 
30 </RelativeLayout>
用於存放數據的容器

ListView模擬微信好友功能