目录
一、自定义Adapter
二、在布局文件中添加ListView标签
三、编写行布局文件
四、在Activity中编写代码
五、ListView Item点击长按事件
六、ListView 优化
七、页面跳转传值
一、在java/com.ryan.test下创建MyAdapter类,继承类BaseAdapter类
public class MyAdapter extends BaseAdapter{
//数据集合
List<Map<String,Object>> list;
//反射器
LayoutInflater inflater;
//构造器
public MyAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
}
public void setList(List<Map<String, Object>> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//实现getView方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//反射行布局
View view = inflater.inflate(R.layout.item,null);
//获取各个控件
ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView version = (TextView) convertView.findViewById(R.id.version);
TextView size = (TextView) convertView.findViewById(R.id.size);
//给控件赋值
Map map = list.get(position);
logo.setImageResource((Integer) map.get("logo"));
title.setText((String) map.get("title"));
version.setText((String) map.get("version"));
size.setText((String) map.get("size"));
return view;
}
}
二、在布局文件中添加ListView标签(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ryan.test.MainActivity">
<ListView
android:listSelector="@drawable/item_selector"
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
注:用Selector背景选择器实现Item项选中后的背景颜色改变
res/drawable下item_selector.xml文件设置选中和未选中的颜色 透明色为#00000000
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/transparent" android:state_pressed="false"/>
<item android:drawable="@color/choose" android:state_pressed="true"/>
</selector>
三、编写行布局文件(item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:descendantFocusability="blocksDescendants"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="5dp"
android:layout_margin="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="@+id/logo"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/danger"
/>
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_marginTop="3dp"
android:textSize="18sp"
android:text="危险品"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/version"
android:layout_marginTop="3dp"
android:textSize="12sp"
android:text="版本:1.0"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/size"
android:layout_marginTop="3dp"
android:textSize="12sp"
android:text="占用空间:20M"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:text="卸载"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/btn_selector"
android:textColor="#fff"
android:textSize="18sp"
/>
</LinearLayout>
</LinearLayout>
注:
问题: 当点击某一行的时候, 没有背景色的变化
原因: 行中包含了按钮, 按钮抢夺了焦点
解决方案: 夺回焦点,在行布局中设置descendantFocusability属性
四、在Activity中编写代码
//1.获取ListView对象
ListView list_main = (ListView) findViewById(R.id.list_main);
//2.数据源
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("logo", R.drawable.danger);
map.put("title", "千千静听");
map.put("version", "版本: 8.4.0");
map.put("size", "大小: 32.81M");
list.add(map);
//3. 准备适配器Adapter
MyAdapter adapter = new MyAdapter(this);
adapter.setList(list);
//4. 将适配器关联到ListView
list_main.setAdapter(adapter);
五、ListView Item点击长按事件
//5.监听item事件
list_main.setOnItemClickListener(this);
list_main.setOnItemLongClickListener(this);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this,"点击"+position,Toast.LENGTH_SHORT).show();
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this,"长按"+position,Toast.LENGTH_SHORT).show();
return true;
}
注:长按事件onItemLongClick需要返回值
如果希望长按事件后,不再触发点击事件,则应将返回值设为true
六、ListView 优化
public class ViewHolder{
ImageView logo;
TextView title;
TextView version;
TextView size;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.item,null);
holder = new ViewHolder();
holder.logo = (ImageView) convertView.findViewById(R.id.logo);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.version = (TextView) convertView.findViewById(R.id.version);
holder.size = (TextView) convertView.findViewById(R.id.size);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
Map map = list.get(position);
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"));
return convertView;
}
七、页面跳转传值
1.创建一个独立的Activity
public class DetailActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
String index = getIntent().getStringExtra("index");
String title = getIntent().getStringExtra("title");
TextView info = (TextView) findViewById(R.id.info);
info.setText("编号:"+index+" 标题:"+title);
}
}
2.编写item点击事件,实现跳转操作
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//1.创建Intent对象
Intent intent = new Intent();
//2.设置跳转界面
intent.setClass(this,DetailActivity.class);
//传值
HashMap<String,Object> itemMap = (HashMap<String,Object>)parent.getItemAtPosition(position);
intent.putExtra("index",""+position);
intent.putExtra("title",""+itemMap.get("title"));
//3.跳转
startActivity(intent);
}