1.文字列表
MainAtivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取ListView对象
ListView listview = findViewById(R.id.listview);
//准备数据源
String[] data = {
"初识安卓",
"开发环境搭建",
"基础控件",
"线性布局",
"相对布局"};
//配置数组适配器
ArryAdapter<String> dadpter
= new ArrayAdapter<String>(
this,
android.R.layout.Simple_list_item_1,//系统自带布局
data);
//绑定适配器
listview.setAdapter(adapter);
activity_main.xml:
//添加ListView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
展示图:
2.图文列表
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simpleList);
//获取ListView对象
ListView lv = findViewById(R.id.iv_main);
//准备数据源
List<Map<String, Object>> List = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("logo", R.drawable.keeplogo);
map.put("title", "KEEP");
map.put("version", "版本:5.2.0");
map.put("size", "大小:281M");
List.add(map);
map = new HashMap<String, Object>();
map.put("logo", R.drawable.logo1);
map.put("title", "智能管家");
map.put("version", "版本:8.4.0");
map.put("size", "大小:32.81M");
List.add(map);
map = new HashMap<String, Object>();
map.put("logo", R.drawable.logo2);
map.put("title", "LOG健身");
map.put("version", "版本:5.4.0");
map.put("size", "大小:132.81M");
List.add(map);
map = new HashMap<String, Object>();
map.put("logo", R.drawable.logo3);
map.put("title", "余额宝");
map.put("version", "版本:2.7.0");
map.put("size", "大小:67.40M");
List.add(map);
//配置简单适配器
SimpleAdapter adapter = new SimpleAdapter(
this,//上下文
List,//数据源
R.layout.item,//行布局
//Map中的key
new string[]{"logo","title","version","size"},
//控件id
new int[]
{R.id.logo,R.id.title,R.id.version,R.id.size}
);
//绑定适配器
lv.setAdapter(adapter);
}
item.xml(自定义行布局文件):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
//item获取焦点
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="3dp"
>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/logo"
/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/button"
android:layout_toRightOf="@+id/logo"
android:layout_marginLeft="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textColor="#444444"
android:textSize="16sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vertion"
android:textColor="#999"
android:layout_marginTop="6dp"
android:textSize="12sp"
android:layout_below="@+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/size"
android:textColor="#999"
android:textSize="12sp"
android:layout_marginTop="6dp"
android:layout_below="@+id/vertion"
/>
</RelativeLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="下载" />
</RelativeLayout>
simpleList.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_main"
//item选中背景改变
android:listSelector="@drawable/item">
</ListView>
</RelativeLayout>
展示图:
自定义列表(采用ViewHolder和convertView优化)
MainAcivity.java:
public class MainActivity extends AppCompatActivity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myList);
//获取listview对象
ListView lv = findViewById(R.id.iv_main);
//向map对象中存入键值对数据
List<Map<String, Object>> List = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("logo", R.drawable.keeplogo);
map.put("title", "KEEP");
map.put("version", "版本:5.2.0");
map.put("size", "大小:281M");
//将map对象存入List容器中
List.add(map);
map = new HashMap<String, Object>();
map.put("logo", R.drawable.logo1);
map.put("title", "智能管家");
map.put("version", "版本:8.4.0");
map.put("size", "大小:32.81M");
List.add(map);
map = new HashMap<String, Object>();
map.put("logo", R.drawable.logo2);
map.put("title", "LOG健身");
map.put("version", "版本:5.4.0");
map.put("size", "大小:132.81M");
List.add(map);
map = new HashMap<String, Object>();
map.put("logo", R.drawable.logo3);
map.put("title", "余额宝");
map.put("version", "版本:2.7.0");
map.put("size", "大小:67.40M");
List.add(map);
//绑定适配器
MyAdapter adapter = new MyAdapter(List,this);
lv.setAdapter(adapter);
}
/配置适配器/
Myadapter.java:
public class MyAdapter extends BaseAdapter {
//定义对象
private Context context;
private List<Map<String, Object>> List;/
private LayoutInflater inflater;
//初始化对象
public MyAdapter(List<Map<String, Object>> List, Context context) {
this.context = context;
this.List = List;
inflater = LayoutInflater.from(context);
}
//获取列表总长度
@Override
public int getCount() {
return List.size();
}
//获取当前item的position
@Override
public Object getItem(int i) {
return List.get(i);
}
//获取当前item的id
@Override
public long getItemId(int i) {
return i;
}
//获取View对象
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup){
//定义ViewHolder
ViewHolder holder = null;
//添加viewHolder类
public class ViewHolder {
ImageView logo;
TextView title;
TextView version;
TextView size;
}
if (convertView == null) {
//将行布局文件转化为convertView对象
convertView = inflater.inflate(R.layout.item, null);
//实例化viewholder
holder = new ViewHolder();
//获取控件对象
holder.logo = convertView.findViewById(R.id.logo);
holder.title = convertView.findViewById(R.id.title);
holder.version = convertView.findViewById(R.id.vertion);
holder.size = convertView.findViewById(R.id.size);
//保存viewholder
convertView.setTag(holder);
} else {
//取出viewholder
holder = (ViewHolder) convertView.getTag();
}
//将数据传递给控件
Map map = List.get(i);
holder.logo.setImageResource((Integer) map.get("logo"));
holder.title.setText((String) map.get("title"));
holder.version.setText((String) map.get("version"));
holder.size.setText((String) map.get("size"));
//返回convertView
return convertView;
}
}
item.xml(自定义行布局文件):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
//item获取焦点
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="3dp"
>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/logo"
/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/button"
android:layout_toRightOf="@+id/logo"
android:layout_marginLeft="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textColor="#444444"
android:textSize="16sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vertion"
android:textColor="#999"
android:layout_marginTop="6dp"
android:textSize="12sp"
android:layout_below="@+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/size"
android:textColor="#999"
android:textSize="12sp"
android:layout_marginTop="6dp"
android:layout_below="@+id/vertion"
/>
</RelativeLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="下载" />
</RelativeLayout>
myList.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_main"
android:listSelector="@drawable/item">
</ListView>
</RelativeLayout>