1.类似效果如下:
![](https://img-my.csdn.net/uploads/201210/16/1350379574_8008.jpg)
2.实现效果如下(时间有限完成的一般):
![](https://img-my.csdn.net/uploads/201210/17/1350438947_4675.png)
3.上代码
3.1 ListViewTest.java
import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class ListViewTest extends Activity{ private ListView list; private ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); init(); } private void init() { list = (ListView)findViewById(R.id.listView1); /** * 获取数据时应该把相同flag的数据放在一起 * 不同的放在前面 */ HashMap< String, String> map; map = new HashMap<String, String>(); map.put("name", "测试数据A"); map.put("flag", "flag"); data.add(map); map = new HashMap<String, String>(); map.put("name", "测试数据B"); map.put("flag", "flag"); data.add(map); map = new HashMap<String, String>(); map.put("name", "测试数据C"); map.put("flag", "flag"); data.add(map); map = new HashMap<String, String>(); map.put("name", "测试数据D"); map.put("flag", "flag"); data.add(map); for(int i = 0; i<20; i++) { map = new HashMap<String, String>(); map.put("name", "测试数据"+i); map.put("flag", "flag"+i/5); data.add(map); } int length = data.size(); String flag = ""; for(int i=0; i<length; i++){//flag 处理 if((i<length-1)&&!flag.equals(data.get(i+1).get("flag").replace("flag", ""))) { flag = data.get(i+1).get("flag").replace("flag", ""); data.get(i).put("flag", flag); }else{ data.get(i).put("flag", "flag"); } } list.setCacheColorHint(0); list.setAdapter(new Adapter()); } class Adapter extends BaseAdapter { @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.listview_item, null); TextView name = (TextView)view.findViewById(R.id.name); name.setText(data.get(position).get("name")); if(!"flag".equals(data.get(position).get("flag"))) { TextView flag_name = (TextView) view.findViewById(R.id.flag_name); TextView flag = (TextView) view.findViewById(R.id.flag); LinearLayout line = (LinearLayout) view.findViewById(R.id.line); flag_name.setText(data.get(position).get("flag")); flag.setText(data.get(position).get("flag").replace("flag", "")); line.setVisibility(View.VISIBLE); flag_name.setVisibility(View.VISIBLE); flag.setVisibility(View.VISIBLE); } return view; } } }
3.2 listview.xml
<?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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
3.3listview_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="100dip" android:background="#ffffff" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="name" /> <TextView android:id="@+id/flag_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name" android:text="flag_name" android:visibility="invisible" /> <TextView android:id="@+id/flag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name" android:text="flag" android:layout_alignParentRight="true" android:visibility="invisible"/> <LinearLayout android:id="@+id/line" android:layout_width="fill_parent" android:layout_height="2dip" android:layout_alignParentBottom="true" android:background="#ff0000" android:visibility="invisible" > </LinearLayout> </RelativeLayout>
这只是个demo如有其它需求可以根据这个思路 自己修改-.-。