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 ...
随机推荐
- [CareerCup] 15.3 Renting Apartment III 租房之三
Building #11 is undergoing a major renovation. Implement a query to close all requests from apartmen ...
- android-ProgressBar
制定ProgressBar显示风格 * 参考系统自带的进度条 * ProgressBar分类 * 可以精确显示进度(可以显示刻度和百分比) * 不可以精确显示进度 * 标题上ProgressBar的设 ...
- html5 canvas画图之图形随拖动而复制(有操作指示)
学习html5 canvas,写了一个小练习来加深理解,可以实现图形随拖动而复制. <!DOCTYPE html> <html> <head> <meta c ...
- Hibernate的基本查询语句
1.最简单的查询 List<Special> specials = (List<Special>)session.createQuery("select spe fr ...
- sass学习笔记1
less在处理CSS动画时,非常恶心,决定转向sass了.sass诞生得比less早,只是因为它是ruby写的,因此受众面够少.但我们不需要自己下编译器或使用命令行,我们可以koala这神器 首先几个 ...
- Oracle手工建库
环境准备 手工建库的前提是ORACLE软件已经正确安装到操作系统中,只是需要我们利用ORACLE软件提供的一些工具和脚本来创建一个数据库,创建这个数据库可以运行DBCA工具图形化创建,也可以使用CRE ...
- 自动删除Mysql备份(数组+for)
#!/bin/bash #author:V #Dispaly:auto delete mysql backup. BACKDIR=(/home/11/mysqlbackup/ /home/full/) ...
- innodb_buffer_pool_size 大小建议
innodb_buffer_pool_size参数大小建议: 查看Innodb_buffer_pool_pages_data大小,即已使用页面 MySQL> SHOW GLOBAL STATUS ...
- C++ Primer Pluse_7_课后题
#include <iostream> using namespace std; double Sum2(double x, double y) { double sum = 0; if ...
- 多线程 - CyclicBarrier
一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 CyclicBarri ...