在 CursorAdapter 中处理按钮点击

Handle Button Clicks in CursorAdapter

我想在我的 ListView 中实现 View 的下拉和总结 正在从数据库中获取数据并使用游标适配器,我已尝试为 onClickListner 添加onItemClickListner 中的按钮,但没有成功。
我面临的问题是当单击按钮 DROPDOWN 时它会使 relative layout 可见但是当再次单击时它不会 relative layoutGONE
[可见=19=]

这是我的 CursorAdapter 代码

public class ProductViewCursorAdapter extends CursorAdapter {
    private CardView cv_singleItem;
    private RelativeLayout infoLayout;
    private Button dropDown;
    public ProductViewCursorAdapter(Context context, Cursor c) {
        super(context, c, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.custom_product_view_listview,parent,false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        cv_singleItem = view.findViewById(R.id.cv_custom_single_product);
        infoLayout = view.findViewById(R.id.layout_dropdown_relative);
        dropDown = view.findViewById(R.id.expand_user_info_listView);
        dropDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (infoLayout.getVisibility() == View.GONE){
                    TransitionManager.beginDelayedTransition(cv_singleItem,new AutoTransition());
                    infoLayout.setVisibility(View.VISIBLE);
                    dropDown.setBackgroundResource(R.drawable.ic_sumup);
                }else{
                    Log.i("THIS IS TEST","DEMO VIEW "+ infoLayout.getVisibility());
                    TransitionManager.beginDelayedTransition(cv_singleItem,new AutoTransition());
                    infoLayout.setVisibility(View.GONE);
                    dropDown.setBackgroundResource(R.drawable.ic_dropdown);
                }
            }
        });

    }
}

ListView单View XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:id="@+id/cv_custom_single_product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TextAppearance.AppCompat.Title">
        <LinearLayout
            android:background="#D5000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RelativeLayout
                android:id="@+id/image_layout_main"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/iv_product_image_listView"
                    android:layout_width="match_parent"
                    android:scaleType="fitCenter"
                    android:src="@drawable/selimage"
                    android:layout_height="200dp"/>
                <TextView
                    android:id="@+id/tv_no_image_available"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="No Image Available"
                    android:textAlignment="center"
                    android:paddingTop="5dp"
                    android:textSize="15sp"
                    android:textStyle="bold|italic"
                    android:textColor="@color/white"/>

                <TextView
                    android:id="@+id/tv_product_name_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Kurti"
                    android:textSize="18sp"
                    android:textColor="#FFFFFF"
                    android:fontFamily="sans-serif-black"
                    android:textStyle="italic"
                    android:layout_marginLeft="20dp"
                    android:layout_below="@id/iv_product_image_listView"
                    android:layout_marginTop="-25dp"/>
                <Button
                    android:id="@+id/expand_user_info_listView"
                    android:layout_alignParentEnd="true"
                    android:layout_below="@id/iv_product_image_listView"
                    android:focusable="false"
                    android:layout_marginTop="-49dp"
                    android:layout_marginEnd="8dp"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@drawable/ic_dropdown"/>
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/layout_dropdown_relative"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:layout_marginStart="15dp"
                android:layout_marginEnd="15dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp">
                <TextView
                    android:id="@+id/tv_date_dropdown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2020-12-14"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#E2FFFFFF"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"/>
                <TextView
                    android:id="@+id/tv_profit_dropdown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Profit 500"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#E2FFFFFF"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"
                    android:layout_alignParentEnd="true"/>

                <TextView
                    android:id="@+id/tv_sellingPrice_dropdown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Selling Price: 800"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#E2FFFFFF"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"
                    android:layout_below="@id/tv_date_dropdown_listView"
                    android:paddingTop="3dp"/>

                <TextView
                    android:id="@+id/tv_actualPrice_dropDown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Actual Price: 850"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#E2FFFFFF"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"
                    android:layout_alignParentRight="true"
                    android:layout_below="@id/tv_profit_dropdown_listView"
                    android:paddingTop="3dp"/>

                <TextView
                    android:id="@+id/tv_pending_dropdown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Pending: 0"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#F50057"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"
                    android:layout_below="@id/tv_sellingPrice_dropdown_listView"
                    android:paddingTop="3dp"/>

                <Button
                    android:id="@+id/btn_update_single_item"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_below="@id/tv_actualPrice_dropDown_listView"
                    android:layout_alignParentEnd="true"
                    android:background="@drawable/ic_update"/>
            </RelativeLayout>
        </LinearLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

您对每个列表视图行使用相同的 infoLayout。每次调用 bindView() 时,您都会用新布局替换 infoLayout。这当然会导致问题,当您使用 infoLayout.setVisibility(View.VISIBLE); 时,infoLayout 它与创建列表视图行时的布局不同,因为您已替换它。

所以替换,

infoLayout = view.findViewById(R.id.layout_dropdown_relative);

final RelativeLayout infoLayout = view.findViewById(R.id.layout_dropdown_relative);