Android - Better alternative of expandable listview

12:29:00 Dhaval Sodha Parmar 0 Comments

This is alternate solution of Expandable ListView Android. 




Your Main Activity Class:

 package com.example.my_demo;  
 import java.util.ArrayList;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.ViewGroup;  
 import android.widget.AdapterView;  
 import android.widget.AdapterView.OnItemClickListener;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.ListAdapter;  
 import android.widget.ListView;  
 import android.widget.Toast;  
 public class MainActivity extends Activity {  
      private Button btn1;  
      private Button btn2;  
      private ListView list1;  
      ArrayList<String> a = new ArrayList<String>();  
      public boolean LI1 = false;  
      public boolean LI2 = false;  
      public boolean LI3 = false;  
      private ArrayAdapter<String> listAdapter;  
      private ListView list2;  
      private Button btn3;  
      private ListView list3;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           a.add(" Android");  
           a.add(" iPhone");  
           a.add(" Dhaval Sodha Parmar");  
           btn1 = (Button) findViewById(R.id.button1);  
           btn2 = (Button) findViewById(R.id.button2);  
           btn3 = (Button) findViewById(R.id.button3);  
           list1 = (ListView) findViewById(R.id.listView1);  
           list2 = (ListView) findViewById(R.id.listView2);  
           list3 = (ListView) findViewById(R.id.listView3);  
           // listAdapter = new ArrayAdapter<String>(this,  
           // android.R.layout.simple_list_item_1, a);  
           listAdapter = new ArrayAdapter<String>(this, R.layout.childrow, a);  
           list1.setAdapter(listAdapter);  
           btn1.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     if (!LI1) {  
                          list1.setAdapter(listAdapter);  
                          Utility.setListViewChild(list1);  
                          list1.setVisibility(ListView.VISIBLE);  
                          LI1 = true;  
                     } else {  
                          list1.setVisibility(ListView.GONE);  
                          LI1 = false;  
                     }  
                }  
           });  
           btn2.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     if (!LI2) {  
                          list2.setAdapter(listAdapter);  
                          Utility.setListViewChild(list2);  
                          list2.setVisibility(ListView.VISIBLE);  
                          LI2 = true;  
                     } else {  
                          list2.setVisibility(ListView.GONE);  
                          LI2 = false;  
                     }  
                }  
           });  
           btn3.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     if (!LI3) {  
                          list3.setAdapter(listAdapter);  
                          Utility.setListViewChild(list3);  
                          list3.setVisibility(ListView.VISIBLE);  
                          LI3 = true;  
                     } else {  
                          list3.setVisibility(ListView.GONE);  
                          LI3 = false;  
                     }  
                }  
           });  
           list1.setOnItemClickListener(new OnItemClickListener() {  
                @Override  
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                          long arg3) {  
                     // TODO Auto-generated method stub  
                     Toast.makeText(MainActivity.this,  
                               "You Select : " + a.get(arg2), Toast.LENGTH_LONG)  
                               .show();  
                }  
           });  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.activity_main, menu);  
           return true;  
      }  
      public static class Utility {  
           public static void setListViewChild(ListView list) {  
                ListAdapter listadapter = list.getAdapter();  
                if (listadapter == null) {  
                     return;  
                }  
                int totalHight = 0;  
                for (int i = 0; i < listadapter.getCount(); i++) {  
                     View listitem = listadapter.getView(i, null, list);  
                     listitem.measure(0, 0);  
                     totalHight += listitem.getMeasuredHeight();  
                }  
                ViewGroup.LayoutParams params = list.getLayoutParams();  
                params.height = totalHight  
                          + (list.getDividerHeight() * (listadapter.getCount() - 1));  
                list.setLayoutParams(params);  
           }  
      }  
 }  

Activity_main.xml

 <?xml version="1.0" encoding="UTF-8"?>  
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/scrollView1"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:fadeScrollbars="false" >  
   <LinearLayout  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent" >  
     <LinearLayout  
       android:layout_width="fill_parent"  
       android:layout_height="fill_parent"  
       android:orientation="vertical" >  
       <Button  
         android:id="@+id/button1"  
         android:textSize="26dp"  
         android:layout_width="match_parent"  
         android:layout_height="60dp"  
         android:background="#345ea8"  
         android:gravity="left|center"  
         android:text="list one" />  
       <ListView  
         android:id="@+id/listView1"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:visibility="gone" >  
       </ListView>  
       <Button  
         android:id="@+id/button2"  
         android:textSize="26dp"  
         android:gravity="left|center"  
         android:layout_width="match_parent"  
         android:layout_height="60dp"  
         android:background="#345ea8"  
         android:text="list two" />  
       <ListView  
         android:id="@+id/listView2"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:visibility="gone" >  
       </ListView>  
       <Button  
         android:id="@+id/button3"  
         android:textSize="26dp"  
         android:gravity="left|center"  
         android:layout_width="match_parent"  
         android:layout_height="60dp"  
         android:background="#345ea8"  
         android:text="list three" />  
       <ListView  
         android:id="@+id/listView3"  
         android:layout_width="match_parent"  
         android:layout_height="40dp"  
         android:visibility="gone" >  
       </ListView>  
       <View  
         android:layout_width="match_parent"  
         android:layout_height="match_parent" />  
     </LinearLayout>  
   </LinearLayout>  
 </ScrollView>  


childrow.xml

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/textView1"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:background="#c7e8fa"  
   android:text="Medium Text"  
   android:gravity="left|center"  
   android:textAppearance="?android:attr/textAppearanceSmall"  
   android:textColor="#345ea8"  
   android:textSize="26dp" />  

manifest file

 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.example.my_demo"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="16" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.example.my_demo.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  


Enjoy Coding...... 

0 comments: