一起学android之设置ListView数据显示的动画效果(24)

效果图:

技术分享


平时我们要实现ListView数据显示时的动画效果,可以使用LayoutAnimationController为ListView设置动画效果,并通过ListView的setLayoutAnimation方法来设置,最后startLayoutAnimation,启动动画。参看以下代码:

public class MainActivity extends Activity {
	private ListView lv_arrayadapter;
	private String[] str_name = new String[] { "jack", "debb", "robin", "kikt",  
            "dog", "cat", "elep" }; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		setData();
		loadAnimation();
	}

	private void loadAnimation() {
		/*
		 * 创建动画的集合
		 */
		AnimationSet set = new AnimationSet(false);
		
		Animation animation ;	
		/*
		 * 创建旋转动画
		 */
		animation = new RotateAnimation(180, 10);
		animation.setDuration(1000);
		set.addAnimation(animation);
		
		LayoutAnimationController controller = new LayoutAnimationController(
				set, 1);
		controller
				.setInterpolator(this, android.R.anim.accelerate_interpolator);
		controller.setAnimation(set);
		lv_arrayadapter.setLayoutAnimation(controller);
		lv_arrayadapter.startLayoutAnimation();
	}

	private void initView() {
		lv_arrayadapter = (ListView) findViewById(R.id.lv_arrayadapter);
		// 注册监听事件
		lv_arrayadapter.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(MainActivity.this, str_name[position],
						Toast.LENGTH_SHORT).show();

			}
		});

	}

	private void setData() {
		// 创建ArrayAdapter
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
				MainActivity.this, android.R.layout.simple_list_item_1,
				str_name);
		// 绑定适配器
		lv_arrayadapter.setAdapter(arrayAdapter);
	}

}


activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <ListView  
        android:id="@+id/lv_arrayadapter"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" >  
    </ListView>  

</RelativeLayout>



转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/44248637 情绪控_

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。