<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_second" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_light" android:orientation="vertical" tools:context="karry.example.com.test.SecondActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="start" android:text="显示ListView数据" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="stop" android:text="隐藏ListView数据" /> <ListView android:id="@+id/lv" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right|bottom" android:orientation="vertical"></ListView></LinearLayout>二、在anim文件夹下新建slide_left.xmlm,这个动画实现ListView显示子View时的动画效果
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:duration="500" android:fromXDelta="5%p" android:toXDelta="-2%p" /> <alpha android:duration="500" android:fromAlpha="0" android:toAlpha="1"></alpha></set>在anim文件夹下新建slide_right.xml,这个动画实现隐藏子View时的动画效果
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:duration="300" android:fromXDelta="0%p" android:toXDelta="5%p" /> <alpha android:duration="300" android:fromAlpha="1" android:toAlpha="0"></alpha></set>三、MainActivity中ListView数据的添加,以及点击事件的完成:
package karry.example.com.test;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.view.animation.LayoutAnimationController;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import butterknife.Bind;import butterknife.ButterKnife;public class SecondActivity extends AppCompatActivity { @Bind(R.id.lv) ListView lv; PRivate MyAdapter adapter = new MyAdapter(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); ButterKnife.bind(this); lv.setAdapter(adapter); } //隐藏ListView数据的点击事件 public void stop(View v) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_right); LayoutAnimationController controller = new LayoutAnimationController(animation); controller.setDelay(0.2f);//表示在第一个子View动画执行到20%时,第二个子View的动画开始执行 controller.setOrder(LayoutAnimationController.ORDER_NORMAL);//顺序显示子View的动画 lv.setLayoutAnimation(controller); lv.startLayoutAnimation(); animation.setFillAfter(true); } //显示ListView数据的点击事件 public void start(View v) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_left); LayoutAnimationController controller = new LayoutAnimationController(animation); controller.setDelay(0.2f); controller.setOrder(LayoutAnimationController.ORDER_REVERSE);//逆序显示子View的动画 lv.setLayoutAnimation(controller); lv.startLayoutAnimation(); animation.setFillAfter(false); } public void clear(View v) { lv.setVisibility(View.INVISIBLE); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return 4; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { TextView textView = new TextView(SecondActivity.this); textView.setText("按钮" + i); textView.setTextSize(DentisyUtils.sp2px(SecondActivity.this, 18)); textView.setGravity(Gravity.CENTER); ListView.LayoutParams param = new ListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView.setLayoutParams(param); return textView; } }}
新闻热点
疑难解答