How to expand and collapse an item in a list

I am new to Android. I want to implement a list view. It contains some list items and when clicked they should expand to show more information. But I can't find a way to do this

Here is my activity_main.xml

<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" >

    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:clickable="True"
        android:layout_alignParentTop="true"
        android:divider="#000"
        android:dividerHeight="0.5dp"
        android:padding="7dp" >
    </ListView>

</RelativeLayout>

      

listview.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="20dp"
        android:drawableLeft="@drawable/expand_icon"
        android:drawablePadding="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:visibility="gone" />
</RelativeLayout>

      

I want to fill textview2

in to show more information. Below is mymain_activity.java

public class MainActivity extends Activity {

    ListView list;
    String[] titles;
    String[] desc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources res = getResources();
        list = (ListView) findViewById(R.id.listview1);
        titles = res.getStringArray(R.array.text_info);
        desc = res.getStringArray(R.array.text_desc);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.listview, R.id.textView1, titles);
        list.setAdapter(adapter);
    }

      

strings.xml

<string name="app_name">ListView</string>
<string name="hello_world">Hello world!</string>
<string-array name="text_info">
    <item>Tutorial 1</item>
    <item>Tutorial 2</item>
    <item>Tutorial 3</item>
    <item>Tutorial 4</item>
    <item>Tutorial 5</item>
    <item>Tutorial 6</item>
    <item>Tutorial 7</item>
</string-array>

<string-array name="text_desc">
    <item>Tutorial 1</item>
    <item>Tutorial 2</item>
    <item>Tutorial 3</item>
    <item>Tutorial 4</item>
    <item>Tutorial 5</item>
    <item>Tutorial 6</item>
    <item>Tutorial 7</item>
</string-array>

<string name="action_settings">Settings</string>

      

Any suggestions on how to do this?

enter image description here

+3


source to share


2 answers


You only need to set the visibility of the second text view to View.GONE and View.VISIBLE when the user clicks on the item. Execute OnItemClickListener first. Using ExpandableListView for this simple task is overkill.

In the OnItemClick handler:



            TextView textView = (TextView)arg1.findViewById(R.id.textView2);

            if ( textView.getVisibility() == View.GONE)             
                {
                //expandedChildList.set(arg2, true);
                textView.setVisibility(View.VISIBLE);
                }
            else
                {
                //expandedChildList.set(arg2, false);
                textView.setVisibility(View.GONE);
                }

      

+3


source


You should be using ExpandableListview instead of plain ListView.

Update



Follow this tutorial and it will help you https://www.youtube.com/channel/UCbP2HeYGC3kfHjHLMPplZuQ it will teach you how to use ListView using BaseAdapter and it will increase your confidence to implement ExpandableListView yourself.

+3


source







All Articles