RecycleView可以策划的Item
public abstract class SwipeMenuViewHolder {
/* 从左侧滑出菜单 */
public static final int EDGE_LEFT = 1;
/* 从右侧滑出菜单 */
public static final int EDGE_RIGHT = 2;
/* 默认从右侧滑出菜单 */
private int mTrackingEdges = EDGE_RIGHT;
/* 滑出来的布局 */
private View swipMenuView;
/* 可以拖动的布局 */
private View captureView;
/* 整个条目的布局 */
protected DragLayout itemView; private Context context; private DragViewHolder dragViewHolder; public SwipeMenuViewHolder(Context context, View swipMenuView, View captureView) {
this.swipMenuView = swipMenuView;
this.captureView = captureView;
this.context = context;
initItemView();
} public SwipeMenuViewHolder(Context context, View swipMenuView, View captureView, int mTrackingEdges) {
this.mTrackingEdges = mTrackingEdges;
this.swipMenuView = swipMenuView;
this.captureView = captureView;
this.context = context;
initItemView();
} private void initItemView() {
itemView = new DragLayout(context);
itemView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
itemView.setmTrackingEdges(mTrackingEdges);
itemView.initView(captureView, swipMenuView);
dragViewHolder = new DragViewHolder(this, itemView);
} public boolean isOpen() {
return itemView.state == DragLayout.STATE_OPNE;
} public DragViewHolder getDragViewHolder() {
return dragViewHolder;
} /**
* 初始化控件
*
* @param itemView 条目的布局
*/
public abstract void initView(View itemView); /**
* 获取this
*
* @param viewHolder 获取当前的 ViewHolder
* @return RecyclerViewDragHolder
*/
public static SwipeMenuViewHolder getHolder(RecyclerView.ViewHolder viewHolder) {
return ((DragViewHolder) viewHolder).holder;
} class DragViewHolder extends RecyclerView.ViewHolder { public SwipeMenuViewHolder holder; public DragViewHolder(SwipeMenuViewHolder holder, View itemView) {
super(itemView);
this.holder = holder;
initView(itemView); }
} private class DragLayout extends FrameLayout { private ViewDragHelper mDragHelper;
private View captureView;
private View swipeMenuView; /* 左侧起点 */
private int startX;
/* 可拉出来布局的宽度 */
private int swipeMenuWidth;
private int mTrackingEdges = EDGE_RIGHT;
/* 滑出来的百分比,大于这个值松开自动打开,否则就关闭 */
private float scrollPercent = 0.5f;
/* 当前打开状态 */
protected int state = STATE_CLOSE;
protected static final int STATE_OPNE = 1;
protected static final int STATE_CLOSE = 2; public DragLayout(Context context) {
super(context);
init();
} public DragLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
} public void initView(View captureView, View swipeMenuView) {
this.captureView = captureView;
this.swipeMenuView = swipeMenuView;
startX = 0;
addView(createBgView(swipeMenuView));
addView(captureView);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (widthMeasureSpec != 0) {
swipeMenuWidth = swipeMenuView.getWidth();
}
} /* 可以滑出来的布局 */
private View createBgView(View view) {
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
linearLayout.setGravity(mTrackingEdges == EDGE_RIGHT ? Gravity.END : Gravity.START);
linearLayout.addView(view);
return linearLayout;
} private void init() {
mDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragCallBack());
mDragHelper.setEdgeTrackingEnabled(mTrackingEdges == EDGE_RIGHT ? ViewDragHelper.EDGE_RIGHT : ViewDragHelper.EDGE_LEFT);
} public class ViewDragCallBack extends ViewDragHelper.Callback { public ViewDragCallBack() {
} @Override
public boolean tryCaptureView(View child, int pointerId) {
return child == captureView;
} @Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
if (left != startX) {
if (swipeMenuView.getVisibility() == View.GONE)
swipeMenuView.setVisibility(View.VISIBLE);
} else {
if (swipeMenuView.getVisibility() == View.VISIBLE)
swipeMenuView.setVisibility(View.GONE); }
invalidate();
} @Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
if (releasedChild != captureView)
return;
int endX;
Log.d("captureView.Left", captureView.getLeft() + "");
Log.d("scrollSwipeMenuWidth", (int) ((float) swipeMenuWidth * scrollPercent) + "");
Log.d("state", state + "");
Log.d("mTrackingEdges", mTrackingEdges + "");
Log.d("startX", +startX + "");
if (mTrackingEdges == EDGE_LEFT) {
if (captureView.getLeft() < (int) ((float) swipeMenuWidth * scrollPercent) || state == STATE_OPNE) {
endX = startX;
state = STATE_CLOSE;
} else {
endX = swipeMenuWidth;
state = STATE_OPNE;
// add by wanggeng for post event
postOPNEEvent();
}
} else {//bgView从右边缘滑出
if (captureView.getLeft() > -(int) ((float) swipeMenuWidth * scrollPercent) || state == STATE_OPNE) {//向右滑动关闭bgView
endX = startX;
state = STATE_CLOSE;
} else {//向左滑动拉出bgView
endX = -1 * swipeMenuWidth;
state = STATE_OPNE;
// add by wanggeng for post event
postOPNEEvent();
}
} Log.d("endX", +endX + "");
Log.d("swipeMenuWidth", +swipeMenuWidth + "");
if (mDragHelper.smoothSlideViewTo(captureView, endX, 0)) {
ViewCompat.postInvalidateOnAnimation(DragLayout.this);
}
invalidate();
} @Override
public int getViewHorizontalDragRange(View child) {
return (int) (swipeMenuWidth * scrollPercent);
} /**
* @param child 被拖动到view
* @param left captureView被拖动后距x轴的距离
* @param dx 横向移动的距离
* @return captureView被拖动后距x轴的距离
*/
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (mTrackingEdges == EDGE_LEFT) {
if (left > swipeMenuWidth && dx > 0) return swipeMenuWidth;
if (left < 0 && dx < 0) return 0;
} else {
if (left > 0 && dx > 0) return 0;
if (left < -swipeMenuWidth && dx < 0) return -swipeMenuWidth;
}
return left;
}
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (state == STATE_CLOSE) {
swipeMenuView.setVisibility(View.GONE);
}
return mDragHelper.shouldInterceptTouchEvent(ev);
} @Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
mDragHelper.processTouchEvent(event);
if (state == STATE_CLOSE) {
if (event.getAction() == MotionEvent.ACTION_UP) {
swipeMenuView.setVisibility(View.GONE);
}
}
return true;
} @Override
public void computeScroll() {
super.computeScroll();
if (mDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
} public void setmTrackingEdges(int mTrackingEdges) {
this.mTrackingEdges = mTrackingEdges;
mDragHelper.setEdgeTrackingEnabled(mTrackingEdges == EDGE_RIGHT ? ViewDragHelper.EDGE_RIGHT : ViewDragHelper.EDGE_LEFT);
}
}
public void scrollToClose() {
if (itemView.mDragHelper.smoothSlideViewTo(captureView, itemView.startX, 0)) {
ViewCompat.postInvalidateOnAnimation(itemView);
}
itemView.invalidate();
} for post event
public void postOPNEEvent() {
final ActionEvent actionEvent = new ActionEvent("STATE_OPNE");
actionEvent.setData(SwipeMenuViewHolder.this);
EventBus.getDefault().post(actionEvent);
}
}
RecycleView可以策划的Item的更多相关文章
- RecycleView实现侧滑删除item
对于列表空间的侧滑操作,网上有很多开源的空间可以使用,Google在它的新控件RecycleView中增加了侧滑的API,完全遵循Material Design设计规范,下面看看效果演示: 下面看看介 ...
- 实现RecycleView动态使列表item可以点击或不可点击切换
效果 这里讲的是第二个button跳转的Activity,这里和上一篇不同之处在于可以item点击.item子控件点击 继承BaseAdapter 同样也要继承BaseAdapter public c ...
- android基础开发之RecycleView(1)---基本使用方式
RecycleView是google为了优化listview,gridview 提供的一个新的控件. 1.android 导入recycleview 在app的gradle里面加入: dependen ...
- NestedScrollView嵌套RecycleView发生的小问题
1.解决方法:嵌套滑动不激活. recycleView.setNestedScrollingEnable(false); 这样做有个弊端,RecycleView的item会一次性加载完,不管是否显示, ...
- IAdjustCountOption--动态设置recycleView的itemCount(不须要改动数据源)
概述 RecycleViewUtil是新增的一个主要针对RecycleView的一个工具类.该工具类中提供了部分RecycleView可能会使用到的方法,当中也包含了一些用来增强HeaderRecyc ...
- android UI控件小记
1.关于text和drawableTop之类的间距 android:drawablePadding="10dp" 2.EditText属性 android:phoneNumber= ...
- 拆解轮子之XRecyclerView
简介 这个轮子是对RecyclerView的封装,主要完成了下拉刷新.上拉加载更多.RecyclerView头部.在我的Material Design学习项目中使用到了项目地址,感觉还不错.趁着毕业答 ...
- 如何自定义RecycleView item的间距
引言 在以前使用ListView和GridView时,设置item之间的间距还是相对比较简单的,因为它们的基本属性里面Android已经定义好了,可以直接设置属性值即可.但Google为了通用性和灵活 ...
- Android RecycleView 自定义Item的使用
自定义布局的RecycleView需要自己实现Adapter,ViewHolder和布局: 自定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBin ...
随机推荐
- python 之ConfigParser
ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...
- print_r、echo、var_dump三者的区别
这三者均是php里输出的方式. 1.print_r用于输出数组. 2.echo只能用于输出字符串,在没有框架的时候,多用这种方法将结果打印在html页面上. 3.var_dump能输出任何类型的数据, ...
- 无损破解Android图案密码及原理
作者:http://weibo.com/hellguard 安卓图案密码穷举,秒破gesture.key.图案密码是明文的SHA1,明文取值范围是[00-08],不超过9字节,不重复出现,没有SALT ...
- AspxSpy2014 Final
受bin牛委托修改并发布,版权归bin牛所有. Bug/建议提交:zcgonvh@rootkit.net.cn 祝各位马年大吉,财源滚滚. 免责声明: 本程序只用于管理员安全检测,使用前请注意环境与法 ...
- super()和this()的区别
1)调用super()必须写在子类构造方法的第一行,否则编译不通过.每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错. 2)supe ...
- 科技来电:不容错过的win10轻度美化攻略
作为微软不择手段采用弹框.限时.警告等方式强推win10的结果, win10市场占有率大增,微软高兴了,可用户苦了,吹洗剪+杀马特般的metro&传统混血开始菜单.鼻青脸肿般的任务栏.单一颜色 ...
- 一个链接直接打开APP
http://www.cnblogs.com/jzm17173/p/4569574.html 这是IOS http://www.jianshu.com/p/af211f2a990e
- 织梦DEDECMS网站后台安全检测提示 加一个开关
1.进入后台后,点击 系统->系统基本参数->添加变量: 变量名称:cfg_safecheck_switch 变量值:N 变量类型:布尔(Y/N) 参数说明:启用安全监测系统: 2.找到系 ...
- 关闭SSMS的事务自动提交,改为手动提交
SQLServer 2005-2008-2012使用Oracle时,默认是手动提交.而SQLServer2005中,默认是自动提交,但是SQLServer支持配置. 方法: 用SSMS连接到SQL S ...
- 【iCore3 双核心板_FPGA】实验二十三:使用JTAG UART终端打印信息
实验指导书及代码包下载: http://pan.baidu.com/s/1c83OPC iCore3 购买链接: https://item.taobao.com/item.htm?id=5242294 ...