ExpandableListView重复ID问题-如何解决?

时间:2018-10-06 10:36:58

标签: android expandablelistview

我正在按照这个很棒的教程(https://www.youtube.com/watch?v=c4XagtQlqS0)来制作Expandable ListView。我遇到的问题是当我想“添加”第二个可扩展文本时。我得到的错误是 “此处最初定义的重复ID @ + id / expandable_text”和“已经在此布局中较早定义的重复ID @ + id / expand_collapse” 。这是一个逻辑错误,但是在本教程中没有显示,所以我的代码有问题吗?

下面您可以找到我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".Body1">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expandable_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxCollapsedLines="1"
        app:animDuration="200"
        >

        <TextView
            android:id="@+id/expandable_text"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="16dp"
            android:textColor="#666"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@+id/expand_collapse"
            android:layout_gravity="right|bottom"
            android:background="@android:color/transparent"
            android:padding="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </com.ms.square.android.expandabletextview.ExpandableTextView>

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expandable_text_view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxCollapsedLines="1"
        app:animDuration="200"
        >

        <TextView
            android:id="@+id/expandable_text"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="16dp"
            android:textColor="#666"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@+id/expand_collapse"
            android:layout_gravity="right|bottom"
            android:background="@android:color/transparent"
            android:padding="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </com.ms.square.android.expandabletextview.ExpandableTextView>
</ScrollView>


</LinearLayout>

还有Java文件:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ms.square.android.expandabletextview.ExpandableTextView;

public class Body1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body1);
        ExpandableTextView  expandableTextView1 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
        expandableTextView1.setText(getString(R.string.text1));

        ExpandableTextView  expandableTextView2 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
        expandableTextView2.setText(getString(R.string.text2));

    }
}

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

您为textview和imagebutton使用的ID相同,因此在定义布局视图时需要使用唯一的ID。

如下更改代码

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expandable_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxCollapsedLines="1"
        app:animDuration="200"
        >

        <TextView
            android:id="@id/expandable_text"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="16dp"
            android:textColor="#666"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@id/expand_collapse"
            android:layout_gravity="right|bottom"
            android:background="@android:color/transparent"
            android:padding="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </com.ms.square.android.expandabletextview.ExpandableTextView>

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expandable_text_view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxCollapsedLines="1"
        app:animDuration="200"
        >

        <TextView
            android:id="@id/expandable_text"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="16dp"
            android:textColor="#666"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@id/expand_collapse"
            android:layout_gravity="right|bottom"
            android:background="@android:color/transparent"
            android:padding="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </com.ms.square.android.expandabletextview.ExpandableTextView>
</ScrollView>

答案 1 :(得分:0)

let navVC = UINavigationController.init(rootViewController: YourFirstViewControllerObject()) navVC.pushViewController(NewViewControllerObj(), animated: true) 用于第二个可扩展的文本视图。

R.id.expandable_text_view2

答案 2 :(得分:0)

在您的布局中,有2个具有相同ID的ImageButton。首先,更改第二个ID:

let aboutVC = AboutVC()
self.navigationController?.pushViewController(aboutVC, animated: true)

在Java代码中,您声明了相同的ExpandableTextView:

$ cat >/etc/udev/rules.d/20-gpiomem.rules <<EOF
SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio",
MODE="0660" EOF

因此您需要将expandableTextView2更改为:

<ImageButton
        android:id="@+id/expand_collapse_two"
        android:layout_gravity="right|bottom"
        android:background="@android:color/transparent"
        android:padding="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

最后,您的布局应如下所示:

ExpandableTextView  expandableTextView1 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
ExpandableTextView  expandableTextView2 = (ExpandableTextView) findViewById(R.id.expandable_text_view);

以及您的Java代码:

 ExpandableTextView  expandableTextView2 = (ExpandableTextView) findViewById(R.id.expandable_text_view2);