转:
Android开发——ListView的简单使用
一、自定义一个Friend类,用于对应ListView的每一基本数据项
Friend.java
public class Friend {
private String name; //名称
private int icon; //头像
private String description; // 描述
//初始花化数据
public Friend(String n, int ic, String des){
name = n;
icon = ic;
description = des;
}
//获取数据的三个函数
public String getName() {
return name;
}
public int getIcon() {
return icon;
}
public String getDescription() {
return description;
}
}
二、自定义一个布局文件来存储每一项数据的UI
item_friend_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:id="@+id/item_im_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/head_1"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="@+id/item_tv_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="#000"
android:text="张三"
android:textSize="25sp"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/item_tv_des"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="我是张三"
android:textSize="20sp"
android:gravity="center_vertical"/>
LinearLayout>
LinearLayout>
效果显示
三、自定义一个Adapter, 继承自BaseAdapter,并绑定item_friend_layout.xml布局
FreiendAdapter.java
public class FriendAdapter extends BaseAdapter {
private List<Friend> data; //friend数据
private Context context;
private LayoutInflater layoutInflater;
//初始化数据,并获取主窗口的context,获取friend数据,之后获取context的布局服务
public FriendAdapter(Context context, List<Friend> data) {
this.data = data;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
//获取friend的数目
@Override
public int getCount() {
return data.size();
}
//获取第i个friend的数据
@Override
public Friend getItem(int i) {
return data.get(i);
}
//获取第i个friend的id
@Override
public long getItemId(int i) {
return i;
}
//设置每一项friend item的UI
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
convertView = layoutInflater.inflate(R.layout.item_friend_layout,null); //加载item布局
//获取item布局的子控件,将friend的数据设置到控件上
TextView textView = (TextView) convertView.findViewById(R.id.item_tv_name);
textView.setText((String)data.get(position).getName()); //设置名称
ImageView imageView = (ImageView)convertView.findViewById(R.id.item_im_icon);
imageView.setImageResource(data.get(position).getIcon()); //设置图片
TextView textView1 = (TextView)convertView.findViewById(R.id.item_tv_des);
textView1.setText((String)data.get(position).getDescription()); //设置描述
return convertView;
}
}
四、在MainActivity.java设置ListView
1、在主布局里面添加ListView控件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
LinearLayout>
2、设置ListView控件
MainActivit.java
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.lv_1);
setListView();
}
//设置ListView
private void setListView(){
//设置ListView内容
FriendAdapter friendAdapter = new FriendAdapter(this,getData());
listView.setAdapter(friendAdapter);
//设置ListView点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,((FriendAdapter)listView.getAdapter()).getItem(i).getName() + ": " +
""+((FriendAdapter)listView.getAdapter()).getItem(i).getDescription(), Toast.LENGTH_SHORT).show();
}
});
}
//获取列表内部的friend数据
private List<Friend> getData(){
List<Friend> data = new ArrayList<Friend>();
for(int i = 0; i<10;i++){
//根据图片名称获取图片id
int id = getResources().getIdentifier("head_"+i%4,"drawable",getPackageName());
data.add(new Friend("用户"+i,id,"我是用户"+i));
}
return data;
}
}
五、效果显示
转: