这个例子主要是实现上面这样的效果,点击Panel按钮,实现测试1/2/3按钮动态显示并挤压其他布局(实现过程参照网上的一些资料,如有侵权,请告之)
package com.android.PanelDemo;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class Panel extends LinearLayout{
private static final String TAG = "Panel";
private OnPanelStateChangeListenr mOnPanelStateChangeListenr;
private Context mContext;
private int MAX_MIDTH = 0;
private static final int SPEED = 20;
private boolean isOperator = false;
private int direct = 0;
public Panel(Context context, int width, int height) {
super(context);
mContext = context;
LayoutParams lp = new LayoutParams(width, height);
if (direct == 1) {
lp.rightMargin = -lp.width;
MAX_MIDTH = Math.abs(lp.rightMargin);
} else {
lp.leftMargin = -lp.width;
MAX_MIDTH = Math.abs(lp.leftMargin);
}
this.setLayoutParams(lp);
}
public Panel(Context context, View bindView, View contentView, View viewBeside, int width, int height, int direct) {
this(context,width,height);
direct = direct;
//必须改变Panel左侧组件的weight属性
LayoutParams p=(LayoutParams) viewBeside.getLayoutParams();
p.weight=1;//支持挤压
viewBeside.setLayoutParams(p);
setBindView(bindView);
setContentView(contentView);
}
public void setBindView(View bindView) {
bindView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LayoutParams lp = (LayoutParams)getLayoutParams();
Log.e(TAG, "bindView onclick leftMargin = "+lp.leftMargin);
if (isOperator == true) {
return;
}else {
isOperator = true;
}
if (lp.rightMargin < 0 || lp.leftMargin < 0) {
new AsyncMove().execute(new Integer[] { SPEED });// 正数展开
} else {
new AsyncMove().execute(new Integer[] { -SPEED });// 负数展开
}
}
});
}
public void setContentView(View contentView) {
this.addView(contentView);
}
public interface OnPanelStateChangeListenr {
void onPanelOpened(Panel panel);
void onPanelCloseed(Panel panel);
}
public void setOnPanelStateChangeListenr(OnPanelStateChangeListenr listener) {
this.mOnPanelStateChangeListenr = listener;
}
class AsyncMove extends AsyncTask<Integer, Integer, Void> {
@Override
protected Void doInBackground(Integer... params) {
int times;
if (MAX_MIDTH % Math.abs(params[0]) == 0)// 整除
times = MAX_MIDTH / Math.abs(params[0]);
else
times = MAX_MIDTH / Math.abs(params[0]) + 1;// 有余数
Log.e(TAG, "doInBackground times = "+times);
for (int i = 0; i < times; i++) {
publishProgress(params);
try {
Thread.sleep(Math.abs(params[0]));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
LayoutParams lp = (LayoutParams)getLayoutParams();
if (values[0] < 0){//关闭
if (direct == 1) {
lp.rightMargin = Math.max(lp.rightMargin + values[0],-MAX_MIDTH);
} else {
lp.leftMargin = Math.max(lp.leftMargin + values[0],-MAX_MIDTH);
}
}
else{//打开
if (direct == 1) {
lp.rightMargin = Math.min(lp.rightMargin + values[0],MAX_MIDTH);
} else {
lp.leftMargin = Math.min(lp.leftMargin + values[0],MAX_MIDTH);
}
}
if (direct == 1) {
if(lp.rightMargin==0 && mOnPanelStateChangeListenr!=null){//展开之后
mOnPanelStateChangeListenr.onPanelOpened(Panel.this);//调用OPEN回调函数
}
else if(lp.rightMargin==-MAX_MIDTH && mOnPanelStateChangeListenr!=null){//收缩之后
mOnPanelStateChangeListenr.onPanelCloseed(Panel.this);//调用CLOSE回调函数
}
} else {
if(lp.leftMargin==0 && mOnPanelStateChangeListenr!=null){//展开之后
mOnPanelStateChangeListenr.onPanelOpened(Panel.this);//调用OPEN回调函数
}
else if(lp.leftMargin==-MAX_MIDTH && mOnPanelStateChangeListenr!=null){//收缩之后
mOnPanelStateChangeListenr.onPanelCloseed(Panel.this);//调用CLOSE回调函数
}
}
if (lp.rightMargin==0 || lp.rightMargin==-MAX_MIDTH || lp.leftMargin==0 || lp.leftMargin==-MAX_MIDTH) {
isOperator = false;
}
Log.e(TAG, "onProgressUpdate");
setLayoutParams(lp);
}
}
}
package com.android.PanelDemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.PanelDemo.Panel.OnPanelStateChangeListenr;
public class PanelViewDemoActivity extends Activity {
private static final String TAG = "PanelViewDemoActivity";
private Panel mPanel;
private LinearLayout mLinearLayout;
private Button mPanelButton;
private TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLinearLayout = (LinearLayout)findViewById(R.id.mLinearlayout_id);
mPanelButton = (Button)findViewById(R.id.mButton_id);
mTextView = (TextView)findViewById(R.id.mTextView_id);
LayoutInflater mInflater = LayoutInflater.from(this);
View contentView = mInflater.inflate(R.layout.mypanel, null);
mPanel = new Panel(this, mPanelButton, contentView, mTextView, 100, LayoutParams.FILL_PARENT, 0);
mPanel.setOnPanelStateChangeListenr(new OnPanelStateChangeListenr() {
@Override
public void onPanelCloseed(Panel panel) {
// TODO Auto-generated method stub
//Toast.makeText(PanelViewDemoActivity.this, "panel Close!!", Toast.LENGTH_SHORT).show();
Log.e(TAG, "onPanelCloseed");
}
@Override
public void onPanelOpened(Panel panel) {
// TODO Auto-generated method stub
//Toast.makeText(PanelViewDemoActivity.this, "panel open!!", Toast.LENGTH_SHORT).show();
Log.e(TAG, "onPanelOpened");
}
});
mLinearLayout.addView(mPanel,0);
}
}
<?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:orientation="vertical" >
<LinearLayout
android:id="@+id/mLinearlayout_id" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1" android:orientation="horizontal"
>
<TextView
android:id="@+id/mTextView_id" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:text="Panel 测试"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffffff"
>
<Button
android:id="@+id/mButton_id" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Panel" android:layout_gravity="left"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/mPanelButton1_id" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="测试1"
/>
<Button
android:id="@+id/mPanelButton2_id" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="测试2"
/>
<Button
android:id="@+id/mPanelButton3_id" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="测试3"
/>
</LinearLayout>
这是一种实现方法,当然我也可以用动画来实现,下面我们来实现一下:(有时间编辑)