如何在列表视图中展开和折叠项目

问题描述:

我对android很陌生.我想实现一个列表视图.它包含一些列表项,当单击它们时,它们应展开以显示更多信息.但我找不到解决办法

I am pretty new to android. I want to implement a list view. It contains some list items and when they are clicked, they should expand showing more information. But I unable to find a way to do that

这是我的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>

我想填充textview2以显示更多信息.以下是我的main_activity.java

I want to populate the textview2 to show more info. Below is my main_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

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?

用户单击项目时,只需将第二个文本视图的可见性设置为View.GONE和View.VISIBLE.首先实现一个OnItemClickListener.为这个简单的任务使用ExpandableListView是过分的.

You only have to set the visibility of the second textview to View.GONE and View.VISIBLE when the user clicks the item. Implement an OnItemClickListener first. Using an ExpandableListView for this simple task is overkill.

在您的OnItemClick处理程序中:

In your 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);
                }