运行效果:
main.xml
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/MyListView">
</ListView>
</LinearLayout>
list_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/myListItem"
android:paddingBottom="4dip" android:paddingLeft="12dip"
android:paddingRight="12dip">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/itemTitle"
android:textSize="20dip" />
</LinearLayout>
list_items_tag.xml
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#555555" android:paddingLeft="10dip">
<TextView android:id="@+id/itemTitle" android:layout_width="wrap_content"
android:layout_height="20dip" android:textColor="#ffffff"
android:gravity="center_vertical" />
</LinearLayout>
MyAdapter.java
Java代码
package com.iaiai;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Map<String, String>> listData;
private List<Map<String, String>> splitData;
public MyAdapter(Context context,
List<Map<String, String>> listData,
List<Map<String, String>> splitData) {
this.mInflater = LayoutInflater.from(context);
this.listData = listData;
this.splitData = splitData;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean isEnabled(int position) {
if (splitData.contains(listData.get(position))) {
return false;
}
return super.isEnabled(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (splitData.contains(listData.get(position))) {
convertView = mInflater.inflate(R.layout.list_items_tag, null);
} else {
convertView = mInflater.inflate(R.layout.list_items, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.itemTitle);
textView.setText(listData.get(position).get("itemTitle"));
return convertView;
}
}
MainActivity.java
Java代码
package com.iaiai;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private List<Map<String, String>> mylist = new ArrayList<Map<String, String>>();
private List<Map<String, String>> splitList = new ArrayList<Map<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.MyListView);
setData(); // 设置数据
// 配置适配器
MyAdapter adapter = new MyAdapter(this, mylist, splitList); // 布局里的控件id
// 添加并且显示
list.setAdapter(adapter);
}
private void setData() {
// 组织数据源
Map<String, String> mp = new HashMap<String, String>();
mp.put("itemTitle", "A");
mylist.add(mp);
splitList.add(mp);
for (int i = 0; i < 3; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("itemTitle", "文章1-" + i);
mylist.add(map);
}
mp = new HashMap<String, String>();
mp.put("itemTitle", "B");
mylist.add(mp);
splitList.add(mp);
for (int i = 0; i < 6; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("itemTitle", "文章2-" + i);
mylist.add(map);
}
}
}
========================================================================
以上为示例所有代码,以下为说明
禁止标签项的响应事件,父类BaseAdapter中提供了isEnable的()方法,我们看看这个方法:
Java代码
- //默认情况,如果这个方法不是分割符,返回true
- //分隔符是无选中和无点击事件的
- //说白了,你想不想把改position项当做分隔符,想的话就返回false,否则返回true
- public boolean isEnabled (int position)
//默认情况,如果这个方法不是分割符,返回true
//分隔符是无选中和无点击事件的
//说白了,你想不想把改position项当做分隔符,想的话就返回false,否则返回true
public boolean isEnabled (int position)
这个方法刚好用来禁用标签项的响应事件。
Java代码
@Override
public boolean isEnabled(int position) {
if (splitData.contains(listData.get(position))) {
return false;
}
return super.isEnabled(position);
}
现在标签项不会再有任何触控效果了,犹如一块死木板。