4

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;
}
}
Community
  • 1
  • 1
Asad
  • 1,260
  • 13
  • 19
  • it's easier for people to help you if they have some relevant **piece of code** to work on - please show us what you have tried so far – Bö macht Blau Nov 30 '15 at 14:53
  • I have added the link already I have tried on and please explain the exact work of list.setHeaderDividerEnabled(false); – Asad Nov 30 '15 at 15:27
  • setHeaderDivider**s**Enabled(boolean) "Enables or disables the drawing of the divider for header views", at least that's what the [documentation](http://developer.android.com/reference/android/widget/ListView.html#setHeaderDividersEnabled%28boolean%29) says. - I can't tell why this does not work for your app because I can't see *your code*. Something like a ["minimal, complete and verifiable example"](http://stackoverflow.com/help/mcve) would be nice. – Bö macht Blau Nov 30 '15 at 15:34
  • I have used that code for my listView; i don't think that is the reason of not working as it extends the ListView as well – Asad Nov 30 '15 at 15:43
  • can you add the solution after knowing what is the custom for my listView @0X0nosugar – Asad Nov 30 '15 at 15:53
  • Thanks for posting the code. (I simply wanted to make sure there was no simple typo at the source of the problem, happens to all of us) I've just looked into the ListView source code (grepcode.com). And started wondering: what if the header dividers are the *dividers between two or more header elements*? Up to now I always thought: header is a kind of singleton. Could you try and add some headers and do some experiments with dividers enabled/disabled? – Bö macht Blau Nov 30 '15 at 16:02
  • ok, I would do that experiment but after trying the answer of Zhli I got that my custom class is the reason of not working for list.setHeaderDividerEnabled(false); now I am searching the answer that after Override that method super class I can't get my expected result – Asad Nov 30 '15 at 16:16
  • 1. you can indeed have two or more headers and the "headerDividersEnabled" stuff is referring to dividers between them. - 2. I can't test with your class because I don't know "Onscroll.java" - 3. I think one solution for your problem is to *disable* any divider and then like some of the answers indicated you can [in the getView() of the list adapter] add your own "1dp" line at the bottom of each list row *except the last*. Which one is the last? position = adapter.getCount() - 1 – Bö macht Blau Nov 30 '15 at 16:43
  • Sorry I don't have the time to post an answer now, anybody feel free to copy what's useful to their answers from my comments. – Bö macht Blau Nov 30 '15 at 16:45
  • Onscroll.java is nothing but a simple interface to access the methods from the fragment. ok, thanks for your valuable comments. – Asad Nov 30 '15 at 16:55

4 Answers4

8

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

mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • sorry but your solution have a problem for the last item of the list as it will add the view at last – Asad Nov 30 '15 at 15:25
  • Works perfectly! @Md.Asaduzzaman you can simply check if it is the last item of the list and hide the divider View. – Mohit Singh Mar 20 '18 at 13:10
8

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

}
Er Mayank
  • 1,017
  • 2
  • 16
  • 39
Zhli
  • 380
  • 1
  • 10
  • Only a major difference from your code is the custom listView class that i have used for my listView, Please see the update above – Asad Nov 30 '15 at 15:38
  • Apparenttly, “android:headerDividersEnabled” is the right answer. – lovefish Jul 04 '16 at 05:22
4
    <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.
HourGlass
  • 1,805
  • 1
  • 15
  • 29
  • and how would this help to **REMOVE** the divider between listview header and first item? – Bö macht Blau Nov 30 '15 at 14:52
  • Rather than setting programmatically, change the same in xml file might work, just trying to help@0X0nosugar. – HourGlass Nov 30 '15 at 14:56
  • 1
    I don't know whether "false" will do the trick. But "true" simply could not be the solution :) - I think I would like to see more code in the question, the xml attribute/ method seems quite straightforward, so I wonder what is "custom" about the listview – Bö macht Blau Nov 30 '15 at 15:00
  • saw the true after you comment only bro.. :-), @0X0nosugar – HourGlass Nov 30 '15 at 15:07
  • I got the solution from your or answer. But the first one I am not sure and can you explain that how that(first one) would solve the problem.. – Asad Nov 30 '15 at 15:19
  • So the xml is set first , so it's better to set headerDividerEnabled="false" in xml than setting it through java code. As the Listview is refreshed every time you scroll through item. The java code you use to disable became not useful. As it is set only once.. @Md.Asaduzzaman – HourGlass Nov 30 '15 at 15:40
  • Ofcourse I have added headerDividerEnabled="false" to the xml first and then also tried to add by java code but neither is working – Asad Nov 30 '15 at 15:46
  • okay then I am not sure , why first one is not working..if my second one is working please mark it as answer. @Md.Asaduzzaman.. – HourGlass Nov 30 '15 at 16:09
0

This Same can be achieved by using recyclerView.removeItemDecoration(dividerItemDecoration); .. .. .. This Basically Removes any kind of item decoration. (You guys can watch item decorations by hitting ctrl + right click on PC on the item decorations)

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82