Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove divider between listview header and first item

From the title of my question, my problem is clear. I have a custom listView and a header and some items. Ofcourse, I have added dividers between all the items. Only thing I don't want is the divider between header and the first item. However, below code is not working.. I also want to know the exact work of this line

list.setHeaderDividerEnabled(false);

I have searched and tried a lot also visited this links but no luck..

Empty space between listview header and first item

Android listView unwanted space between header view

Thanks in advance.

Update!

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;
}
}
like image 536
Asad Avatar asked Nov 29 '22 23:11

Asad


2 Answers

You can remove all divider by android:dividerHeight="0dp" android:divider="@null" Now for each item of listview you add a View with width equal match_parrent, and height equal 1dp at bottom

like image 176
mdtuyen Avatar answered Dec 04 '22 14:12

mdtuyen


I use the property in xml "android:headerDividersEnabled="false"" , and It works fine. And If you want to custom the divider between header and first item, I suppose you could do something in the bottom of header layout to pretend it is a divider.

List View with Header

My code:

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

}
like image 28
Zhli Avatar answered Dec 04 '22 14:12

Zhli