MainActivity.java
package com.example.listviewdemos;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button ArrayBT,BaseBT,SimpleBT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayBT = (Button)findViewById(R.id.ArrayBT);
SimpleBT = (Button)findViewById(R.id.SimpleBT);
BaseBT = (Button)findViewById(R.id.BaseBT);
ArrayBT.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ArrayAdapterDemoActivity.class);
startActivity(intent);
}
});
SimpleBT.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SimpleAdapterDemoActivity.class);
startActivity(intent);
}
});
BaseBT.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,BaseAdapterDemoActivity.class);
startActivity(intent);
}
});
}
}
ArrayAdapterDemoActivity.java
package com.example.listviewdemos;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class ArrayAdapterDemoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_adapter_demo);
ListView listView= (ListView)findViewById(R.id.news_category);
String []mData = getResources().getStringArray(R.array.news_category);
ArrayAdapter<String>adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,mData);
listView.setAdapter(adapter);
}
}
BaseAdapterDemoActivity.java
package com.example.listviewdemos;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class BaseAdapterDemoActivity extends AppCompatActivity {
private List<HashMap<String, Object>> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_adapter_demo);
//视图
ListView listView = (ListView) findViewById(R.id.baby_album_bt);
//准备子项布局Listview_item_bt.xml
//准备数据
mData = getData();
MyAdapter adapter = new MyAdapter(this);
//桥接
listView.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter {
LayoutInflater mInflater;
private Log log;
public MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
//实例化一个新的itemView
convertView = mInflater.inflate(R.layout.listview_item_bt, null);
//缓存
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.album_thumb);
holder.title = (TextView) convertView.findViewById(R.id.album_title);
holder.info = (TextView) convertView.findViewById(R.id.album_info);
holder.btn = (ImageButton) convertView.findViewById(R.id.album_btn);
convertView.setTag(holder);
log.e("BaseAdapter3",""+convertView.toString());
} else {//复用convertView
holder = (ViewHolder) convertView.getTag();
log.e("BaseAdapter3",""+convertView.toString());
}//绑定数据到子项
HashMap<String, Object> data = mData.get(position);
holder.img.setImageResource((Integer) data.get("album_thumb"));
holder.title.setText(data.get("album_title").toString());
holder.info.setText(data.get("album_info").toString());
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInfo(position);
}
});
return convertView;
}
}
private void showInfo(int position){
HashMap<String,Object> data = mData.get(position);
new AlertDialog.Builder(this)
.setTitle(data.get("album_title").toString())
.setMessage(data.get("album_info").toString())
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
//子控件布局中的子控件的view缓存
public final class ViewHolder{
private ImageView img;
private TextView title;
private TextView info;
private ImageButton btn;
private View WholseView;
}
private List<HashMap<String,Object>> getData()
{
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列1");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列2");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列3");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i3);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列4");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i4);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列5");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i5);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列6");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i6);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列7");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i7);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列8");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列9");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列10");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i3);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列11");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i4);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列12");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i5);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列13");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i6);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列14");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i7);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列15");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列16");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列17");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i3);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列18");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i4);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列19");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i5);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列20");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i6);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列21");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i6);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列22");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i7);
list.add(map);
return list;
}
}
SimpleAdapterDemoActivity.java
package com.example.listviewdemos;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class SimpleAdapterDemoActivity extends AppCompatActivity {
private List<HashMap<String,Object>> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_adapter_demo);
//视图
ListView listView = (ListView) findViewById(R.id.baby_album);
//准备子项布局:listview_item.xml
// 准备数据
mData = getData();
//准备Adaptera
String[] keysMap = {"album_title", "album_info", "album_thumb"};
int[] resId = {R.id.album_title, R.id.album_info, R.id.album_thumb};
SimpleAdapter adapter = new SimpleAdapter(this, mData, R.layout.listview_item, keysMap, resId);
//桥接
listView.setAdapter(adapter);
//子项单击
/*listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
private void showInfo(int position){
HashMap<String,Object>data = mData.get(position);
new AlertDialog.Builder(this)
.setTitle(data.get("album_title").toString())
.setMessage(data.get("album_info").toString())
.setPositiveButton("确定", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
}).show();}
});*/
}
private List<HashMap<String,Object>> getData()
{
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i3);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i4);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i5);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i6);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i7);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i3);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i4);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i5);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i6);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i7);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i3);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i4);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i5);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i6);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i6);
list.add(map);
map = new HashMap<String, Object>();
map.put("album_title", "毡帽系列");
map.put("album_info", "此系列服装有点cute,像不像小车夫。");
map.put("album_thumb", R.drawable.i7);
list.add(map);
return list;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/ArrayBT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ArrayAdapterDemoActivity"
/>
<Button
android:id="@+id/SimpleBT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SimpleAdapterDemoActivity" />
<Button
android:id="@+id/BaseBT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BaseAdapterDemoActivity" />
</LinearLayout>
activity_array_adapter_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ArrayAdapterDemoActivity">
<ListView
android:id="@+id/news_category"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity_base_adapter_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BaseAdapterDemoActivity">
<ListView
android:id="@+id/baby_album_bt"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity_simple_adapter_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SimpleAdapterDemoActivity">
<ListView
android:id="@+id/baby_album"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/album_thumb"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="@+id/album_title"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textSize="16sp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/album_info"
android:layout_width="match_parent"
android:layout_height="30dp"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
listview_item_bt.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/album_thumb"
android:layout_width="0dp"
android:layout_height="119dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="133dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="@+id/album_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp" />
<TextView
android:id="@+id/album_info"
android:layout_width="match_parent"
android:layout_height="38dp"
android:textSize="14sp" />
<ImageButton
android:id="@+id/album_btn"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:src="@drawable/album_btt"
android:background="@android:color/transparent"/>
</LinearLayout>
</LinearLayout>
res/lvalues/Strings.xml
<resources>
<string name="app_name">ListViewDemos</string>
<string-array name="news_category">
<item>新闻</item>
<item>财经</item>
<item>科技</item>
<item>体育</item>
<item>娱乐</item>
<item>汽车</item>
<item>博客</item>
<item>读书</item>
<item>新闻</item>
<item>财经</item>
<item>科技</item>
<item>体育</item>
<item>娱乐</item>
<item>汽车</item>
<item>博客</item>
<item>读书</item>
<item>新闻</item>
<item>财经</item>
<item>科技</item>
<item>体育</item>
<item>娱乐</item>
<item>汽车</item>
<item>博客</item>
<item>读书</item>
</string-array>
</resources>