Android列表视图示例

时间:2015-02-09 09:45:50

标签: android listview

当我点击列表项时,只有第一项给我所需的回复。并单击其他项仅调用第一项的结果。为什么会这样?

list = (ListView) findViewById(R.id.list);
list.setAdapter(new Second(this));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
        if(linear.getVisibility()==View.GONE){
            linear.setVisibility(View.VISIBLE);
        }
        else {
            linear.setVisibility(View.GONE);
        }
    }
});

3 个答案:

答案 0 :(得分:0)

Single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageView
        android:src="@drawable/player1"
        android:layout_width="61dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_weight="0.07"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp" />

    <TextView
        android:layout_width="286dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="MS Dhoni"
        android:id="@+id/textView"
        android:layout_gravity="right"
        android:layout_weight="0.07"
        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="WK Batsman(C)"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView"
        android:textStyle="bold"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/linear"
        android:orientation="horizontal"
        android:layout_below="@+id/textView2"
        android:visibility="gone">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview3"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceMedium"

            />

        </LinearLayout>
</RelativeLayout>

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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:scrollbars="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

答案 1 :(得分:0)

您的findViewById()应该在点击的view中使用,否则它将始终返回找到的第一个带有给定ID的内容。 所以换行:
 LinearLayout linear=(LinearLayout)findViewById(R.id.linear)
于:
LinearLayout linear=(LinearLayout)view.findViewById(R.id.linear)

答案 2 :(得分:0)

尝试LinearLayout linear=(LinearLayout)view.findViewById(R.id.linear);