【问题标题】:Create a UI same as default contacts design android?创建与默认联系人设计 android 相同的 UI?
【发布时间】:2014-01-14 13:56:55
【问题描述】:

我有一个由联系人组成的字符串数组,我从网络服务中获取这些数据。我使用自定义数组适配器来显示列表。我在顶部添加了一个 EditText 进行过滤,它工作正常。现在我想要 UI 右侧的字母,以便用户也可以使用字母进行过滤。我怎样才能做到这一点?

这是我的阵列适配器。

public class EntryAdapter extends ArrayAdapter<Item> implements Filterable,SectionIndexer {
    HashMap<String, Integer> alphaIndexer;
    String[] sections;
    private Context context;
    private ArrayList<Item> items;
    private ArrayList<Item> fitems;
    private LayoutInflater vi;
    private contact contact;
     private ItemsFilter mFilter;
    public EntryAdapter(Context context,ArrayList<Item> items) {
        super(context,0, items);
        this.context = context;
        this.contact=(contact) context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);alphaIndexer = new HashMap<String, Integer>();
        int size = items.size();

        for (int x = 0; x < size; x++) {
            Item s = items.get(x);
            String q=s.toString();
            // get the first letter of the store
            String ch = q.substring(0, 1);
            // convert to uppercase otherwise lowercase a -z will be sorted
            // after upper A-Z
            ch = ch.toUpperCase();
            // put only if the key does not exist
            if (!alphaIndexer.containsKey(ch))
                alphaIndexer.put(ch, x);
        }

        Set<String> sectionLetters = alphaIndexer.keySet();
        // create a list from the set to sort
        ArrayList<String> sectionList = new ArrayList<String>(
                sectionLetters);
        Collections.sort(sectionList);
        sections = new String[sectionList.size()];
        sections = sectionList.toArray(sections);
    }

@Override
public int getCount() {
    // TODO Auto-generated method stub

    return items.size();
}

@Override
public Item getItem(int position) {
    // TODO Auto-generated method stub
    return super.getItem(position);
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        final Item i = items.get(position);

        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.list_item_section, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());

            }else{
                EntryItem ei = (EntryItem)i;
                v = vi.inflate(R.layout.entrylist, null);
                final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
                final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);


                if (title != null) 
                    title.setText(ei.title);
                if(subtitle != null)
                    subtitle.setText(ei.subtitle);
            }
        }
        return v;
    }

     public Filter getFilter() {
            if (mFilter == null) {
                mFilter = new ItemsFilter();
            }
            return mFilter;

        }

     private class ItemsFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint == null || constraint.length() == 0){
                results.values = items;
                results.count = items.size();

            }
            else{

                 ArrayList<Item> itemsList = new ArrayList<Item>();

                 for (Item i : items){

                     if (i.toString().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                            itemsList.add(i);
                 }
                 results.values = itemsList;
                    results.count = itemsList.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
             if (results.count == 0){

                 notifyDataSetInvalidated();
             }
             else{

                 ArrayList<Item> lst = (ArrayList<Item>)results.values;
                 ArrayList<Item> itemsList = new ArrayList<Item>(lst);
                 //this.items=mItems;
                items =itemsList;              
                notifyDataSetChanged();
             }

        }


     }

    @Override
    public int getPositionForSection(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getSectionForPosition(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object[] getSections() {
        // TODO Auto-generated method stub
        return null;
    }

}

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以做的一件事是启用ListView 的启用快速滚动。这是public void setFastScrollEnabled (boolean enabled) 的文档。您也可以点击链接获取此类要求。 Android ListView with an alphabet scroller。 看这个链接Create easy alphabetical scrolling in ListView?

    【讨论】:

      猜你喜欢
      • 2013-02-06
      • 2015-06-02
      • 1970-01-01
      • 2021-10-11
      • 2012-08-26
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多