删除项目后Recyclerview高度没有改变

问题描述 投票:0回答:3

我需要帮助。我正在开发一个 Android 应用程序。我使用回收视图。 我的布局看起来像添加项目后。

p.s(Ekle:添加/?indekiler:成分/Tarif Başlığı:食谱标题)

当我单击“X”按钮时,该项目将被删除,但 recyclerview 高度不会改变。

我该如何处理? 提前致谢

适配器:

public class IngredientsAddAdapter extends RecyclerView.Adapter<IngredientsAddAdapter.IngredientsVH> {

    List<String> dataList;
    Context context;
    OnItemClickListener clickListener;

    public IngredientsAddAdapter(Context context, List<String> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public IngredientsVH onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.ingredients_add_list_item, viewGroup, false);
        return new IngredientsVH(view);
    }

    @Override
    public void onBindViewHolder(IngredientsVH ingredientsVH, int i) {
        ingredientsVH.title.setText(dataList.get(i));

    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    class IngredientsVH extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView title;
        ImageButton deleteIng;

        public IngredientsVH(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.ingredientItem);
            deleteIng = (ImageButton) itemView.findViewById(R.id.deleteIng);
            deleteIng.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            clickListener.onItemClick(v, getAdapterPosition());
            dataList.remove(getAdapterPosition());
            notifyItemRemoved(getAdapterPosition());
            notifyItemRangeChanged(getAdapterPosition(), dataList.size());
        }

    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    public void SetOnItemClickListener(final OnItemClickListener itemClickListener) {
        this.clickListener = itemClickListener;
    }

}

活动:

ingList = (RecyclerView) findViewById(R.id.ingList);
ingList.setNestedScrollingEnabled(false);
ingList.setLayoutManager(new LinearLayoutManager(recipeAdd.this));
ingList.setItemAnimator(new DefaultItemAnimator());
ingList.setHasFixedSize(true);
if (ingredientsAddAdapter == null) {
    ingredientsAddAdapter = new IngredientsAddAdapter(recipeAdd.this, ingredientsAddArray);
    ingList.setAdapter(ingredientsAddAdapter);
    ingredientsAddAdapter.SetOnItemClickListener(new IngredientsAddAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
        }
    });
}

recipe_add.xml(这些代码仅用于测试目的。:D)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:background="@color/colorBg"
    android:padding="5dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">


        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="10dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:id="@+id/recipeTitle"
                    android:inputType="text"
                    android:hint="Tarif Başlığı"
                    style="@style/Widget.AppCompat.Light.AutoCompleteTextView"
                    android:fontFamily="sans-serif"
                    android:textSize="14sp"
                    android:textStyle="normal|bold"
                    android:layout_margin="5dp"
                    android:background="@drawable/border"
                    android:padding="10dp" />

            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <Button
                    android:text="Ekle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:id="@+id/ingAdd"
                    android:onClick="addIng" />

                <TextView
                    android:text="İçindekiler"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView4"
                    android:textStyle="normal|bold"
                    android:textSize="14sp"
                    android:padding="10dp"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />
            </RelativeLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/ingList" />
        </android.support.v7.widget.CardView>

    </LinearLayout>

</RelativeLayout>

ingredients_add_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/border_top"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:src="@drawable/crop__ic_cancel"
        android:id="@+id/deleteIng"
        android:background="@android:color/transparent" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ingredientItem"
        android:text="Hello world"
        android:textSize="16dp"
        android:textColor="@color/colorDark"
        android:textStyle="bold"
        android:padding="8dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/isClick"
        android:text="false"
        android:textSize="16dp"
        android:visibility="gone"/>
</LinearLayout>
java android android-recyclerview
3个回答
12
投票

我面临着完全相同的问题,对我有用的是

recyclerView.setHasFixedSize(false);

setHasFixedSize
告诉
RecyclerView
其大小是否取决于适配器内容。将其设置为 false 确实很昂贵,因为
RecyclerView
中会频繁插入和删除。但就我而言,这是必要的。


6
投票

我找到了解决方案。 :D swapAdapter 就是答案(感谢 IDE)

ingList.swapAdapter(ingredientsAddAdapter,true);


0
投票

我对以下代码也有同样的问题:

class AdapterIntent() : RecyclerView.Adapter<AdapterIntent.ItemViewHolder>(), ItemTouchHelperAdapter {{

    override fun onItemDismiss(position: Int) {
        items.removeAt(position)
        notifyItemRemoved(position)
    }

项目已删除,但 recycler.view 列表高度未更新。如果我没有按顺序删除项目,我什至会遇到 IndexOutOfBoundsException 错误。解决我的问题的方法是在适配器类参数中添加对适配器本身的引用,如下所示。

class AdapterIntent(val rv: RecyclerView) : RecyclerView.Adapter<AdapterIntent.ItemViewHolder>(), ItemTouchHelperAdapter {{

    override fun onItemDismiss(position: Int) {
        items.removeAt(position)
        rv.adapter?.notifyItemRemoved(position)
    }

我不明白为什么这解决了我的问题,特别是因为我嵌套了 recyclerview 适配器,仅使用 notificationItemRemoved(position) 即可正常更新。我想这与绑定有关,因为我的内部回收器视图在布局内有绑定项目,而我的主要项目列表在布局中确实有绑定。

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