Android带侧滑菜单的列表
2016-05-27 08:54
316 查看
实现了侧滑显示一个功能菜单的功能。自定义控件继承自HorizontalScrollView。放在一个自定义的列表适配器中。
public class MyHorizontalScrollView extends HorizontalScrollView implements
OnTouchListener {
private int screenWidth;
private int screenHeight;
private int changed;
public MyHorizontalScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stu
getScreenParam(context);
setOnTouchListener(this);
}
public MyHorizontalScrollView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
getScreenParam(context);
setOnTouchListener(this);
}
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
getScreenParam(context);
setOnTouchListener(this);
}
int left;
int oldleft;
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
left = l;
oldleft = oldl;
}
/**
* 获取屏幕宽高
*
* @param context
*/
protected void getScreenParam(Context context) {
MainActivity mContext = (MainActivity) context;
DisplayMetrics dm = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
Log.e("aaa", screenWidth + "," + screenHeight);
changed = dip2px(context, 100f);
}
/**
* dp转px方法
*
* @param context
* @param dpValue
* @return
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
private int touchEventId = -9983761;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
View scroller = (View) msg.obj;
if (msg.what == touchEventId) {
onStop();
}
}
};
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_POINTER_UP
|| event.getAction() == MotionEvent.ACTION_UP) {
handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v),
5);
}
return false;
}
private void onStop() {
if (oldleft < changed) {
smoothScrollTo(0, 0);
}
if (oldleft > changed) {
smoothScrollTo(changed * 2, 0);
}
}
}
列表的布局
<?xml version="1.0" encoding="utf-8"?>
<com.example.testscrolllist.MyHorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="@null" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/btn_delete"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/test"
android:background="#5fc1ce"
android:padding="10dp"
android:text="删除"
android:textColor="#fff"
android:textSize="18dp" />
<Button
android:id="@+id/btn_up"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/btn_delete"
android:background="#ffc64b"
android:padding="10dp"
android:text="置顶"
android:textColor="#fff"
android:textSize="18dp" />
</RelativeLayout>
</com.example.testscrolllist.MyHorizontalScrollView>适配器没有特殊的地方。只在testview中显示一行文字。
运行起来的效果。松手后如果菜单显示部分超过一半则显示菜单。如果显示部分不超过一半则隐藏菜单。
public class MyHorizontalScrollView extends HorizontalScrollView implements
OnTouchListener {
private int screenWidth;
private int screenHeight;
private int changed;
public MyHorizontalScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stu
getScreenParam(context);
setOnTouchListener(this);
}
public MyHorizontalScrollView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
getScreenParam(context);
setOnTouchListener(this);
}
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
getScreenParam(context);
setOnTouchListener(this);
}
int left;
int oldleft;
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
left = l;
oldleft = oldl;
}
/**
* 获取屏幕宽高
*
* @param context
*/
protected void getScreenParam(Context context) {
MainActivity mContext = (MainActivity) context;
DisplayMetrics dm = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
Log.e("aaa", screenWidth + "," + screenHeight);
changed = dip2px(context, 100f);
}
/**
* dp转px方法
*
* @param context
* @param dpValue
* @return
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
private int touchEventId = -9983761;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
View scroller = (View) msg.obj;
if (msg.what == touchEventId) {
onStop();
}
}
};
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_POINTER_UP
|| event.getAction() == MotionEvent.ACTION_UP) {
handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v),
5);
}
return false;
}
private void onStop() {
if (oldleft < changed) {
smoothScrollTo(0, 0);
}
if (oldleft > changed) {
smoothScrollTo(changed * 2, 0);
}
}
}
列表的布局
<?xml version="1.0" encoding="utf-8"?>
<com.example.testscrolllist.MyHorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="@null" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/btn_delete"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/test"
android:background="#5fc1ce"
android:padding="10dp"
android:text="删除"
android:textColor="#fff"
android:textSize="18dp" />
<Button
android:id="@+id/btn_up"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/btn_delete"
android:background="#ffc64b"
android:padding="10dp"
android:text="置顶"
android:textColor="#fff"
android:textSize="18dp" />
</RelativeLayout>
</com.example.testscrolllist.MyHorizontalScrollView>适配器没有特殊的地方。只在testview中显示一行文字。
运行起来的效果。松手后如果菜单显示部分超过一半则显示菜单。如果显示部分不超过一半则隐藏菜单。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories