Tuesday 6 December 2016

Three Level Exapandable ListView In Android with Two level Indicators

Java:
package zaihuishou.com.expandablerecyclerview;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import zaihuishou.com.expandablerecyclerview.loader.listener.ServiceResponseListener;
import zaihuishou.com.expandablerecyclerview.loader.manager.ServiceManager;
import zaihuishou.com.expandablerecyclerview.navcategory.FirstLevelCategory;
import zaihuishou.com.expandablerecyclerview.navcategory.NavigationGategoryBaseResponse;
import zaihuishou.com.expandablerecyclerview.navcategory.SecondLevelCategory;

public class TestActivity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    private Context context;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        context = this;
        expandableListView = (ExpandableListView) findViewById(R.id.mainList);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            expandableListView.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
        } else {
            expandableListView.setIndicatorBoundsRelative(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
        }
        addNavigationList();
    }

    public int GetPixelFromDips(float pixels) {
        // Get the screen's density scale        final float scale = getResources().getDisplayMetrics().density;
        // Convert the dps to pixels, based on density scale        return (int) ((scale * pixels + 0.5f) - 5);
    }

    public void addNavigationList() {
        ServiceManager.navigationList(getApplicationContext(), new ServiceResponseListener<NavigationGategoryBaseResponse>() {
                    @Override                    public void onSuccess(NavigationGategoryBaseResponse response) {
                        expandableListView.setAdapter(new TestActivity.ParentLevel(context, response.getMainCategories()));
                    }

                    @Override                    public void onFailure(Throwable throwable, String errorResponse) {

                    }
                }
        );
    }


    public class ParentLevel extends BaseExpandableListAdapter {

        private Context context;
        private List<FirstLevelCategory> firstLevelCategories;

        public ParentLevel(Context context, List<FirstLevelCategory> firstLevelCategoryList) {
            this.context = context;
            this.firstLevelCategories = firstLevelCategoryList;
        }

        @Override        public Object getChild(int arg0, int arg1) {
            return arg1;
        }

        @Override        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            TestActivity.SecondLevelExpandableListView secondLevelELV = new TestActivity.SecondLevelExpandableListView(TestActivity.this);
            secondLevelELV.setAdapter(new TestActivity.SecondLevelAdapter(context, firstLevelCategories.get(groupPosition).getSubGrouphs().get(childPosition)));
            secondLevelELV.setGroupIndicator(getResources().getDrawable(R.drawable.settings_selector));
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int width = metrics.widthPixels;
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
                secondLevelELV.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
            } else {
                secondLevelELV.setIndicatorBoundsRelative(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
            }
            return secondLevelELV;
        }

        @Override        public int getChildrenCount(int groupPosition) {
            return firstLevelCategories.get(groupPosition).getSubGrouphs().size();
        }

        @Override        public Object getGroup(int groupPosition) {
            return groupPosition;
        }

        @Override        public int getGroupCount() {
            return firstLevelCategories.size();
        }

        @Override        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override        public View getGroupView(int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.row_first, null);
                TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
                final ImageView icon = (ImageView) convertView.findViewById(R.id.icon);

                String name = firstLevelCategories.get(groupPosition).getCategoryname();
                if (name.equals(context.getString(R.string.Fancy))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.fancy));
                } else if (name.equals(context.getString(R.string.Women))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.women));
                } else if (name.equals(context.getString(R.string.Office))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.stationary));
                } else if (name.equals(context.getString(R.string.BooksMore))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.books));
                } else if (name.equals(context.getString(R.string.Men))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.businessman));
                } else if (name.equals(context.getString(R.string.DailyNeeds))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.calendar));
                } else if (name.equals(context.getString(R.string.Garments))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.garments));
                } else if (name.equals(context.getString(R.string.BabyKids))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.babykids));
                } else if (name.equals(context.getString(R.string.Fashion))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.fasion));
                } else if (name.equals(context.getString(R.string.Electronics))) {
                    icon.setBackground(context.getResources().getDrawable(R.drawable.elctronics));
                }
                text.setText(name);
            }
            return convertView;
        }

        @Override        public boolean hasStableIds() {
            return true;
        }

        @Override        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

    public class SecondLevelExpandableListView extends ExpandableListView {


        public SecondLevelExpandableListView(Context context) {
            super(context);
        }

        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public class SecondLevelAdapter extends BaseExpandableListAdapter {

        private Context context;
        private SecondLevelCategory secondLevelCategories;

        public SecondLevelAdapter(Context context, SecondLevelCategory secondLevelCategories) {
            this.context = context;
            this.secondLevelCategories = secondLevelCategories;
        }

        @Override        public Object getGroup(int groupPosition) {
            return groupPosition;
        }

        @Override        public int getGroupCount() {
            return 1;
        }

        @Override        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.row_third, null);
                TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
                text.setText(secondLevelCategories.getSubcategoryname());
            }
            return convertView;
        }

        @Override        public Object getChild(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.row_second, null);
                TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
                text.setText(secondLevelCategories.getThirdLevelCategoryList().get(childPosition).getLowersubcategoryname());
            }
            return convertView;
        }

        @Override        public int getChildrenCount(int groupPosition) {
            return secondLevelCategories.getThirdLevelCategoryList().size();
        }

        @Override        public boolean hasStableIds() {
            return true;
        }

        @Override        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
}

main.Xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_test"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="zaihuishou.com.expandablerecyclerview.TestActivity"><ExpandableListViewandroid:id="@+id/mainList"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffffff"android:groupIndicator="@drawable/settings_selector"android:transcriptMode="alwaysScroll"></ExpandableListView></RelativeLayout>
rowfirst.xml:

<?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="100dp"android:orientation="horizontal"android:padding="10dp"><ImageViewandroid:id="@+id/icon"android:layout_width="25dp"android:layout_height="25dp"android:layout_alignParentLeft="true" /><TextViewandroid:id="@+id/eventsListEventRowText"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_toLeftOf="@+id/plus"android:layout_toRightOf="@+id/icon"android:textColor="@android:color/background_dark"android:textSize="15sp" /></LinearLayout>
rowsecond.xml:

<?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="match_parent"android:orientation="vertical"android:padding="10dp"><TextViewandroid:id="@+id/eventsListEventRowText"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="20dp" /></LinearLayout>

rowthird.xml:
<?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="match_parent"android:orientation="vertical"android:padding="10dp"><TextViewandroid:id="@+id/eventsListEventRowText"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>


No comments:

Post a Comment

Send Whatsapp Message via PHP Code

  How to Send and Receive Messages in WhatsApp using PHP In this tutorial you will see How to Send and Receive Messages in WhatsApp using PH...