【问题标题】:ListView with alphabets on the right, like the iPhone. Is it possible?右侧有字母的 ListView,如 iPhone。可能吗?
【发布时间】:2011-06-25 02:27:37
【问题描述】:

我想知道 Android 中的 ListView 是否可以像 iPhone ListView 的范例一样将字母放在右侧,如下所示

如果是,谁能提供示例代码。

我不是在寻找带有来自 ApisDemo 的 Alphabet 覆盖的那个,而是一个与 iPhone 范例完全相同的那个。有可能吗?

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    您可以查看此项目https://github.com/woozzu/IndexableListView。 这是截图

    【讨论】:

    • 这是一个很好的分享。我有一个问题...是否可以在所有设备上进行调整?
    • @EngrAnum:据我所知,答案是肯定的。 Scroller 缩放其大小和文本大小取决于设备。您可以签入代码public IndexScroller(Context context, ListView lv) { mDensity = context.getResources().getDisplayMetrics().density; mScaledDensity = context.getResources().getDisplayMetrics().scaledDensity;link
    • 谢谢。合并了两个答案,第一个是 @Kenny 和你的 Jul
    【解决方案2】:

    我不久前实现了类似的东西,所以我修改了我的活动,你可以看看下面,抱歉,它的评论不太好 - 希望对你有帮助!

      public class AZIndexer extends Activity {
        ListView myListView;
        ArrayList<String> elements;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // elements
            String s = "MNBVCXZLKJHGFDSAQWERTYUIOP";
            Random r = new Random();
            elements = new ArrayList<String>();
            for (int i = 0; i < 300; i++) {
                elements.add(s.substring(r.nextInt(s.length())));
            }
            Collections.sort(elements); // Must be sorted!
    
            // listview
            myListView = (ListView) findViewById(R.id.myListView);
            myListView.setFastScrollEnabled(true);
            MyAZAdapter<String> adapter = new MyAZAdapter<String>(
                    getApplicationContext(), android.R.layout.simple_list_item_1,
                    elements);
            myListView.setAdapter(adapter);
    
        }
    
        class MyAZAdapter<T> extends ArrayAdapter<T> implements SectionIndexer {
            ArrayList<String> myElements;
            HashMap<String, Integer> azIndexer;
            String[] sections;
    
            public MyAZAdapter(Context context, int textViewResourceId, List<T> objects) {
                super(context, textViewResourceId, objects);
                myElements = (ArrayList<String>) objects;
                azIndexer = new HashMap<String, Integer>(); //stores the positions for the start of each letter
    
                int size = elements.size();
                for (int i = size - 1; i >= 0; i--) {
                    String element = elements.get(i);
                    //We store the first letter of the word, and its index.
                    azIndexer.put(element.substring(0, 1), i); 
                } 
    
                Set<String> keys = azIndexer.keySet(); // set of letters 
    
                Iterator<String> it = keys.iterator();
                ArrayList<String> keyList = new ArrayList<String>(); 
    
                while (it.hasNext()) {
                    String key = it.next();
                    keyList.add(key);
                }
                Collections.sort(keyList);//sort the keylist
                sections = new String[keyList.size()]; // simple conversion to array            
                keyList.toArray(sections);
            }
    
            public int getPositionForSection(int section) {
                String letter = sections[section];
                return azIndexer.get(letter);
            }
    
            public int getSectionForPosition(int position) {
                Log.v("getSectionForPosition", "called");
                return 0;
            }
    
            public Object[] getSections() {
                return sections; // to string will be called to display the letter
            }
        }
    }
    

    以xml为:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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/myListView" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ListView>
    </LinearLayout>
    

    截图:

    【讨论】:

    • 你能发布最后的截图吗?我不明白它如何在屏幕右侧显示字母。使用 SectionIndexer,我相信它会将项目显示为叠加,对吗?
    • 我只说相似!它没有在右侧显示字母表,但我认为这并不难实现,因为我拥有的代码为您提供了每个字母开头的索引,这是您需要的。您可能需要编写自己的自定义列表视图,顺便说一句,这是 android 上最复杂的视图之一!不是想让你失望,只是看不到你这样做会得到什么,我当然不会尝试! iPhone让我:(不想复制它!
    • 哈哈。好的!我的客户快疯了,我清楚地告诉他,Android 和 iOS 是硬币的两个不同方面。唉!我讨厌这个 ListView,玩 ListView 就是在玩火。
    • 我有时会以不同的方式对列表进行排序,但之后我无法显示我的 sectionIndexer...任何想法如何管理这个?
    • 好文章。如果每一行包含一个不同的字段(意味着在我的列表中,每一行都需要包含名称、图片和另一个文本视图),如何管理这个。我已经根据名称对数组列表进行了排序。所以请给我建议。
    【解决方案3】:

    如果您愿意编写代码,这是可能的,但 AOSP 联系人应用程序中显示的快速滚动条是平台提供的方式来呈现按部分索引的列表。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多