ListView模拟微信好友功能_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > ListView模拟微信好友功能

ListView模拟微信好友功能

 2017/8/26 23:08:47  钻石VIP  程序员俱乐部  我要评论(0)
  • 摘要:ListView模拟微信好友功能效果图:分析:1、创建listView2、创建数据3、创建适配器将数据放到呈现数据的容器里面。将这个容器(带数据)连接适配器。4、ListView设置适配器代码:1packagefry;23importjava.util.ArrayList;4importjava.util.List;56importcom.example.weChatFriends.R;78importandroid.app.Activity;9importandroid.os.Bundle
  • 标签:功能 view list

ListView模拟微信好友功能

效果图:

 

分析:

1、创建listView

2、创建数据

3、创建适配器

  将数据放到呈现数据的容器里面。

  将这个容器(带数据)连接适配器。

4、ListView设置适配器

  

 

 

代码:

 

class="code_img_closed" src="/Upload/Images/2017082623/0015B68B3C38AA5B.gif" alt="">
 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 }
logs_code_collapse">主界面
 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>
用于存放数据的容器

 

发表评论
用户名: 匿名