Search code examples
androidandroid-listviewandroid-listfragment

ListFragment missing divider


I am modifying my existing app to use ListFragment instead of ListView.

I am not able to set divider using JAVA. No exceptions are thrown. ListFragment is displayed with no dividers.

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setRetainInstance(true);
        setHasOptionsMenu(true);

        lv = getListView();  
        lv.setTextFilterEnabled(true);
        lv.setCacheColorHint(Color.TRANSPARENT);
        lv.setFastScrollEnabled(true);
        lv.setDividerHeight(1);

        lv.setDivider(getActivity().getResources().getDrawable(android.R.color.black));

        lv.setBackgroundDrawable(getResources().getDrawable(R.drawable.merge)); //white background
        lv.setScrollingCacheEnabled(false);
        lv.setSmoothScrollbarEnabled(false);    

        setListAdapter(new IconicAdapter());

      }

What am I doing wrong? You help is highly appreciated.


Solution

  • You should use lv.setDividerHeight(1) after lv.setDivider(getActivity().getResources().getDrawable(android.R.color.black));

    So Invert them:

    lv.setDivider(getActivity().getResources().getDrawable(android.R.color.black));
    lv.setDividerHeight(1);