android – AnimateLayoutChanges不适用于RecyclerView

这篇文章主要介绍android – AnimateLayoutChanges不适用于RecyclerView, 对于目前你搜索查找的问题还是具有很好的参考价值,希望编程之家小编整理的这个内容对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教。

编程之家(jb51.cc)小编说:我有一个带有Recyclerview的屏幕和LinearLayout中的其他元素.问题是当我删除RecyclerView的一个项目时,animateLayoutChanges在这种情况下不起作用. anayone知道为什么会这样吗? XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:

我有一个带有Recyclerview的屏幕和LinearLayout中的其他元素.问题是当我删除RecyclerView的一个项目时,animateLayoutChanges在这种情况下不起作用. anayone知道为什么会这样吗?

XML

<LinearLayout
xmlns: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 {

@Override
protected 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();

@Override
protected 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开发中获得屏幕的宽度和高度还是比较常用的,在网上找...
本次课程表应用功能非常丰富,而且非常实用,可以添加学期的...
Frida-Dexdump 脱壳工具下载使用以及相关技术介绍
实现注册界面的布局效果,包含用户名,密码,忘记密码,登录...
Android App开发实战之实现微信记账本功能(附源码 超详细必...
前言首先看一下小米中的加载动画是怎么样的,恩恩~~~~虽...