删除列表视图 header 和第一项之间的分隔线

Remove divider between listview header and first item

从我的问题标题来看,我的问题很明确了。我有一个自定义 listView 和一个 header 以及一些项目。当然,我在所有项目之间添加了分隔线。我唯一不想要的是 header 和第一项之间的分隔符。 但是,下面的代码不起作用。我也想知道这一行的确切工作

list.setHeaderDividerEnabled(false);

我已经搜索并尝试了很多也访问了这个链接,但没有运气..

Empty space between listview header and first item

Android listView unwanted space between header view

提前致谢。

更新!

public class ListView extends android.widget.ListView {

private OnScrollListener onScrollListener;
private Onscroll onscrollObj;

public ListView(Context context) {
    super(context);
    onCreate(context, null, null);
}

public ListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    onCreate(context, attrs, null);
}

public ListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    onCreate(context, attrs, defStyle);
}

@SuppressWarnings("UnusedParameters")
private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
    setListeners();
}

private void setListeners() {
    super.setOnScrollListener(new OnScrollListener() {

        private int oldTop;
        private int oldFirstVisibleItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (onScrollListener != null) {
                onScrollListener.onScrollStateChanged(view, scrollState);
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (onScrollListener != null) {
                onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }

            if (onscrollObj != null) {
                onDetectedListScroll(view, firstVisibleItem);
            }
        }

        private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem) {
            View view = absListView.getChildAt(0);
            int top = (view == null) ? 0 : view.getTop();

            if (firstVisibleItem == oldFirstVisibleItem) {
                if (top > oldTop) {
                    onscrollObj.onUpScrolling();
                } else if (top < oldTop) {
                    onscrollObj.onDownScrolling();
                }
            } else {
                if (firstVisibleItem < oldFirstVisibleItem) {
                    onscrollObj.onUpScrolling();
                } else {
                    onscrollObj.onDownScrolling();
                }
            }

            oldTop = top;
            oldFirstVisibleItem = firstVisibleItem;
        }
    });
}

@Override
public void setOnScrollListener(OnScrollListener onScrollListener) {
    this.onScrollListener = onScrollListener;
}

public void setOnDetectScrollListener(Onscroll onDetectScrollListener) {
    this.onscrollObj = onDetectScrollListener;
}
}
    <ListView
    android:headerDividersEnabled="false"
    android:dividerHeight="1dp"
    />          

    or do this, 

don't add a listview.headerview(view);
instead, add a header view in the xml design and do like this
<LinearLayout>
<layout header design>
<Listview>
</LinearLayout>          
Don't provide space between header and Listview.

您可以通过 android:dividerHeight="0dp" android:divider="@null" 删除所有分隔符现在为列表视图的每个项目添加一个 View,其中 width 等于 match_parrentheight 等于1dp 在底部

我在 xml "android:headerDividersEnabled="false"" 中使用 属性 ,它工作正常。如果你想自定义 header 和第一项之间的分隔线,我想你可以在 header 布局的底部做一​​些事情来假装它是一个分隔线。

我的代码:

Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<ListView
    android:headerDividersEnabled="false"
    android:dividerHeight="5dp"
    android:divider="@color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_list_view"/>


</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3px"
    />
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        />
</LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
@Bind(R.id.my_list_view)
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    initviews();
}

private void initviews() {
    View view = View.inflate(this,R.layout.headerview,null);
    listView.addHeaderView(view);
    SimpleAdapter adapter = new SimpleAdapter(this, getData(),
            R.layout.list_item, new String[] { "img", "title", "info" },
            new int[] { R.id.img, R.id.title, R.id.info });
    listView.setAdapter(adapter);

}


private List<Map<String, Object>> getData() {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("img", R.mipmap.ic_launcher);
    map.put("title", "php");
    map.put("info", "for server");
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("img",  R.mipmap.ic_launcher);
    map.put("title", "java");
    map.put("info", "stable");
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("img",  R.mipmap.ic_launcher);
    map.put("title", "C++");
    map.put("info", "cool and hard");
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("img",  R.mipmap.ic_launcher);
    map.put("title", "python");
    map.put("info", "pretty clean");
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("img",  R.mipmap.ic_launcher);
    map.put("title", "hello");
    map.put("info", "every thing");
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("img",  R.mipmap.ic_launcher);
    map.put("title", "world");
    map.put("info", "hello world");
    list.add(map);

    return list;
}

}

这同样可以通过使用来实现 recyclerView.removeItemDecoration(dividerItemDecoration); .. .. .. 这基本上消除了任何种类的物品装饰。 (你们可以通过点击 ctrl + 右击 PC 上的物品装饰来观看物品装饰)