ListView分组导航

分组显示数据,屏幕最上面显示当前组。可读取联系人,排序好后以这样的分组导航来显示。

实现效果如图,请忽略界面的难看:



header_activity.xml,因为主布局和listview里的item布局都需要,在主布局里显示当前所在组,在item里显示每组的首字符。所以就单独拿出来了:

<?xml version="1.0" encoding="utf-8"?>
<TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/head_text"
        style="@android:style/TextAppearance.Small"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="#aaaa00"
/>


主布局文件,包括屏幕最上面的heaer和显示数据的listview:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"

        >
   <ListView
           android:id="@+id/list"
           android:layout_height="match_parent"
           android:layout_width="match_parent"
           />

    <include layout="@layout/header_activity" />

</FrameLayout>




listview里的item布局,两个textview,上面一个显示第一个字符,作为header,下面的显示内容;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <include layout="@layout/header_activity" />

    <TextView
            android:id="@+id/item_text"
            style="@android:style/TextAppearance.Large"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            />
</LinearLayout>


主文件:

public class MyActivity extends Activity {
    private ListView mListView;
    private TextView mTopHeaderView;
    private int mTopVisiblePosition;
 //要显示的内容
    private String[] str = new String[]{  "abc","american","ada","bbc",
                                          "bob", "tim","tid","bod","ddt",
                                          "data", "dada", "heeee", "sara", "heihei", "haha",
                                          "kuba", "kouba", "kasa", "kago",};
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTopHeaderView = (TextView) findViewById(R.id.head_text);

        mListView = (ListView) findViewById(R.id.list);
        mListView.setEmptyView(findViewById(R.id.empty_view));
        //添加自定义的适配器到listview
        mListView.setAdapter(new SectionAdapter(this, str));
        //监听滚动事件
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
               //如果屏幕最上面显示的一行改变了,则更新头部显示
                if (firstVisibleItem != mTopVisiblePosition) {
                    mTopVisiblePosition = firstVisibleItem;
                    setTopHeader(firstVisibleItem);
                }

            }
        });
       //初始化头部显示
        setTopHeader(0);
    }

    /**
     *更新头部显示
     * @param pos
     */
    private void setTopHeader(int pos){
        final String text = str[pos].substring(0, 1);
        mTopHeaderView.setText(text);
    }

}



适配器:
public class SectionAdapter extends ArrayAdapter<String>{

    private Activity mActivity;

    public  SectionAdapter(Activity activity, String[] objects){
      super(activity,R.layout.list_items, R.id.item_text, objects);
      this.mActivity = activity;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = mActivity.getLayoutInflater().inflate(R.layout.list_items, parent,false);
        }
        TextView headerView = (TextView) convertView.findViewById(R.id.head_text);
        String currentText = getItem(position);
        if ( 0 == position ||
                //当前item的首字符与前一个item的不同则显示头,相同则不显示,这样就有分组的效果了
                currentText.charAt(0)  != getItem(position - 1).charAt(0)){

            headerView.setVisibility(View.VISIBLE);
            headerView.setText(currentText.substring(0, 1));

        }else{
            headerView.setVisibility(View.GONE);
        }
        return super.getView(position, convertView, parent);
    }
}



想实现很酷的联系人效果可参考:http://blog.csdn.net/guolin_blog/article/details/9033553


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值