Saturday 1 July 2017

Implement the Custom Expandable ListView in Android.


Creating New Project.

Open android studio and create a new project.


File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.


First we need to add Library to our project.

compile 'com.mcxiaoke.volley:library-aar:1.0.0'


Create Xml file in project.

Open => app => res => layout - activity_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:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

    <ExpandableListView

        android:id="@+id/exp_list"

        android:layout_width="match_parent"

        android:layout_height="fill_parent" >

    </ExpandableListView>

    

</RelativeLayout>


Open => app => res => layout - child_item.xml.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_marginLeft="50dp"

    android:paddingBottom="4dp"

    android:paddingTop="4dp" >

    <TextView

        android:id="@+id/country_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="TextView"

        android:textSize="20dp" />

    

</RelativeLayout>

Open => app => res => layout - group_item.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" >

    <TextView

        android:id="@+id/group_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:paddingLeft="25dp"

        android:textSize="25dp" />

</LinearLayout>


Create the Java file in project.

Open app => main => src = MainActivity.java


public class MainActivity extends Activity {

    String url = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";

    ProgressDialog dialog;

    private ExpandListAdapter ExpAdapter;

    private ExpandableListView ExpandList;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ExpandList = (ExpandableListView) findViewById(R.id.exp_list);

        dialog = new ProgressDialog(this);

        dialog.setMessage("Loading.....");

        dialog.setCancelable(false);

        getSpinnerData();

    }

    //Get the country data here...

    private void getSpinnerData() {

        dialog.show();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,

                null, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject response) {

                ArrayList<Group> list = new ArrayList<Group>();

                ArrayList<Child> ch_list;

                try {

                    Iterator<String> key = response.keys();

                    while (key.hasNext()) {

                        String k = key.next();

                        Group gru = new Group();

                        gru.setName(k);

                        ch_list = new ArrayList<Child>();

                        JSONArray ja = response.getJSONArray(k);

                        for (int i = 0; i < ja.length(); i++) {

                            JSONObject jo = ja.getJSONObject(i);

                            Child ch = new Child();

                            ch.setName(jo.getString("population"));

                            ch_list.add(ch);

                        } // for loop end

                        gru.setItems(ch_list);

                        list.add(gru);

                    } // while loop end

                    ExpAdapter = new ExpandListAdapter(MainActivity.this, list);

                    ExpandList.setAdapter(ExpAdapter);

                    dialog.dismiss();

                } catch (JSONException e) {

                    e.printStackTrace();

                }

            }

        },

                new Response.ErrorListener() {

                    @Override

                    public void onErrorResponse(VolleyError error) {

                        dialog.dismiss();

                    }

                });

        //Creating a request queue

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue

        requestQueue.add(jsonObjReq);

    }

    //**********END**

}


Create the Java file in project.

Open app => main => src = ExpandListAdapter.java


public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;

    private ArrayList<Group> groups;

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {

        this.context = context;

        this.groups = groups;

    }

    @Override

    public Object getChild(int groupPosition, int childPosition) {

        ArrayList<Child> chList = groups.get(groupPosition).getItems();

        return chList.get(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) {

        Child child = (Child) getChild(groupPosition, childPosition);

        if (convertView == null) {

            LayoutInflater infalInflater = (LayoutInflater) context

                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);

            convertView = infalInflater.inflate(R.layout.child_item, null);

        }

        TextView tv = (TextView) convertView.findViewById(R.id.country_name);

        tv.setText(child.getName().toString());

        return convertView;

    }

    @Override

    public int getChildrenCount(int groupPosition) {

        ArrayList<Child> chList = groups.get(groupPosition).getItems();

        return chList.size();

    }

    @Override

    public Object getGroup(int groupPosition) {

        return groups.get(groupPosition);

    }

    @Override

    public int getGroupCount() {

        return groups.size();

    }

    @Override

    public long getGroupId(int groupPosition) {

        return groupPosition;

    }

    @Override

    public View getGroupView(int groupPosition, boolean isExpanded,

                             View convertView, ViewGroup parent) {

        Group group = (Group) getGroup(groupPosition);

        if (convertView == null) {

            LayoutInflater inf = (LayoutInflater) context

                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);

            convertView = inf.inflate(R.layout.group_item, null);

        }

        TextView tv = (TextView) convertView.findViewById(R.id.group_name);

        tv.setText(group.getName());

        return convertView;

    }

    @Override

    public boolean hasStableIds() {

        return true;

    }

    @Override

    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return true;

    }

}


Create the Java file in project.

Open app => main => src = Group.java


public class Group {

    private String Name;

    private ArrayList<Child> Items;

    public String getName() {

        return Name;

    }

    public void setName(String name) {

        this.Name = name;

    }

    public ArrayList<Child> getItems() {

        return Items;

    }

    public void setItems(ArrayList<Child> Items) {

        this.Items = Items;

    }

}

Create the Java file in project.

Open app => main => src = Child.java


public class Child {

    private String Name;

    private String Image;

    public String getName() {

        return Name;

    }

    public void setName(String Name) {

        this.Name = Name;

    }

    public String getImage() {

        return Image;

    }

    public void setImage(String Image) {

        this.Image = Image;

    }

}


Add Internet permission in your manifest.

<uses-permission android:name="android.permission.INTERNET" />

No comments:

Post a Comment