android listView 根据字母排序和定位 (http://www.apkbus.com/android-69999-1-1.html)

第一步是实现字母的a-z的控件的显示:
import
android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
 
public class SideBar extendsView {
    privatechar[] l;
    privateSectionIndexer sectionIndexter = null;
    privateListView list;
    privateTextView mDialogText;
    Bitmap mbitmap;
    privateint type = 1;
    privateint color = 0xff858c94;
 
    publicSideBar(Context context) {
        super(context);
        init();
    }
 
    publicSideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    privatevoid init() {
 
        l =new char[] {‘!‘,‘A‘,‘B‘, ‘C‘,‘D‘, ‘E‘,‘F‘, ‘G‘,‘H‘,
         ‘I‘,‘J‘, ‘K‘,‘L‘, ‘M‘,‘N‘, ‘O‘,‘P‘, ‘Q‘,‘R‘, ‘S‘,‘T‘, ‘U‘,‘V‘,
          ‘W‘,‘X‘, ‘Y‘,‘Z‘,‘#‘};
        mbitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.scroll_bar_search_icon);
    }
 
    publicSideBar(Context context, AttributeSet attrs, intdefStyle) {
        super(context, attrs, defStyle);
        init();
    }
 
 
    publicvoid setListView(ListView _list) {
        list = _list;
        HeaderViewListAdapter ha = (HeaderViewListAdapter) _list
                .getAdapter();
        MyAdapter ad = (MyAdapter)ha.getWrappedAdapter();
        sectionIndexter = (SectionIndexer) ad;
         
    }
 
    publicvoid setTextView(TextView mDialogText) {
        this.mDialogText = mDialogText;
    }
 
    publicboolean onTouchEvent(MotionEvent event) {
 
        super.onTouchEvent(event);
        inti = (int) event.getY();
 
        intidx = i / (getMeasuredHeight() / l.length);
        if(idx >= l.length) {
            idx = l.length -1;
        }else if(idx < 0) {
            idx =0;
        }
        if(event.getAction() == MotionEvent.ACTION_DOWN
                || event.getAction() == MotionEvent.ACTION_MOVE) {
            setBackgroundResource(R.drawable.scrollbar_bg);
            mDialogText.setVisibility(View.VISIBLE);
            if(idx == 0) {
                mDialogText.setText("Search");
                mDialogText.setTextSize(16);
            }else {
                mDialogText.setText(String.valueOf(l[idx]));
                mDialogText.setTextSize(34);
            }
            if(sectionIndexter == null) {
 
             
 
                sectionIndexter = (SectionIndexer) list.getAdapter();
                 
            }
            intposition = sectionIndexter.getPositionForSection(l[idx]);
 
            if(position == -1) {
                returntrue;
            }
            list.setSelection(position);
        }else {
            mDialogText.setVisibility(View.INVISIBLE);
 
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            setBackgroundDrawable(newColorDrawable(0x00000000));
        }
        returntrue;
    }
 
    protectedvoid onDraw(Canvas canvas) {
        Paint paint =new Paint();
        paint.setColor(color);
        paint.setTextSize(12);
        paint.setStyle(Style.FILL);    
        paint.setTextAlign(Paint.Align.CENTER);
        floatwidthCenter = getMeasuredWidth() / 2;
        if(l.length > 0) {
            floatheight = getMeasuredHeight() / l.length;
            for(int i = 0; i < l.length; i++) {
                if(i == 0 && type != 2) {
                    canvas.drawBitmap(mbitmap, widthCenter -7, (i + 1)
                            * height - height /2, paint);
                }else
                    canvas.drawText(String.valueOf(l[i]), widthCenter,
                            (i +1) * height, paint);
            }
        }
        this.invalidate();
        super.onDraw(canvas);
    }
}

第二步:设计XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@null" />
 
    <com.example.listsildedel.SideBar
        android:id="@+id/sideBar"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical" />
 
</FrameLayout>

第三步:继承BaseAdapter并实现SectionIndexer接口,并且Item是通过当字母重复的时候进行隐藏 ,不重复时显示。
import java.util.List;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;
 
public class MyAdapter extendsBaseAdapter implementsSectionIndexer{
 
    privateList<Content> list = null;
    privateContext mContext;
    privateSectionIndexer mIndexer;
     
    publicMyAdapter(Context mContext, List<Content> list) {
        this.mContext = mContext;
        this.list = list;
 
    }
 
    publicint getCount() {
        returnthis.list.size();
    }
 
    publicObject getItem(intposition) {
        returnnull;
    }
 
    publiclong getItemId(intposition) {
        returnposition;
    }
 
    publicView getView(finalint position, View view, ViewGroup arg2) {
        ViewHolder viewHolder =null;
        if(view == null) {
            viewHolder =new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
            viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        finalContent mContent = list.get(position);
        if(position == 0) {
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(mContent.getLetter());
        }else {
            String lastCatalog = list.get(position -1).getLetter();
            if(mContent.getLetter().equals(lastCatalog)) {
                viewHolder.tvLetter.setVisibility(View.GONE);
            }else {
                viewHolder.tvLetter.setVisibility(View.VISIBLE);
                viewHolder.tvLetter.setText(mContent.getLetter());
            }
        }
     
        viewHolder.tvTitle.setText(this.list.get(position).getName());
         
        returnview;
 
    }
     
 
 
    finalstatic class ViewHolder {
        TextView tvTitle;
        TextView tvLetter;
    }
 
 
    publicObject[] getSections() {
        // TODO Auto-generated method stub
        returnnull;
    }
 
    publicint getSectionForPosition(intposition) {
         
        return0;
    }
 
    publicint getPositionForSection(intsection) {
        Content mContent;
        String l;
        if(section == ‘!‘) {
            return0;
        }else {
            for(int i = 0; i < getCount(); i++) {
                mContent = (Content) list.get(i);
                l = mContent.getLetter();
                charfirstChar = l.toUpperCase().charAt(0);
                if(firstChar == section) {
                    returni + 1;
                }
 
            }
        }
        mContent =null;
        l =null;
        return-1;
    }
}

第四步: activity的实现:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
 
public class MainActivity extendsActivity {
 
    privateListView mListView;
    privateSideBar indexBar;
    privateWindowManager mWindowManager;
    privateTextView mDialogText;
    privateView head;
 
    @Override
    publicvoid onCreate(Bundle savedInstanceState) {
         //启动activity时不自动弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView)this.findViewById(R.id.list);
        indexBar = (SideBar) findViewById(R.id.sideBar);
        mDialogText = (TextView) LayoutInflater.from(this).inflate(R.layout.list_position,null);
        head = LayoutInflater.from(this).inflate(R.layout.head,null);
        mListView.addHeaderView(head);
        mDialogText.setVisibility(View.INVISIBLE);
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams lp =new WindowManager.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        mWindowManager.addView(mDialogText, lp);
        indexBar.setTextView(mDialogText);
        //初始化数据
        List<Content> list =new ArrayList<Content>();
        for(int i = 0; i < 10; i++) {
            Content m;
            if(i < 3)
                m =new Content("A","选项" + i);
            elseif (i < 6)
                m =new Content("F","选项" + i);
            else
                m =new Content("D","选项" + i);
            list.add(m);
        }
        //根据a-z进行排序
        Collections.sort(list,new PinyinComparator());
        // 实例化自定义内容适配类     
        MyAdapter adapter =new MyAdapter(this, list);
        // 为listView设置适配
        mListView.setAdapter(adapter);
        //设置SideBar的ListView内容实现点击a-z中任意一个进行定位
        indexBar.setListView(mListView);       
    }
 
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。