如联系人应用程序列表视图的Andr​​oid字母部分列表视图 [英] Android alphabets section Listview like contacts app listview

查看:102
本文介绍了如联系人应用程序列表视图的Andr​​oid字母部分列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在任何一个可以帮助我如何看我的看法是这样的。请帮助我,我太混乱了。

我在这里的code:-------

phonebooklistview.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    <的EditText
        机器人:ID =@ + ID / searchTxtBox
        机器人:layout_width =match_parent
        机器人:layout_height =40dp
        机器人:layout_marginTop =65dp
        机器人:提示=@字符串/ searchHintTxt
        机器人:单线=真
        机器人:drawableLeft =@机器人:可绘制/ ic_search_category_default
        机器人:drawablePadding =0dp
        机器人:文本=/>

    <的ListView
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:fadingEdge =垂直
        机器人:fastScrollEnabled =真
        机器人:填充=2DP
        机器人:layout_below =@ + ID / searchTxtBox>
    < / ListView控件>

    <的TextView
        机器人:ID =@ + ID / phoneBookEmptyView
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=center_horizo​​ntal
        机器人:layout_gravity =center_horizo​​ntal
        机器人:文本=@字符串/ phoneBookEmptyMsg
        机器人:文字颜色=@色/白
        机器人:layout_below =@ + ID / searchTxtBox
        机器人:layout_marginTop =5DP
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

< / RelativeLayout的>
 

和在PhoneBookList.java文件

 保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.phonebooklistview);

    ListView控件= getListView();
    适配器=新ItemsAdapter(本);
    adapter.notifyDataSetChanged();
    listView.setAdapter(适配器);
    listView.setTextFilterEnabled(真正的);
    //listView.setFastScrollEnabled(true);
    listView.setOnItemLongClickListener(本);
    listView.setItemsCanFocus(假);
    listView.setEmptyView(findViewById(R.id.phoneBookEmptyView));
    registerForContextMenu(ListView控件);

}



私有类ItemsAdapter扩展了BaseAdapter实现SectionIndexer
{
    HashMap的<字符串,整数> alphaIndexer;
    的String []段;
      私人LayoutInflater充气;
      的String [] []项目;

      公共ItemsAdapter(上下文的背景下)
      {
          充气= LayoutInflater.from(上下文);
          this.items = phoneBookDataArr;

            alphaIndexer =新的HashMap<字符串,整数>();
            INT大小= items.length;

            为(中间体X = 0 X  - 其中;大小; X ++)
            {
                字符串名称=项[X] [2];
                串CH = name.substring(0,1);
                CH = ch.toUpperCase();
                alphaIndexer.put(CH,X);

                Log.e(TAG,alphaIndexer =+ CH);
            }

            设置<字符串> sectionLetters = alphaIndexer.keySet();
            ArrayList的<字符串> sectionList =新的ArrayList<字符串>(sectionLetters);
            Collections.sort(sectionList);
            部分=新的String [sectionList.size()];
            sectionList.toArray(段);
      }

      // @覆盖
      公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
      {
    //这里我的自定义listviewcontentview去。
      }

      公众诠释getCount将()
      {
          返回items.length;
      }

      公共对象的getItem(INT位置)
      {
          返回的位置;
      }

      众长getItemId(INT位置)
      {
          返回0;
      }

      公众诠释getPositionForSection(INT部分)
      {
          Log.e(TAG,getPositionForSection =+部分);
          返回alphaIndexer.get(第[节]);
      }

      公众诠释getSectionForPosition(INT位置)
      {
          Log.e(TAG,getSectionForPosition =+位置);
          返回1;
      }

      公共对象[] getSections()
      {
          Log.e(TAG,getSections =+ sections.length);
          对于(字符串str:部分)
          {
            Log.e(TAG,STR);
          }
          返回段;
      }
}
 

解决方案

添加一个搜索栏

1:使用的EditText的搜索栏
2:它注册addTextChangedListener
3:实现onTextChanged(...)方法来跟踪用户4中输入的字符序列:取使用gettext()方法currentSearchName。遍历联系人列表来查找匹配的搜索。存储匹配的结果的列表,并使用内容适配器来显示列表。

具体实施将在下面的链接
提供 <一href="http://www.androidpeople.com/android-listview-searchbox-sort-items">http://www.androidpeople.com/android-listview-searchbox-sort-items

添加标签和侧边栏:

