没有列表项分隔符的ListView标头

时间:2014-05-01 11:03:20

标签: java android listview user-interface android-listview

我目前正在编写一个使用带有标题的ListView的Android应用。它工作正常,但不是我想要的。 ListView中的每个项目在其顶部和底部都有1-2px分隔符。标题也是如此 - 这就是问题所在。它看起来不太漂亮......

Ugly separators between items and headers

有趣的是,系统应用程序(例如“设置”)没有这样的问题。

Settings app

这是我的示例适配器:

setListAdapter(new BaseAdapter() {
    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(i % 3 == 0 ? R.layout.list_header : android.R.layout.simple_list_item_1, viewGroup, false);
        ((TextView)v.findViewById(android.R.id.text1)).setText("test");
        return v;
    }
});

列表标题布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello, World"
    style="?android:attr/listSeparatorTextViewStyle">

</TextView>

所以问题是:如何摆脱标题和常规项之间的项分隔符,就像设置应用程序那样?

编辑: 在阅读答案后,我想澄清一件事。我想要完全删除分隔符。我想只在标题项和常规项之间删除它们。此外,像“完全删除分隔符并将其添加到某些项目上”这样的一半措施也不能满足我的要求。

4 个答案:

答案 0 :(得分:0)

您似乎必须使用自定义项目视图进行分隔和一些解决方法。让我解释一下如何管理这个:

  • 请勿使用默认分隔符remove it
  • 创建一个自定义布局,其底部为View,以作为标题的子行。
  • 在顶部创建一个View的自定义布局,以设置项目的分隔符。

然后,两种类型的分隔器将是胶水,只为头部分制作一个子线,因此分隔线应具有相同的高度以便形成良好的子线。这将避免在标题部分上方有一个分隔符,但保留项目列表的分隔符。

因此,让我展示一些实现它的代码。首先,不要忘记避免ListView上的默认分隔符:

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:dividerHeight="0dp"/>

使用顶部设置为1dp(或其他)的分隔符创建项目布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- this is the divider for items -->
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>

    <!-- and the rest of item's content in another ViewGroup -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:id="@+id/item_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:textColor="@android:color/white"/>
    </LinearLayout>
</LinearLayout>

最后,标题布局,底部有一个分隔符(与项目分隔符的高度相同):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/item_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        style="?android:attr/listSeparatorTextViewStyle"/>

    <!-- this is the ("half-")divider for header section -->
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>
    <!-- this view above will be merged with item's dividers -->
</LinearLayout>

它给出了这个结果:

答案 1 :(得分:-1)

删除您为标题TextView

设置的样式

使用所需的分隔符创建自己的自定义样式,并将其设置为TextView

  <style name="CustomListSeperatorTextViewStyle" parent="Widget.TextView.ListSeparator">
<item name="android:background">@drawable/your_own_here</item>

答案 2 :(得分:-1)

分隔符是由于您使用textview设置的样式,只需删除样式,希望这样可行。

答案 3 :(得分:-1)

我刚刚发现这些参数似乎是您所需要的,您可以尝试在ListView中添加它们:

android:headerDividersEnabled="false"
android:footerDividersEnabled="false"

文档可用here表示:

  

设置为false时,ListView不会在每个标题视图后绘制分隔符。默认值为true。

相关问题