android – 如何使用fastscroll和albhabet索引器实现ListView

android – 如何使用fastscroll和albhabet索引器实现ListView,第1张

概述请帮助我这2天.我想实现一个带有fastscroll和albhabet索引器的ListView,如联系人应用程序/下面的图像.我正在使用SimpleAdapter来填充ListView.从图像中可以看出,通过从右侧的字母索引器中选择一个字母,listView选项将转到相应的ListItem.请分享一些例子.谢谢你提前. EX:下面是我的代码,它只是排序字母表,当我点击E字母表时,与E相关的数据是g 请帮助我这2天.我想实现一个带有fastscroll和albhabet索引器的ListVIEw,如联系人应用程序/下面的图像.我正在使用SimpleAdapter来填充ListVIEw.从图像中可以看出,通过从右侧的字母索引器中选择一个字母,ListVIEw选项将转到相应的ListItem.请分享一些例子.谢谢你提前.
EX:下面是我的代码,它只是排序字母表,当我点击E字母表时,与E相关的数据是gng to diaplay ..但是当我触摸字母时,它没有在中间显示E文本框.

如何在中间显示E文本框..当我触摸快速滚动中间文本框时必须显示.

MainActivity.java

public class MainActivity extends Activity implements SearchVIEw.OnqueryTextListener,SearchVIEw.OnCloseListener {private ListVIEw ListVIEw;private SearchVIEw search;EfficIEntAdapter objectAdapter;EfficIEntAdapter2 objectAdapter1;int textlength=0;private CheckBox checkStat,checkRoutine,checkTat; private GestureDetector mGestureDetector;    // x and y coordinates within our sIDe index    private static float sIDeIndexX;    private static float sIDeIndexY;    // height of sIDe index    private int sIDeIndexHeight;    // number of items in the sIDe index    private int indexListSize;    // List with items for sIDe index    private ArrayList<Object[]> indexList = null;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);   setContentVIEw(R.layout.homempleb);   Log.i("scan"," txtScanResult ");   Arrays.sort(CountrIEsList.name);ActionItem nextItem     = new ActionItem(); final quickaction quickaction = new quickaction(this,quickaction.VERTICAL);quickaction.addActionItem(nextItem);quickaction.setondismissListener(new quickaction.OndismissListener()           {                @OverrIDe    public voID ondismiss() {        Toast.makeText(getApplicationContext(),"dismissed",Toast.LENGTH_SHORT).show();    }});ListVIEw = (ListVIEw) findVIEwByID(R.ID.homeListVIEw);ListVIEw.setTextFilterEnabled(true);objectAdapter = new EfficIEntAdapter(this); mGestureDetector = new GestureDetector(this,new SIDeIndexGestureListener());ListVIEw.setAdapter(objectAdapter);} @OverrIDe    public boolean ontouchEvent(MotionEvent event)    {        if (mGestureDetector.ontouchEvent(event))        {            return true;        } else        {            return false;        }    }    private ArrayList<Object[]> createIndex(String[] strArr)    {        ArrayList<Object[]> tmpIndexList = new ArrayList<Object[]>();        Object[] tmpIndexItem = null;        int tmpPos = 0;        String tmpLetter = "";        String currentLetter = null;        String strItem = null;        for (int j = 0; j < strArr.length; j++)        {            strItem = strArr[j];            currentLetter = strItem.substring(0,1);            // every time new letters comes            // save it to index List            if (!currentLetter.equals(tmpLetter))            {                tmpIndexItem = new Object[3];                tmpIndexItem[0] = tmpLetter;                tmpIndexItem[1] = tmpPos - 1;                tmpIndexItem[2] = j - 1;                tmpLetter = currentLetter;                tmpPos = j + 1;                tmpIndexList.add(tmpIndexItem);            }        }        // save also last letter        tmpIndexItem = new Object[3];        tmpIndexItem[0] = tmpLetter;        tmpIndexItem[1] = tmpPos - 1;        tmpIndexItem[2] = strArr.length - 1;        tmpIndexList.add(tmpIndexItem);        // and remove first temporary empty entry        if (tmpIndexList != null && tmpIndexList.size() > 0)        {            tmpIndexList.remove(0);        }        return tmpIndexList;    }    @OverrIDe    public voID onWindowFocusChanged(boolean hasFocus)    {        super.onWindowFocusChanged(hasFocus);        final ListVIEw ListVIEw = (ListVIEw) findVIEwByID(R.ID.homeListVIEw);        linearLayout sIDeIndex = (linearLayout) findVIEwByID(R.ID.sIDeIndex);        sIDeIndexHeight = sIDeIndex.getHeight();        sIDeIndex.removeAllVIEws();        // TextVIEw for every visible item        TextVIEw tmpTV = null;        // we'll create the index List        indexList = createIndex(CountrIEsList.name);        // number of items in the index List        indexListSize = indexList.size();        // maximal number of item,which Could be displayed        int indexMaxSize = (int) Math.floor(sIDeIndex.getHeight() / 20);        int tmpIndexListSize = indexListSize;        // handling that case when indexListSize > indexMaxSize        while (tmpIndexListSize > indexMaxSize)        {            tmpIndexListSize = tmpIndexListSize / 2;        }        // computing delta (only a part of items will be displayed to save a        // place)        double delta = indexListSize / tmpIndexListSize;        String tmpLetter = null;        Object[] tmpIndexItem = null;        // show every m-th letter        for (double i = 1; i <= indexListSize; i = i + delta)        {            tmpIndexItem = indexList.get((int) i - 1);            tmpLetter = tmpIndexItem[0].toString();            tmpTV = new TextVIEw(this);            tmpTV.setText(tmpLetter);            tmpTV.setGravity(Gravity.CENTER);            tmpTV.setTextSize(20);            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);            tmpTV.setLayoutParams(params);            sIDeIndex.addVIEw(tmpTV);        }        // and set a touch Listener for it        sIDeIndex.setontouchListener(new OntouchListener()        {            @OverrIDe            public boolean ontouch(VIEw v,MotionEvent event)            {                // Now you kNow coordinates of touch                sIDeIndexX = event.getX();                sIDeIndexY = event.getY();                // and can display a proper item it country List                displayListItem();                return false;            }        });    }    class SIDeIndexGestureListener extends    GestureDetector.SimpleOnGestureListener {   @OverrIDe    public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY){    // we kNow already coordinates of first touch    // we kNow as well a scroll distance    sIDeIndexX = sIDeIndexX - distanceX;    sIDeIndexY = sIDeIndexY - distanceY;    // when the user scrolls within our sIDe index    // we can show for every position in it a proper    // item in the country List    if (sIDeIndexX >= 0 && sIDeIndexY >= 0)    {        displayListItem();    }    return super.onScroll(e1,e2,distanceX,distanceY);}  }    public voID displayListItem()    {    // compute number of pixels for every sIDe index item    double pixelPerIndexItem = (double) sIDeIndexHeight / indexListSize;    // compute the item index for given event position belongs to    int itemposition = (int) (sIDeIndexY / pixelPerIndexItem);    // compute minimal position for the item in the List    int minposition = (int) (itemposition * pixelPerIndexItem);    // get the item (we can do it since we kNow item index)    Object[] indexItem = indexList.get(itemposition);    // and compute the proper item in the country List    int indexMin = Integer.parseInt(indexItem[1].toString());    int indexMax = Integer.parseInt(indexItem[2].toString());    int indexDelta = Math.max(1,indexMax - indexMin);    double pixelPerSubitem = pixelPerIndexItem / indexDelta;    int subitemposition = (int) (indexMin + (sIDeIndexY - minposition) / pixelPerSubitem);    ListVIEw ListVIEw = (ListVIEw) findVIEwByID(R.ID.homeListVIEw);    ListVIEw.setSelection(subitemposition);    }

EfficIEntAdapter.java

public class EfficIEntAdapter extends BaseAdapter { private LayoutInflater mInflater;  private Context context;  public EfficIEntAdapter(Context context) {mInflater = LayoutInflater.from(context);this.context=context;  } public int getCount() {return CountrIEsList.name.length; }     public Object getItem(int position) {return position; }      public long getItemID(int position) {return position;   }         public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {VIEwHolder holder;if (convertVIEw == null) {    convertVIEw = mInflater.inflate(R.layout.homemplebrowvIEw,null);    holder = new VIEwHolder();    holder.text1 = (TextVIEw) convertVIEw            .findVIEwByID(R.ID.name);    holder.text2 = (TextVIEw) convertVIEw            .findVIEwByID(R.ID.mrn);    holder.text3 = (TextVIEw) convertVIEw            .findVIEwByID(R.ID.date);    holder.text4 = (TextVIEw) convertVIEw            .findVIEwByID(R.ID.age);    holder.text5 = (TextVIEw) convertVIEw            .findVIEwByID(R.ID.gender);    holder.text6 = (TextVIEw) convertVIEw            .findVIEwByID(R.ID.wardno);    holder.text7 = (TextVIEw) convertVIEw            .findVIEwByID(R.ID.roomno);    holder.text8 = (TextVIEw) convertVIEw            .findVIEwByID(R.ID.bedno);                      holder.btnList = (button)convertVIEw.findVIEwByID(R.ID.Listbutton); //   holder.btnList.setonClickListener(this);    holder.btnList.setonClickListener(new OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {                                   Intent next=new Intent(context,SeviceDetails.class);            context.startActivity(next);        }    });    convertVIEw.setTag(holder);} else {    holder = (VIEwHolder) convertVIEw.getTag();}holder.text1.setText(CountrIEsList.name[position]);holder.text2.setText(CountrIEsList.mrn[position]);holder.text3.setText(CountrIEsList.actualstart[position]);holder.text4.setText(CountrIEsList.age[position]);holder.text5.setText(CountrIEsList.gender[position]);holder.text6.setText(CountrIEsList.wardNo[position]);holder.text7.setText(CountrIEsList.roomNo[position]);holder.text8.setText(CountrIEsList.bedno[position]);return convertVIEw;     }     static class VIEwHolder {public button btnList;public TextVIEw text8;public TextVIEw text7;public TextVIEw text6;public TextVIEw text5;public TextVIEw text4;public TextVIEw text1;public TextVIEw text2;public TextVIEw text3;     }  @OverrIDe     public voID notifyDataSetChanged()  {super.notifyDataSetChanged();      }    }
解决方法 这是ListVIEw中字母索引的简单代码,它使用2个java程序文件

1. MainActivity文件,我们处理ListvIEw的索引

2. CustomAdapter.java文件,它是BaseAdapter的子类,用于列出元素.

MainActivity.javapublic class MainActivity extends Activity implements VIEw.OnClickListener{    private Map<String,Integer> mAPIndex;    private String[] fruits;    private ListVIEw fruitsList;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        init();        fruitsList.setAdapter(new CustomAdapter(this,Arrays.asList(fruits)));        getIndexList(fruits);        displayIndex();    }    private voID init()    {        fruitsList = (ListVIEw)findVIEwByID(R.ID.List_fruits);        fruits = getResources().getStringArray(R.array.fruits_array);        Arrays.sort(fruits);    }    private voID getIndexList(String[] fruits)    {        mAPIndex = new linkedHashMap<String,Integer>();        for(int i=0; i<fruits.length; i++)        {            String fruit = fruits[i];            String index = fruit.substring(0,1);            if(mAPIndex.get(index) == null)                mAPIndex.put(index,i);        }    }    private voID displayIndex()    {        linearLayout indexLayout = (linearLayout)findVIEwByID(R.ID.sIDe_index);        List<String> indexList = new ArrayList<String>(mAPIndex.keySet());        TextVIEw textVIEw;        for(String index : indexList)        {            textVIEw = (TextVIEw) getLayoutInflater().inflate(R.layout.Alphabetindicator,null);            textVIEw.setText(index);            textVIEw.setonClickListener(this);            indexLayout.addVIEw(textVIEw);        }    }    @OverrIDe    public voID onClick(VIEw v) {        TextVIEw selectedTextVIEw  = (TextVIEw) v;        fruitsList.setSelection(mAPIndex.get(selectedTextVIEw.getText()));    }}

CustomAdaper.java

public class CustomAdapter extends BaseAdapter{    private Context context;    private List<String> ListItem;    private LayoutInflater layoutInflater;    CustomAdapter(Context context,List<String> ListItem) {        this.context = context;        this.ListItem = ListItem;    }    @OverrIDe    public VIEw getVIEw(int position,VIEwGroup parent) {        VIEwHolder vIEwHolder;        if (convertVIEw == null) {            vIEwHolder = new VIEwHolder();            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            convertVIEw = layoutInflater.inflate(R.layout.indexindicator,parent,false);            vIEwHolder.textVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.Listitem);            convertVIEw.setTag(vIEwHolder);        } else {            vIEwHolder = (VIEwHolder) convertVIEw.getTag();        }        vIEwHolder.textVIEw.setText(ListItem.get(position));        return convertVIEw;    }    static class VIEwHolder {        TextVIEw textVIEw;    }    @OverrIDe    public int getCount() {        return ListItem.size();    }    @OverrIDe    public Object getItem(int position) {        return ListItem.get(position);    }    @OverrIDe    public long getItemID(int position) {        return 0;    }}

activity_main.xml中

<linearLayout>    <ListVIEw        androID:ID="@+ID/List_fruits"        androID:layout_wIDth="0dp"        androID:layout_weight="1"        androID:layout_height="wrap_content"        androID:scrollbars="none">    </ListVIEw>    <linearLayout        androID:ID="@+ID/sIDe_index"        androID:layout_wIDth="wrap_content"        androID:layout_height="fill_parent"        androID:background="@color/white"        androID:gravity="center_horizontal"        androID:orIEntation="vertical" >    </linearLayout></linearLayout>

AlphabetIndicator.xml

<?xml version="1.0" enCoding="utf-8"?>    <TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:ID="@+ID/sIDe_List_item"        androID:layout_wIDth="wrap_content"        androID:layout_height="fill_parent"        androID:gravity="center"        androID:padding="3dp"        androID:textSize="14sp" />

IndexIndicator.xml

<linearLayout    androID:layout_height="wrap_content"    androID:layout_wIDth="match_parent"    androID:orIEntation="vertical">    <TextVIEw        androID:ID="@+ID/Listitem"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:gravity="left"        androID:padding="3dp"        androID:textcolor="#000000"        androID:textSize="14sp" /></linearLayout>

最后,在strings.xml文件中包含一个字符串数组(根据您的需要替换/增加项目)

<string-array name="fruits_array">        <item>Cantalope</item>        <item>Date</item>        <item>Grape</item>        <item>GooseBerry</item>        <item>Guava</item>        <item>Honeydew melon</item>        <item>ElderBerry</item>        <item>fig</item>        <item>Grapefruit</item></string-array>
总结

以上是内存溢出为你收集整理的android – 如何使用fastscroll和albhabet索引器实现ListView全部内容,希望文章能够帮你解决android – 如何使用fastscroll和albhabet索引器实现ListView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1127206.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存