package com.test.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;
public class TTTTTActivity extends Activity {
private GridView gridview;
private LinearLayout rl;
private PanelDrawer drawer;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
init();
}
private void init(){
rl = (LinearLayout)findViewById(R.id.rl);
gridview = (GridView)findViewById(R.id.gridview);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "" + arg2, Toast.LENGTH_LONG).show();
}
});
drawer = new PanelDrawer(getBaseContext(), gridview, 160, 480);
rl.addView(drawer);
}
private BaseAdapter adapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(getBaseContext());
iv.setImageResource(R.drawable.icon);
iv.setScaleType(ScaleType.CENTER_INSIDE);
return iv;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 50;
}
};
public boolean onTouchEvent(MotionEvent event) {
return drawer.onTouchEvent(event);
};
}
上面是activity
package com.test.android;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class PanelDrawer extends LinearLayout implements GestureDetector.OnGestureListener {
private static final int STEP_WIDTH = 20;
private static final int BUTTON_WIDTH = 20;
private int width;
private int height;
private int rightmargin;
private OnOpenListener openlistener;
private OnCloseListener closelistener;
private LinearLayout container;
private GestureDetector detector;
public PanelDrawer(Context context,View view,int width,int height){
super(context);
this.width = width;
this.height = height;
init(context,view);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return detector.onTouchEvent(event);
}
private void init(Context context,View view){
this.setFocusableInTouchMode(false);
detector = new GestureDetector(this);
detector.setIsLongpressEnabled(true);
// gridview设置可以进行压缩
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)view.getLayoutParams();
lp.weight = 1;
view.setLayoutParams(lp);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.width, this.height);
this.setOrientation(LinearLayout.HORIZONTAL);
params.rightMargin = -this.width + BUTTON_WIDTH;
this.rightmargin = params.rightMargin;
this.setLayoutParams(params);
Button bar = new Button(context);
bar.setFocusableInTouchMode(false);
LinearLayout.LayoutParams barparams = new LinearLayout.LayoutParams(BUTTON_WIDTH, this.height);
bar.setBackgroundColor(Color.BLUE);
this.addView(bar,barparams);
container = new LinearLayout(context);
LinearLayout.LayoutParams containerparams = new LinearLayout.LayoutParams(width - BUTTON_WIDTH, LinearLayout.LayoutParams.FILL_PARENT);
// containerparams.width = 1;
container.setLayoutParams(containerparams);
container.setBackgroundColor(Color.RED);
this.addView(container);
bar.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return detector.onTouchEvent(event);
}
});
}
private int distance;
private class AsyMoveDrawable extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String move = params[0];
if(move.toString().trim().toLowerCase().toString().equals("open")){
while(rightmargin < 0){
rightmargin += STEP_WIDTH;
if(rightmargin > 0){
rightmargin = 0;
}
publishProgress(rightmargin);
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
} else {
while(rightmargin > (-width + BUTTON_WIDTH)){
rightmargin -= STEP_WIDTH;
if(rightmargin < (-width + BUTTON_WIDTH)){
rightmargin = (-width + BUTTON_WIDTH);
}
publishProgress(rightmargin);
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
int current = values[0];
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)PanelDrawer.this.getLayoutParams();
params.rightMargin = current;
PanelDrawer.this.setLayoutParams(params);
rightmargin = current;
// container.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
}
};
public interface OnOpenListener{
public void onOpen();
}
public interface OnCloseListener{
public void onClose();
}
public void addContainerView(View view){
container.addView(view);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "containeer", Toast.LENGTH_LONG).show();
distance += distanceX;
// 向左滑动
if(rightmargin < 0 && distanceX > 0){
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)container.getLayoutParams();
rightmargin += distanceX;
params.rightMargin = rightmargin;
if(rightmargin > 0){
rightmargin = 0;
params.rightMargin = 0;
}
PanelDrawer.this.setLayoutParams(params);
if(rightmargin == 0){
if(openlistener != null){
openlistener.onOpen();
}
}
} else {
}
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
在控件里进行ontouchevent与手势,只能触发button这种控件,
要想这个控件都进行触发,button与layout,则要重写activity中ontouchevent(),
如: