Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Divider between the last item of the child and next Groupheader expandableListview

I have implemented expandablelistview in android app and also implemented divider in it.

I have one problem in not getting the divider between the last item of the child and next group header.

Following image is how I want :

enter image description here

But following is what I'm getting:

enter image description here

Here if you compare both images the divider between the CID and About Set is not coming how to implement that divider ?

Also the groupIndicator is not changing inspite of providing the xml in the groupindicator containing the item tag with 2 different images in android:state_expanded and android:state_empty.

But the property of android:state_expanded and android:state_empty doesn't appear.

like image 273
Niranjan Balkrishna Prajapati Avatar asked Dec 30 '13 10:12

Niranjan Balkrishna Prajapati


1 Answers

It's very easy to Implement.
1- We will add divider in groub_item_layout and child_item_layout:
group_item_layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:minHeight="50dp"
    android:orientation="vertical">

    <View
        android:id="@+id/adapter_divider_top"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#B6B6B6" />

    <TextView
        android:id="@+id/tv_adapter_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingBottom="10dp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:text="Home"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/primary_text" />

</LinearLayout>


child_item_layout :

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

    <TextView
        android:id="@+id/tv_adapter_desc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:text="description"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#212121" />

    <View
        android:id="@+id/adapter_divider_bottom"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divider" />
</LinearLayout>


2- In your Adapter getChildView() and getGroupView() Methods:

 @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup) {

        if (view == null) {

            view = mInflater.inflate(R.layout.group_item_layout, null);

            mGroupViewHolder = new GroupViewHolder();

            mGroupViewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_adapter_title);

            mGroupViewHolder.dividerTop = view.findViewById(R.id.adapter_divider_top);

            view.setTag(mGroupViewHolder);

        } else {

            mGroupViewHolder = (GroupViewHolder) view.getTag();

        }

        // That if you want to show divider in expanded group only.
        // If you want to show divider in all group items remove this block.
        if (isExpanded) {

            mGroupViewHolder.dividerTop.setVisibility(View.VISIBLE);

        } else {

            mGroupViewHolder.dividerTop.setVisibility(View.INVISIBLE);

        }
        // That if you want to show divider in expanded group only.

        String myTitle = getGroup(groupPosition);

        if(myTitle != null) {

            mGroupViewHolder.tvTitle.setText(myTitle);

        }

        return view;
    }


@Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {


        if (view == null) {

            view = mInflater.inflate(R.layout.child_item_layout, null);

            mChildViewHolder = new ChildViewHolder();

            mChildViewHolder.tvDesc = (TextView) view.findViewById(R.id.tv_adapter_desc);

            mChildViewHolder.dividerBottom = view.findViewById(R.id.adapter_divider_bottom);

            view.setTag(mChildViewHolder);

        } else {

            mChildViewHolder = (ChildViewHolder) view.getTag();

        }

        if(isLastChild) {

            mChildViewHolder.dividerBottom.setVisibility(View.VISIBLE);

        } else {

            mChildViewHolder.dividerBottom.setVisibility(View.INVISIBLE);

        }

        Timesheet timesheet = getChild(groupPosition, childPosition);

        if(timesheet != null) {

            mChildViewHolder.tvDesc.setText(timesheet.getDescription());

        }


        return view;
    }

Hope this helps anyone.

like image 120
Ibrahim Disouki Avatar answered Oct 22 '22 17:10

Ibrahim Disouki