android view.animate,android – AnimateLayoutChanges不适用于RecyclerView

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

XML

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">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:animateLayoutChanges="true"

android:orientation="vertical">

android:id="@+id/recyclerview"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

android:id="@+id/test1"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/colorAccent"

android:gravity="center_vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="LinearLayout"

android:textColor="#FFFFFF"

android:textSize="22sp"/>

android:id="@+id/test2"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/colorPrimaryDark"

android:gravity="center_vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="LinearLayout"

android:textColor="#FFFFFF"

android:textSize="22sp"/>

android:id="@+id/test3"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/colorAccent"

android:gravity="center_vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="LinearLayout"

android:textColor="#FFFFFF"

android:textSize="22sp"/>

android:id="@+id/test4"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/colorPrimaryDark"

android:gravity="center_vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="LinearLayout"

android:textColor="#FFFFFF"

android:textSize="22sp"/>

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{

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());

}

});

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用`v-for`指令结合`:class`绑定来动态渲染多个元素,并且通过Vue的数据驱动特性来控制它们的显示与隐藏。具体实现可以参考以下代码: ```html <template> <div> <div v-for="(item, index) in elements" :key="index" :class="[item.visible ? 'animate__animated animate__fadeIn' : 'animate__animated animate__fadeOut', 'threebox_image_box']" > 这是第 {{ index + 1 }} 个元素 </div> <button @click="hideElements">隐藏元素</button> </div> </template> <script> export default { data() { return { elements: [ { visible: true }, { visible: true }, { visible: true }, { visible: true }, { visible: true }, { visible: true }, { visible: true }, { visible: true } ] }; }, methods: { hideElements() { for (let i = 0; i < this.elements.length; i++) { this.elements[i].visible = false; } } } }; </script> <style> .threebox_image_box { display: block; } .animate__animated { animation-duration: 1s; } .animate__fadeIn { animation-name: fadeIn; } .animate__fadeOut { animation-name: fadeOut; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } } </style> ``` 在上面的代码中,我们首先定义了一个`elements`数组,它包含了需要动态渲染的多个元素,并且每个元素都有一个`visible`属性,用于控制它们的显示与隐藏。然后在模板中使用`v-for`指令循环渲染多个元素,并且通过`:class`绑定来动态设置元素的`class`属性,以便实现渐变动画效果。最后在方法中,我们通过修改`elements`数组中每个元素的`visible`属性,来控制它们的显示与隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值