Android带动画下拉布局

Home / Android MrLee 2014-12-19 3560

当你想要展现自己的产品或者其它列表的时候,你可能觉得屏幕比较小,放的条目有限。于是可以向下滑动的时候把列表以上布局自动隐藏起来,类似淘宝客户端。向上滑动的时候标题自动弹下来。之前用动画实在发现有问题,listview的高度随着动画开始、结束这两个状态的值,后来改了下实现方法,用AsyncTask动态改变其topMargin,这样就完美实现了。
这里只上传自动伸展、收缩控件,那个listview的滑动大家自己写吧,监听当前位置就可以了,很简单的,只需调用一下doMove方法即可!

20141219103952


package com.lee.move.view;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.lee.move.view.MoveLayout;
public class MainActivity extends Activity implements OnClickListener {
	protected MoveLayout layout;
	protected Button change;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		layout = (MoveLayout) findViewById(R.id.zoom);
		change = (Button) findViewById(R.id.change);
		ListView list = (ListView) findViewById(R.id.list);
		ArrayList arrayList = new ArrayList();
		arrayList.add("aaa");
		arrayList.add("bbb");
		arrayList.add("ccc");
		arrayList.add("ddd");
		arrayList.add("eee");
		arrayList.add("fff");
		arrayList.add("ggg");
		arrayList.add("hhh");
		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, android.R.id.text1,
				arrayList);
		list.setAdapter(adapter);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == change) {
			layout.doMove();
		}
	}
}

XML文件


    
        

MoveLayout .java文件
import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class MoveLayout extends LinearLayout {
	private final static int SPEED = 15;
	private int MAX_VALUE = 0;
	public MoveLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		MAX_VALUE = b - t;
	}
	public void doMove() {
		LayoutParams lp = (LayoutParams) getLayoutParams();
		if (lp.topMargin >= 0)
			new AsynMove().execute(new Integer[] { -SPEED });
		else if (lp.topMargin <= -lp.height)
			new AsynMove().execute(new Integer[] { SPEED });
	}
	private class AsynMove extends AsyncTask {
		@Override
		protected Void doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			int times = 0;
			if (MAX_VALUE % Math.abs(params[0]) == 0)
				times = MAX_VALUE / Math.abs(params[0]);
			else
				times = MAX_VALUE / Math.abs(params[0]) + 1;
			try {
				for (int i = 0; i < times; i++) {
					publishProgress(params);
					Thread.sleep(Math.abs(params[0]));
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			LayoutParams lp = (LayoutParams) getLayoutParams();
			if (values[0] < 0)
				lp.topMargin = Math.max(lp.topMargin + values[0], -MAX_VALUE);
			else
				lp.topMargin = Math.min(lp.topMargin + values[0], MAX_VALUE);
			setLayoutParams(lp);
		}
	}
}

本文链接:https://www.it72.com/469.htm

推荐阅读
最新回复 (0)
返回