自动伸缩布局

来源:互联网 发布:新三板越川网络872316 编辑:程序博客网 时间:2024/09/21 08:59

当你想要展现自己的产品或者其它列表的时候,你可能觉得屏幕比较小,放的条目有限。于是可以向下滑动的时候把列表以上布局自动隐藏起来,类似淘宝客户端。向上滑动的时候标题自动弹下来。之前用动画实在发现有问题,listview的高度随着动画开始、结束这两个状态的值,后来改了下实现方法,用AsyncTask动态改变其topMargin,这样就完美实现了。

这里只上传自动伸展、收缩控件,那个listview的滑动大家自己写吧,监听当前位置就可以了,很简单的,只需调用一下doMove方法即可!

大家自己建个工程跑跑,反正就一个类而已。

上图:


______________________________________________________________________





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. */@Overridepublic 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<String> arrayList = new ArrayList<String>();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<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,arrayList);list.setAdapter(adapter);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v == change) {layout.doMove();}}}


package com.wasai;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}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {// TODO Auto-generated method stubsuper.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<Integer, Integer, Void> {@Overrideprotected Void doInBackground(Integer... params) {// TODO Auto-generated method stubint times = 0;if (MAX_VALUE % Math.abs(params[0]) == 0)times = MAX_VALUE / Math.abs(params[0]);elsetimes = 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 blocke.printStackTrace();}return null;}@Overrideprotected void onProgressUpdate(Integer... values) {// TODO Auto-generated method stubsuper.onProgressUpdate(values);LayoutParams lp = (LayoutParams) getLayoutParams();if (values[0] < 0)lp.topMargin = Math.max(lp.topMargin + values[0], -MAX_VALUE);elselp.topMargin = Math.min(lp.topMargin + values[0], MAX_VALUE);setLayoutParams(lp);}}}



<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center"    android:orientation="vertical" >    <com.lee.move.view.MoveLayout        android:id="@+id/zoom"        android:layout_width="fill_parent"        android:layout_height="100dip"        android:layout_weight="0"        android:background="#FFFFFF"        android:gravity="center" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="sdfds" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="sdfds"            android:textColor="#0000FF" />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/ic_launcher" />    </com.lee.move.view.MoveLayout>    <ListView        android:id="@+id/list"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1" />    <Button        android:id="@+id/change"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="0"        android:onClick="onClick"        android:text="改变" /></LinearLayout>



原创粉丝点击