如何将部分分隔符/分隔符添加到 ListView? [英] How to add section separators / dividers to a ListView?

查看:35
本文介绍了如何将部分分隔符/分隔符添加到 ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的应用制作菜单,使用 DrawerLayout 和 ArrayAdapter 子类来实现类似于 Facebook 抽屉菜单的内容.

I'm currently making a menu for my app, using a DrawerLayout and an ArrayAdapter subclass to achieve something looking like Facebook's drawer menu.

我目前在创建列表时没有问题,但现在看起来不错,我想在不同类型的选项(即与用户相关和与应用程序相关的选项)和顶部的搜索栏之间添加分隔符菜单.

I currently have no problems creating the list, but now that it looks good, i'd like to add separators between different kind of options (i.e. user-related and application-related options) and a search bar on top of the menu.

我当前的 ArrayAdaptor 子类的代码如下:

The code of my current ArrayAdaptor subclass is as following :

public class DrawerMenuAdapter extends ArrayAdapter<String>{
    private Context context;
    private String[] values;
    private int resId;

    public DrawerMenuAdapter(Context context, int textViewResourceId, String[] values) {
        super(context, textViewResourceId, values);
        this.context = context;
        this.values = values;
        this.resId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(this.resId, parent, false);

        TextView elementText = (TextView)rowView.findViewById(R.id.element_text);
        ImageView elementImage = (ImageView)rowView.findViewById(R.id.element_icon);
        String textValue = values[position];

        elementText.setText(textValue);

        //This switch adds the icons to the related elements
        switch (position){
            case 0:
                elementImage.setImageResource(R.drawable.search);
                break;
            case 1:
                elementImage.setImageResource(R.drawable.facebook_friends);
                break;
            case 2:
                elementImage.setImageResource(R.drawable.flirts_history);
                break;
            case 3:
                elementImage.setImageResource(R.drawable.premium);
                break;
            case 4:
                elementImage.setImageResource(R.drawable.settings);
                break;
            case 5:
                elementImage.setImageResource(R.drawable.share_app);
                break;
            case 6:
                elementImage.setImageResource(R.drawable.cgu);
                break;
        }


        return rowView;
    }
}

我假设我必须通过调用 getView 函数来覆盖填充 ListView 的函数,但我找不到它是哪个函数.

I assume that I have to override the function that populates the ListView by calling the getView function, but I can't find which function it is.

推荐答案

如果您想要在 ListView 中使用简单的部分,请查看本教程:

If you want simple sections in your ListView, take a look at this tutorial:

http://cyrilmottier.com/2011/07/05/listview-tips-tricks-2-section-your-listview/

或本教程:

http://bartinger.at/listview-with-sectionsseparators/

第二个没有那么详细,但可能更容易理解/保持简单.

基本思想是您让 ListAdapter 拥有不同种类的视图.例如两种不同的视图,其中一种是显示信息的实际列表项,另一种是部分分隔符.

The basic idea is that you make your ListAdapter have different kinds of views. For example two different Views where one kind is the actual list item displaying the information, and the other kind of View being the Section divider.

来自教程:

ListViews,更具体地说适配器可以处理多种类型的视图.如果您查看 Adapter 接口,您会注意到它包含两个特定的方法:

ListViews and more specifically Adapters can handle several types of Views. If you take a look at the Adapter interface you will notice it contains two specific methods:

  • getViewTypeCount() 返回您的视图类型的数量AdapterView 管理.大多数情况下,此方法返回 1,因为ListView 的所有项目都是相似的.在这种情况下,通过返回 2,ListView 将处理两种类型的视图:常规项目视图和分隔符视图
  • getItemViewType(int) 必须返回介于 0(含)和getViewTypeCount()(独占).给定的数字表示类型给定位置的视图.例如,我们可以确保常规项目视图的返回值为 0,常规项目视图的返回值为 1分隔符
  • getViewTypeCount() which returns the number of types of Views your AdapterView manages. Most of the time this method returns 1 because all items of the ListView are similar. In this case, by returning 2, the ListView will handle two types of Views: the regular item Views and the separator Views
  • getItemViewType(int) must return an integer between 0 (inclusive) and getViewTypeCount() (exclusive). The given number expresses the type of the View at the given position. For instance, we can ensure the returned values are 0 for the regular item Views and 1 for the separators

这篇关于如何将部分分隔符/分隔符添加到 ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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