如何添加部分分离器/分配器到ListView? [英] How to add section separators / dividers to a ListView?

查看:124
本文介绍了如何添加部分分离器/分配器到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子类的code是如下:

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:

<一个href="http://cyrilmottier.com/2011/07/05/listview-tips-tricks-2-section-your-listview/">http://cyrilmottier.com/2011/07/05/listview-tips-tricks-2-section-your-listview/

或本教程:

<一个href="http://bartinger.at/listview-with-sectionsseparators/">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 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()返回类型的意见数量的 适配器视图管理。大多数此方法返回1,因为时间 ListView中的所有项目都差不多。在这种情况下,通过返回2, 在ListView将处理两种意见:对经常项目视图 和分离视图
  • getItemViewType(INT)必须返回之间的整数 0(含) getViewTypeCount()(独家)。在给定数量的前presses类型 的视图在给定位置。例如,我们可以保证 返回的值是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天全站免登陆