类似于选择城市相同的首字母放在一起的效果

1.类似效果如下:
2.实现效果如下(时间有限完成的一般):


3.上代码
3.1 ListViewTest.java
[java]  view plain copy print ?
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
[java]  view plain copy print ?
<?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
[java]  view plain copy print ?
<?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如有其它需求可以根据这个思路 自己修改-.-。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值