1:要添加在你的ContentAdapter类
一个工具实现SectionIndexer 2:遍历联系人列表来获取所有名称的缩写
3:实现setSection(..)方法来设置标签
4:实施getPositionForSection(..)去标签对应的首字母,当用户点击侧边栏

侧栏的实施可以在下面的链接中找到。 <一href="http://$c$clikes.blogspot.com/2012/04/android-alphabet-listview-like-contacts.html">http://$c$clikes.blogspot.com/2012/04/android-alphabet-listview-like-contacts.html?

now can any one help me how to look my view something like this. please help me i am too confusing.

here my code:-------

phonebooklistview.xml

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

    <EditText
        android:id="@+id/searchTxtBox"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="65dp"
        android:hint="@string/searchHintTxt"
        android:singleLine="true"
        android:drawableLeft="@android:drawable/ic_search_category_default"
        android:drawablePadding="0dp"
        android:text="" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="vertical"
        android:fastScrollEnabled="true"
        android:padding="2dp"
        android:layout_below="@+id/searchTxtBox" >
    </ListView>

    <TextView
        android:id="@+id/phoneBookEmptyView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"
        android:text="@string/phoneBookEmptyMsg"
        android:textColor="@color/white"
        android:layout_below="@+id/searchTxtBox"
        android:layout_marginTop="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

and in PhoneBookList.java file

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.phonebooklistview);

    listView = getListView();
    adapter = new ItemsAdapter(this);
    adapter.notifyDataSetChanged();
    listView.setAdapter(adapter);
    listView.setTextFilterEnabled(true);
    //listView.setFastScrollEnabled(true);
    listView.setOnItemLongClickListener(this);
    listView.setItemsCanFocus(false);
    listView.setEmptyView(findViewById(R.id.phoneBookEmptyView));
    registerForContextMenu(listView);

}



private class ItemsAdapter extends BaseAdapter implements SectionIndexer
{
    HashMap<String, Integer> alphaIndexer;
    String[] sections;
      private LayoutInflater inflater;
      String[][] items;

      public ItemsAdapter(Context context)
      {
          inflater = LayoutInflater.from(context);
          this.items = phoneBookDataArr;

            alphaIndexer = new HashMap<String, Integer>();
            int size = items.length;

            for (int x = 0; x < size; x++)
            {
                String name=items[x][2];
                String ch =  name.substring(0, 1);
                ch = ch.toUpperCase();
                alphaIndexer.put(ch, x);

                Log.e(TAG,"alphaIndexer="+ch);
            }

            Set<String> sectionLetters = alphaIndexer.keySet();
            ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); 
            Collections.sort(sectionList);
            sections = new String[sectionList.size()];
            sectionList.toArray(sections);
      }

      //@Override
      public View getView(final int position, View convertView, ViewGroup parent)
      {
    // here my custom listviewcontentview goes.
      }

      public int getCount()
      {
          return items.length;
      }

      public Object getItem(int position)
      {
          return position;
      }

      public long getItemId(int position)
      {
          return 0;
      }

      public int getPositionForSection(int section)
      {
          Log.e(TAG,"getPositionForSection="+section);
          return alphaIndexer.get(sections[section]);
      }

      public int getSectionForPosition(int position)
      {
          Log.e(TAG,"getSectionForPosition="+position);
          return 1;
      }

      public Object[] getSections()
      {
          Log.e(TAG,"getSections="+sections.length);
          for (String str : sections)
          {
            Log.e(TAG,str);
          }
          return sections;
      }
}

解决方案

Adding a searchbar

1: Use EditText for the search bar
2: Register it to addTextChangedListener
3: Implement onTextChanged(...) method to keep track of the character sequence entered by the user 4: Fetch the currentSearchName using getText() method. Iterate over the contact list to find the matching searches. Store the matched results in a list and use Content Adapter to display the list.

Detailed implementation is provided in the following link
http://www.androidpeople.com/android-listview-searchbox-sort-items

Adding labels and sidebar:

1: To add a sidebar implement SectionIndexer in your ContentAdapter class
2: Iterate over the Contacts list to fetch the initials of all the names
3: Implement setSection(..) method to the set the labels
4: Implement getPositionForSection(..) to go the label corresponding to initials when user clicks on the sidebar.

Implementation of the SideBar can be found on the following link. http://codelikes.blogspot.com/2012/04/android-alphabet-listview-like-contacts.html?

这篇关于如联系人应用程序列表视图的Andr​​oid字母部分列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