删除listview标题和第一个项目之间的分隔符

时间:2015-11-30 14:34:31

标签: android android-listview header footer divider

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

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;
}
}

4 个答案:

答案 0 :(得分:8)

您可以按android:dividerHeight="0dp" android:divider="@null"删除所有分频器现在,对于列表视图的每个项目,您添加View width等于match_parrentheight等于{{ 1}}在底部

答案 1 :(得分:5)

    <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.

答案 2 :(得分:5)

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

List View with 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;
}

}

答案 3 :(得分:0)

可以通过使用 recyclerView.removeItemDecoration(dividerItemDecoration); .. .. .. 这基本上删除了任何种类的项目装饰。 (你们可以通过按Ctrl键并在物品装饰上的PC上单击鼠标右键来观看物品装饰)