在列表视图中插入分隔线

发布于 2024-12-17 10:49:44 字数 551 浏览 4 评论 0原文

我有一个按字母顺序排序的列表,名为“songtitle”,我想在列表视图中的此位置插入分隔符。我该怎么做?

   adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle);
              int l= 0;
              while(l < adapter.getCount()-1 ){
                  if(songtitle.get(l).charAt(0) == songtitle.get(l+1).charAt(0)){
                      adapter.add(songtitle.get(l));
                     }else{
                      ///Insert Divider Here ////////        
                           l++;
                  }
                 l++;
              }

I have an alphebetically sorted list called songtitle and I want to insert divider at this location in the list view. How do I do that?

   adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle);
              int l= 0;
              while(l < adapter.getCount()-1 ){
                  if(songtitle.get(l).charAt(0) == songtitle.get(l+1).charAt(0)){
                      adapter.add(songtitle.get(l));
                     }else{
                      ///Insert Divider Here ////////        
                           l++;
                  }
                 l++;
              }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-12-24 10:49:44

覆盖ArrayAdapterView getView(intposition,ViewconvertView,ViewGroupparent),为您提供一个不同的视图(膨胀不同的布局),您希望分隔符可以在其中工作。

请参阅教程来重写 getView 方法。

希望它有帮助:)

override the View getView(int position, View convertView, ViewGroup parent) of the ArrayAdapter to give you a different View (inflate a different layout) where you want a divider might work.

See this tutorial to override the getView method.

Hope it help :)

我要还你自由 2024-12-24 10:49:44

您可以编写自定义适配器。您可以覆盖 getView、getItemViewType 和 getViewTypeCount 并为项目组标题设置不同的布局。像这样的东西:

    public class TestArrayAdapter extends ArrayAdapter<String> {
    LayoutInflater mInflater;
    private static final int
        GROUP_START = 0,
        ITEM = 1,
        COUNT = 2;

    public TestArrayAdapter(Context context, String[] objects) {
        super(context, 0, objects);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (getItemViewType(position) == GROUP_START) {
            v = mInflater.inflate(R.layout.group_start, parent, false);
            ((TextView)v.findViewById(R.id.item_group_header)).setText(
                    Character.toString(getItem(position).charAt(0)));
        } else {
            v = mInflater.inflate(R.layout.item, parent, false);
        }
        ((TextView)v.findViewById(R.id.text)).setText(getItem(position));
        return v;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return GROUP_START;
        return (getItem(position).charAt(0) == getItem(position - 1).charAt(0)) ?
                ITEM : GROUP_START; 
    }

    @Override
    public int getViewTypeCount() {
        return COUNT;
    }
}

You can write custom adapter. Than you can override getView, getItemViewType and getViewTypeCount and set different layout for items group header. Something like:

    public class TestArrayAdapter extends ArrayAdapter<String> {
    LayoutInflater mInflater;
    private static final int
        GROUP_START = 0,
        ITEM = 1,
        COUNT = 2;

    public TestArrayAdapter(Context context, String[] objects) {
        super(context, 0, objects);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (getItemViewType(position) == GROUP_START) {
            v = mInflater.inflate(R.layout.group_start, parent, false);
            ((TextView)v.findViewById(R.id.item_group_header)).setText(
                    Character.toString(getItem(position).charAt(0)));
        } else {
            v = mInflater.inflate(R.layout.item, parent, false);
        }
        ((TextView)v.findViewById(R.id.text)).setText(getItem(position));
        return v;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return GROUP_START;
        return (getItem(position).charAt(0) == getItem(position - 1).charAt(0)) ?
                ITEM : GROUP_START; 
    }

    @Override
    public int getViewTypeCount() {
        return COUNT;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文