【问题标题】:How to draw a section header in android listview just like the ios's UITableview?如何像 ios 的 UITableview 一样在 android listview 中绘制节标题?
【发布时间】:2011-10-30 07:37:06
【问题描述】:

我想在 android listview 中绘制一些部分标题,就像联系人应用程序所做的那样。 当列表视图被拖动时,节标题将平缓移动,谢谢。

【问题讨论】:

    标签: android listview header


    【解决方案1】:

    看看这个Android – Sectioned Headers in ListViews 示例,它很好地描述了如何在ListViews 中实现Sectioned Headers。

    还有

    android-amazing-listview

    Jeff Sharkey's SeparatedListAdapter

    MergeAdapter by CommonsWare

    谢谢。

    【讨论】:

    • 这些链接是否有助于在用户滚动浏览部分时将标题行固定到屏幕顶部?
    • @towpse - 嘿,我没明白你的意思?
    • @user370305 好吧,一些列表视图,如联系人列表,将部分标题行保留在列表顶部,直到下一个标题行在用户滚动时将其推出视图。
    • @towpse - 答案中没有上述链接不喜欢它。无论如何,我也对此感兴趣,所以我也会研究这个主题。如果你得到了什么,请分享它。
    • Dante 提供的链接正是这样做的。
    【解决方案2】:

    在您的列表项布局中创建一个标题布局。我们使用 VISIBILITY 选项来显示和隐藏 HEADER LAYOUT。这就像一个节标题。

    在适配器的“getView”方法中,检查“名称字段(如果您按照名称显示)”的第一个字母与前面的LIST ITEMS“名称字段”的第一个字母。如果它隐藏 HEADER LAYOUT(带有文本视图),则显示 HEADER LAYOUT,标题文本显示名称字段的第一个字母。

    这里是代码

    String nameFirstLetter = "A"; // 全局声明,而不是在 getView 内。

    // 在getView里面 String nameF = Name.slice(0,1);

     if(!nameFirstLetter.equals(nameF )){
            nameFirstLetter = nameF ;           
            holder.headerText.setText(nameFirstLetter );
            holder.headerLayout.setVisibility(View.VISIBLE);
        }else{
            holder.headerLayout.setVisibility(View.GONE);
        }
    

    这是在 Android 列表视图中显示部分标题的最简单方法,但它不会像 Iphone 部分标题那样工作,即。当我们向上/向下滚动时,部分标题会与其他列表项一起隐藏。

    【讨论】:

    • 如果列表项是可点击的,这会使标题可点击。尤其是在现代 Android 中出现了涟漪,这对用户来说现在是显而易见的。
    【解决方案3】:

    我在 Android 中找到了一些可以解决此问题的示例: 请找到有关 PinnedHeaderListView 的示例 PinnedHeaderListView Example

    【讨论】:

      【解决方案4】:

      如果有人需要不同的解决方案,尤其是那些更习惯于 iOS 开发的人,更喜欢它,或者想要模仿 iOS 的外观和感觉;我推荐以下:

      http://applidium.com/en/news/headerlistview_for_android/

      逻辑与 iOS 相同,为您完成大部分工作

      【讨论】:

      【解决方案5】:

      您可以查看SectionedMergeAdapter。如果您有多个数据子列表,您可以将它们拼接在一起并为它们设置标题。

      示例代码 -

      ListView listView = (ListView) findViewById(android.R.id.list);
      
      ArrayAdapter<Integer> adapter1 =
              new ArrayAdapter<>(this, R.layout.item_list, android.R.id.text1, arrayList1);
      ArrayAdapter<Integer> adapter2 =
              new ArrayAdapter<>(this, R.layout.item_list, android.R.id.text1, arrayList2);
      ArrayAdapter<Integer> adapter3 =
              new ArrayAdapter<>(this, R.layout.item_list, android.R.id.text1, arrayList3);
      
      TextView tv1 = new TextView(this);
      tv1.setText("Header 1");
      TextView tv2 = new TextView(this);
      tv2.setText("Header 2");
      TextView tv3 = new TextView(this);
      tv3.setText("Header 3");
      
      SectionedMergeAdapter adapter = new SectionedMergeAdapter();
      
      adapter.addSection(new SectionedMergeAdapter.Section(tv1, adapter1));
      adapter.addSection(new SectionedMergeAdapter.Section(tv2, adapter2));
      adapter.addSection(new SectionedMergeAdapter.Section(tv3, adapter3));
      
      listView.setAdapter(adapter);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-04
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-09
        • 2013-10-05
        • 1970-01-01
        相关资源
        最近更新 更多