在每个itemView上调用onItemClick而不是相同的rootView

问题描述 投票:-1回答:1

我有一个列表视图,其中包含我希望在用户点击它们时展开的项目,最终我得到了它。有点。问题是无论点击哪个项目,最顶层的可见项目都会扩展,而不是用户选择的项目。似乎我需要在每个onItemClick上调用itemView而不是相同的rootView,但似乎没有任何真实的资源描述这个过程。

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"
tools:context="com.teamplum.projectapple.Courses">

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

list_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/itemView"
android:padding="@dimen/activity_horizontal_margin"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="expand">
<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:textStyle="bold"
    android:textColor="@color/colorAccent" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:animateLayoutChanges="true"
    android:id="@+id/expandable"
    android:onClick="expand">
    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d5d"
        android:visibility="gone"
        android:onClick="expand"/>
    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d40"
        android:visibility="gone"
        android:onClick="expand"/>
    <TextView
        android:id="@+id/telephone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d40"
        android:visibility="gone"
        android:onClick="expand"/>
    <TextView
        android:id="@+id/county"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#6438"
        android:textStyle="bold"
        android:textSize="20px"
        android:visibility="gone"
        android:onClick="expand"/>
</LinearLayout>

来自work.java的相关片段:

@Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ListAdapter adapter = new SimpleAdapter(work.this, contactList,
                R.layout.list_item, new String[]{ "email","description","name","county","phone"},
                new int[]{R.id.email, R.id.description, R.id.name,R.id.county,R.id.telephone});
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //itemList = findViewById(R.id.itemView);
                final LinearLayout ex;
                ex = findViewById(R.id.expandable);
                ex.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                int newHeight = ex.getMeasuredHeight() * 3;

                ValueAnimator slideAnimator = ValueAnimator.ofInt(0, newHeight);
                slideAnimator.setInterpolator(new DecelerateInterpolator());
                slideAnimator.setDuration(300);
                slideAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        ex.getLayoutParams().height = (int) animation.getAnimatedValue();
                        ex.requestLayout();
                    }
                });
                slideAnimator.start();
            }
        });


    }

谢谢您的时间,我希望找到解决方案。

java android xml
1个回答
1
投票

您实际上并未使用对所单击视图的任何引用。试试这个。

final LinearLayout ex = view.findViewById(R.id.expandable);

© www.soinside.com 2019 - 2024. All rights reserved.