Expandable ListView in Android



A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:context="csdevbin.arham.expandablelistview.MainActivity"
   tools:showIn="@layout/activity_main">
                                      
    <LinearLayout
        android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:orientation="vertical">

        <ExpandableListView
            android:id="@+id/expan"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />
    </LinearLayout>
</RelativeLayout>

MainActivity.java
 
package csdevbin.arham.expandablelistview;

  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.ExpandableListView;
 
  public class MainActivity extends AppCompatActivity {
    ExpandableListView expandableListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.expan);
        ExpandableListviewAdapter adapter = new ExpandableListviewAdapter(this);
        expandableListView.setAdapter(adapter);  
    }
}


ExpandableListviewAdapter.java

package csdevbin.arham.expandablelistview;

  import android.content.Context;
  import android.graphics.Color;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseExpandableListAdapter;
  import android.widget.TextView;
  import android.widget.Toast;
  public class ExpandableListviewAdapter extends BaseExpandableListAdapter {

    String[] groupNames = {"Asia", "Europe", "Africa"};
    String[][] childNames = {{"Nepal", "Bhutan", "India", "Sri Lanka"},
            {"England", "France", "Ireland"},
            {"Egypt", "Congo", "South Africa"}
    };

    Context context;
    public ExpandableListviewAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return groupNames.length;
    }

    @Override
    public int getChildrenCount(int i) {
        return childNames[i].length;
    }

    @Override
    public Object getGroup(int i) {
        return groupNames[i];
    }
    @Override
    public Object getChild(int i, int i1) {
        return childNames[i][i1];
    }
   @Override
    public long getGroupId(int i) {
        return i;
    }
    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        TextView textView = new TextView(context);
        textView.setText(groupNames[i]);
        textView.setPadding(100, 0, 0, 0);
        textView.setTextColor(Color.BLUE);
        textView.setTextSize(40);

        return textView;
    }
    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

        final TextView textView = new TextView(context);
        textView.setText(childNames[i][i1]);
        textView.setPadding(100, 0, 0, 0);
        textView.setTextColor(Color.RED);
        textView.setTextSize(30);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, textView.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        return textView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}




Comments :

Post a Comment