android – AnimateLayoutChanges不适用于RecyclerView

android – AnimateLayoutChanges不适用于RecyclerView,第1张

概述我有一个带有Recyclerview的屏幕和LinearLayout中的其他元素.问题是当我删除RecyclerView的一个项目时,animateLayoutChanges在这种情况下不起作用. anayone知道为什么会这样吗? XML <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns: 我有一个带有RecyclervIEw的屏幕和linearLayout中的其他元素.问题是当我删除RecyclerVIEw的一个项目时,animateLayoutChanges在这种情况下不起作用. anayone知道为什么会这样吗?

XML

<linearLayoutxmlns: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"tools:context="com.example.alvaro.resizetest.MainActivity"><linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:animateLayoutChanges="true"    androID:orIEntation="vertical">    <androID.support.v7.Widget.RecyclerVIEw        androID:ID="@+ID/recyclervIEw"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"/>    <linearLayout        androID:ID="@+ID/test1"        androID:layout_wIDth="match_parent"        androID:layout_height="50dp"        androID:background="@color/colorAccent"        androID:gravity="center_vertical">        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="linearLayout"            androID:textcolor="#FFFFFF"            androID:textSize="22sp"/>    </linearLayout>    <linearLayout        androID:ID="@+ID/test2"        androID:layout_wIDth="match_parent"        androID:layout_height="50dp"        androID:background="@color/colorPrimaryDark"        androID:gravity="center_vertical">        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="linearLayout"            androID:textcolor="#FFFFFF"            androID:textSize="22sp"/>    </linearLayout>    <linearLayout        androID:ID="@+ID/test3"        androID:layout_wIDth="match_parent"        androID:layout_height="50dp"        androID:background="@color/colorAccent"        androID:gravity="center_vertical">        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="linearLayout"            androID:textcolor="#FFFFFF"            androID:textSize="22sp"/>    </linearLayout>    <linearLayout        androID:ID="@+ID/test4"        androID:layout_wIDth="match_parent"        androID:layout_height="50dp"        androID:background="@color/colorPrimaryDark"        androID:gravity="center_vertical">        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="linearLayout"            androID:textcolor="#FFFFFF"            androID:textSize="22sp"/>    </linearLayout></linearLayout>

JAVA

public class MainActivity extends AppCompatActivity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    RecyclerVIEw recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclervIEw);    Adapter adapter = new Adapter();    recyclerVIEw.setLayoutManager(new linearlayoutmanager(this));    recyclerVIEw.setAdapter(adapter);    recyclerVIEw.setnestedScrollingEnabled(true);    VIEw.OnClickListener Listener = new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw vIEw) {            vIEw.setVisibility(VIEw.GONE);        }    };    findVIEwByID(R.ID.test1).setonClickListener(Listener);    findVIEwByID(R.ID.test2).setonClickListener(Listener);    findVIEwByID(R.ID.test3).setonClickListener(Listener);    findVIEwByID(R.ID.test4).setonClickListener(Listener);}class Adapter extends RecyclerVIEw.Adapter<Adapter.Holder>{    int size = 3;    public Adapter() {    }    @OverrIDe    public Holder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {        VIEw vIEw = getLayoutInflater().inflate(R.layout.item,parent,false);        return new Holder(vIEw);    }    @OverrIDe    public voID onBindVIEwHolder(Holder holder,int position) {    }    @OverrIDe    public int getItemCount() {        return size;    }    class Holder extends RecyclerVIEw.VIEwHolder {        public Holder(final VIEw itemVIEw) {            super(itemVIEw);            itemVIEw.setonClickListener(new VIEw.OnClickListener() {                @OverrIDe                public voID onClick(VIEw vIEw) {                    size --;                    notifyItemRemoved(getAdapterposition());                }            });        }    }}
解决方法 过了一会儿我得到了解决方案.我创建了一个函数来设置recyclelerVIEw高度的动画.

JAVA

public class MainActivity extends AppCompatActivity {private final String TAG = MainActivity.class.getSimplename();@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    RecyclerVIEw recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclervIEw);    Adapter adapter = new Adapter(recyclerVIEw);    linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this) {        @OverrIDe        public boolean canScrollVertically() {            return false;        }    };    recyclerVIEw.setLayoutManager(linearlayoutmanager);    recyclerVIEw.setAdapter(adapter);    recyclerVIEw.setnestedScrollingEnabled(true);    VIEw.OnClickListener Listener = new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw vIEw) {            vIEw.setVisibility(VIEw.GONE);        }    };    findVIEwByID(R.ID.test1).setonClickListener(Listener);    findVIEwByID(R.ID.test2).setonClickListener(Listener);    findVIEwByID(R.ID.test3).setonClickListener(Listener);    findVIEwByID(R.ID.test4).setonClickListener(Listener);}public voID animateHeight(final VIEw v,final int height) {    final int initialHeight = v.getMeasuredHeight();    int duration = 500;    Interpolator interpolator = new AccelerateInterpolator(2);    // I have to set the same height before the animation because there is a glitch    // in the beginning of the animation    v.getLayoutParams().height = initialHeight;    v.requestLayout();    Animation a = new Animation() {        @OverrIDe        protected voID applytransformation(float interpolatedTime,transformation t) {            Log.d(TAG,"InterpolatedTime: " + interpolatedTime);            Log.d(TAG,"Collapsing height: " + (initialHeight - (int) (height * interpolatedTime)));            v.getLayoutParams().height = initialHeight - (int) (height * interpolatedTime);            v.requestLayout();        }        @OverrIDe        public boolean willChangeBounds() {            return true;        }    };    a.setDuration(duration);    a.setInterpolator(interpolator);    v.startAnimation(a);}class Adapter extends RecyclerVIEw.Adapter<Adapter.Holder> {    RecyclerVIEw mRecyclerVIEw;    int size = 3;    public Adapter(RecyclerVIEw recyclerVIEw) {        mRecyclerVIEw = recyclerVIEw;    }    @OverrIDe    public Holder onCreateVIEwHolder(VIEwGroup parent,int position) {    }    @OverrIDe    public int getItemCount() {        return size;    }    class Holder extends RecyclerVIEw.VIEwHolder {        public Holder(final VIEw itemVIEw) {            super(itemVIEw);            itemVIEw.setonClickListener(new VIEw.OnClickListener() {                @OverrIDe                public voID onClick(VIEw vIEw) {                    size--;                    notifyItemRemoved(getAdapterposition());                    animateHeight((VIEw) itemVIEw.getParent(),itemVIEw.getMeasuredHeight());                }            });        }    }}

}

总结

以上是内存溢出为你收集整理的android – AnimateLayoutChanges不适用于RecyclerView全部内容,希望文章能够帮你解决android – AnimateLayoutChanges不适用于RecyclerView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1139169.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存