Java 对每个itemView而不是同一个rootView调用onItemClick

Java 对每个itemView而不是同一个rootView调用onItemClick,java,android,xml,Java,Android,Xml,我有一个列表视图,其中包含我想在用户点击时展开的项目,最终我实现了这一点。某种程度上。问题在于,无论点击哪个项目,最上面可见的项目都会展开,而不是用户选择的项目。似乎我需要在每个itemView上调用onimclick,而不是相同的rootView,但似乎没有任何真正的资源来描述这个过程 activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to

我有一个列表视图,其中包含我想在用户点击时展开的项目,最终我实现了这一点。某种程度上。问题在于,无论点击哪个项目,最上面可见的项目都会展开,而不是用户选择的项目。似乎我需要在每个
itemView
上调用
onimclick
,而不是相同的
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();
            }
        });


    }
@覆盖
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
ListAdapter=新的SimpleAdapter(work.this,contactList,
R.layout.list_项,新字符串[]{“email”、“description”、“name”、“country”、“phone”},
新int[]{R.id.email,R.id.description,R.id.name,R.id.county,R.id.telephone});
低压设置适配器(适配器);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//itemList=findViewById(R.id.itemView);
最终线性布局ex;
ex=findViewById(R.id.可扩展);
ex.measure(未指定视图.MeasureSpec,未指定视图.MeasureSpec);
int newHeight=ex.getMeasuredHeight()*3;
ValueAnimator slideAnimator=ValueAnimator.ofInt(0,新高度);
slideAnimator.setInterpolator(新减速插补器());
slideAnimator.setDuration(300);
slideAnimator.addUpdateListener(新值animator.AnimatorUpdateListener(){
@凌驾
动画更新上的公共无效(ValueAnimator动画){
例如:getLayoutParams().height=(int)animation.getAnimatedValue();
例如requestLayout();
}
});
slideAnimator.start();
}
});
}

感谢您抽出时间,我希望找到解决方案。

您实际上没有使用任何对单击的视图的引用。试试这个


final LinearLayout ex=view.findviewbyd(R.id.expandable)

您可以检查该方法,但我假设它使用活动
findViewById
,该活动只返回它找到的第一个实例,它将始终是列表中的第一项。
view.findViewById
将查找范围(在)该视图中的
R.id.expandable
而不是整个活动。您需要在您的
onItemClick
中设置断点,并确保您单击的项目是预期的项目。如果代码的操作以可视方式显示,但断点未被击中,请确保您处于调试模式。否则,这意味着您的方法实际上不会被调用。如果要在activities
onCreate
中设置断点,是否会被命中?我知道有时候调试器可能会遇到嵌套类的问题。尝试关闭应用程序并使用新的调试器重新启动。我会关闭当前的调试进程并启动新的调试进程。