ListView——android菜鸟成长之路

时间:2022-04-15 15:40:31

ListView的基本用法

  建博客这么久了,一直没能写点什么,其实一直想写来着,却又无从下手,今天终于下定决心写点什么,好吧,就ListView吧,这个控件是个搞基控件,所以初学者都会觉得很难,于是乎写了个demo,好吧转入正题!

  • 效果图:

ListView——android菜鸟成长之路

  • 工程结构(3个class,2个xml):

    分别是一个activity

    一个实体类Mode

    一个适配器adapter

ListView——android菜鸟成长之路

  • 首先是程序的入口类MainActivity:
public class MainActivity extends AppCompatActivity {

    private List<Mode> itemList =null;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = this;
setContentView(R.layout.activity_main);
......
initdata();
ListView listView = (ListView) findViewById(R.id.lv);
myAdapter myAdapter = new myAdapter(this, itemList);
listView.setAdapter(myAdapter); }
//绑定假数据
void initdata(){
for (int i = 0; i < 20; i++) {
Mode mode = new Mode();
mode.setTitle("标题" + i);
mode.setName("程序员" + i);
itemList.add(mode);
}
}
  • activity_main.xml:
 <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
  • list_item.xml:

这个布局文件决定了item的样式、结构。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:background="@color/colorAccent"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp"> <TextView
android:gravity="center"
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/> <TextView
android:gravity="center"
android:id="@+id/tv_name"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout> </LinearLayout>
  • Mode实体类:
public class Mode implements Serializable{
private String title=null;
private String name=null; public String getTitle(){
return title;
}
public void setTitle(String Title){
title=Title;
}
public String getName(){
return name;
}
public void setName(String Name){
name=Name;
}
}
  • 接下来就是最重要的Adapter——myAdapter.class:
package com.example.administrator.listview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; import java.util.List; /**
* Created by Administrator on 2016/6/28.
*/
public class myAdapter extends BaseAdapter{ private Context context;
private List<Mode> modeList=null; public myAdapter(Context context,List<Mode> listList) {//activity中传递过来的上下文和list集合
this.context=context;
modeList=listList;
} @Override
public int getCount() {//获取item的数目
return modeList.size();
} @Override
public Object getItem(int position) {//获取item
return modeList.get(position);
} @Override
public long getItemId(int position) {//item对应的id
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHodler viewHodler=null;
if(null==convertView)
{
viewHodler=new ViewHodler();
convertView=convertView.inflate(context,R.layout.list_item,null);
viewHodler.title=(TextView)convertView.findViewById(R.id.tv_title);
viewHodler.name=(TextView)convertView.findViewById(R.id.tv_name);
convertView.setTag(viewHodler);
}else{
viewHodler=(ViewHodler)convertView.getTag();
} Mode mode=modeList.get(position);
if (mode!=null){
viewHodler.title.setText(mode.getTitle());
viewHodler.name.setText(mode.getName());
} return convertView;
} public class ViewHodler {
TextView title;
TextView name;
}
}

    ListView大致基本结构就是如上所示,如果本文有不对的地方,还望指正。