android横向滑动选择的view

做文字编辑,从网上找来的。

HorizontalScrollSelectView:
    public boolean mAlwaysOverrideTouch = true;protected ListAdapter mAdapter;private int mLeftViewIndex = -1;private int mRightViewIndex = 0;protected int mCurrentX;protected int mNextX;private int mMaxX = Integer.MAX_VALUE;private int mDisplayOffset = 0;protected Scroller mScroller;private GestureDetector mGesture;private Queue<View> mRemovedViewQueue = new LinkedList<View>();private OnItemSelectedListener mOnItemSelected;private OnItemClickListener mOnItemClicked;private OnItemLongClickListener mOnItemLongClicked;private OnScrollListener mScrollListener;/*** 选中item时图片*/private Drawable mDrawable;private boolean mDataChanged = false;private Context context;private boolean scrollerFalg1 = false;private boolean scrollerFalg2 = false;private int position = 0x7f020000;public HorizontalScrollSelectView(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;initView();}private synchronized void initView() {mLeftViewIndex = -1;mRightViewIndex = 0;mDisplayOffset = 0;mCurrentX = 0;mNextX = 0;mMaxX = Integer.MAX_VALUE;mScroller = new Scroller(getContext());mGesture = new GestureDetector(getContext(), mOnGesture);}public void setMScrollListener(OnScrollListener listener) {mScrollListener = listener;}@Overridepublic void setOnItemSelectedListener(OnItemSelectedListener listener) {mOnItemSelected = listener;}@Overridepublic void setOnItemClickListener(OnItemClickListener listener) {mOnItemClicked = listener;}@Overridepublic void setOnItemLongClickListener(OnItemLongClickListener listener) {mOnItemLongClicked = listener;}/*** 设置选中状态时的图片** @param mDrawable*/public void setSelectBitmap(Drawable mDrawable) {this.mDrawable = mDrawable;}private DataSetObserver mDataObserver = new DataSetObserver() {@Overridepublic void onChanged() {synchronized (HorizontalScrollSelectView.this) {mDataChanged = true;}invalidate();requestLayout();}@Overridepublic void onInvalidated() {reset();invalidate();requestLayout();}};@Overridepublic ListAdapter getAdapter() {return mAdapter;}@Overridepublic View getSelectedView() {//TODO: implementreturn null;}@Overridepublic void setAdapter(ListAdapter adapter) {if (mAdapter != null) {mAdapter.unregisterDataSetObserver(mDataObserver);}mAdapter = adapter;mAdapter.registerDataSetObserver(mDataObserver);reset();}private synchronized void reset() {initView();removeAllViewsInLayout();requestLayout();}@Overridepublic void setSelection(int position) {//TODO: implementint positionX = position * this.getWidth();int maxWidth = this.getChildCount() * this.getWidth();if (positionX <= 0) {positionX = 0;}if (positionX > maxWidth) {positionX = maxWidth;}scrollTo(positionX);}private void addAndMeasureChild(final View child, int viewPos) {LayoutParams params = child.getLayoutParams();if (params == null) {params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);}addViewInLayout(child, viewPos, params, true);child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));}@SuppressLint("NewApi")@Overrideprotected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);if (mAdapter == null) {return;}if (mDataChanged) {int oldCurrentX = mCurrentX;initView();removeAllViewsInLayout();mNextX = oldCurrentX;mDataChanged = false;}if (mScroller.computeScrollOffset()) {int scrollx = mScroller.getCurrX();mNextX = scrollx;}//		if(mNextX <= 0){
//			mNextX = 0;
//			mScroller.forceFinished(true);
//		}
//		if(mNextX >= mMaxX) {
//			mNextX = mMaxX;
//			mScroller.forceFinished(true);
//		}int dx = mCurrentX - mNextX;removeNonVisibleItems(dx);fillList(dx);positionItems(dx);
//		Log.e("onlayout", "mLeftViewIndex"+(mLeftViewIndex+1));mCurrentX = mNextX;if (!mScroller.isFinished()) {post(new Runnable() {@Overridepublic void run() {requestLayout();}});} else {if (scrollerFalg1 || (!scrollerFalg1 && scrollerFalg2)) {View chid = getChildAt(0);if (chid != null) {mDisplayOffset = 0;positionItems(0);if (mDrawable != null) {getChildAt(2).setBackground(mDrawable);}if (mScrollListener != null) {int position = (int) getChildAt(2).getTag(this.position);mScrollListener.onScrollSelectItem(this, position);}}}}}/*** 获取屏幕宽度** @param context* @return*/public static int getSecreenWidth(Context context) {DisplayMetrics dm = new DisplayMetrics();dm = context.getResources().getDisplayMetrics();int screenWidth = dm.widthPixels;return screenWidth / 5;}private void fillList(final int dx) {int edge = 0;View child = getChildAt(0);if (child != null) {edge = child.getLeft();}fillListLeft(edge, dx);edge = 0;child = getChildAt(getChildCount() - 1);if (child != null) {edge = child.getRight();}fillListRight(edge, dx);}private void fillListRight(int rightEdge, final int dx) {//&& mRightViewIndex < mAdapter.getCount()while (rightEdge + dx < getWidth()) {if (mRightViewIndex >= mAdapter.getCount()) {mRightViewIndex = 0;}View child = mAdapter.getView(mRightViewIndex, mRemovedViewQueue.poll(), this);child.setTag(position, mRightViewIndex);addAndMeasureChild(child, -1);rightEdge += child.getMeasuredWidth();//			if(mRightViewIndex == mAdapter.getCount()-1) {
//				mMaxX = mCurrentX + rightEdge - getWidth();
//			}
//			
//			if (mMaxX < 0) {
//				mMaxX = 0;
//			}mRightViewIndex++;}}private void fillListLeft(int leftEdge, final int dx) {//&& mLeftViewIndex >= 0while (leftEdge + dx > 0) {if (mLeftViewIndex <= -1) {mLeftViewIndex = mAdapter.getCount() - 1;}View child = mAdapter.getView(mLeftViewIndex, mRemovedViewQueue.poll(), this);child.setTag(position, mLeftViewIndex);addAndMeasureChild(child, 0);leftEdge -= child.getMeasuredWidth();mLeftViewIndex--;mDisplayOffset -= child.getMeasuredWidth();}}private void removeNonVisibleItems(final int dx) {View child = getChildAt(0);while (child != null && child.getRight() + dx <= 0) {mDisplayOffset += child.getMeasuredWidth();mRemovedViewQueue.offer(child);removeViewInLayout(child);mLeftViewIndex++;if (mLeftViewIndex >= mAdapter.getCount()) {mLeftViewIndex = 0;}child = getChildAt(0);}child = getChildAt(getChildCount() - 1);while (child != null && child.getLeft() + dx >= getWidth()) {mRemovedViewQueue.offer(child);removeViewInLayout(child);mRightViewIndex--;if (mRightViewIndex <= -1) {mRightViewIndex = mAdapter.getCount() - 1;}child = getChildAt(getChildCount() - 1);}}private void positionItems(final int dx) {if (getChildCount() > 0) {mDisplayOffset += dx;int left = mDisplayOffset;for (int i = 0; i < getChildCount(); i++) {View child = getChildAt(i);getChildAt(2).setBackground(null);int childWidth = child.getMeasuredWidth();child.layout(left, 0, left + childWidth, child.getMeasuredHeight());left += childWidth + child.getPaddingRight();}}}public synchronized void scrollTo(int x) {mScroller.startScroll(mNextX, 0, x - mNextX, 0);requestLayout();}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {boolean handled = super.dispatchTouchEvent(ev);handled |= mGesture.onTouchEvent(ev);switch (ev.getAction()) {case MotionEvent.ACTION_UP:scrollerFalg2 = true;requestLayout();break;default:break;}return handled;}protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {synchronized (HorizontalScrollSelectView.this) {//mNextXmScroller.fling(mNextX, 0, (int) -velocityX, 0, Integer.MIN_VALUE, mMaxX, 0, 0);}requestLayout();scrollerFalg1 = true;return true;}protected boolean onDown(MotionEvent e) {mScroller.forceFinished(true);return true;}public interface OnScrollListener {/*** 互动过程中选中的item** @param position*/public void onScrollSelectItem(ViewGroup viewGroup, int position);}private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {@Overridepublic boolean onDown(MotionEvent e) {return HorizontalScrollSelectView.this.onDown(e);}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {return HorizontalScrollSelectView.this.onFling(e1, e2, velocityX, velocityY);}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {synchronized (HorizontalScrollSelectView.this) {mNextX += (int) distanceX;}requestLayout();scrollerFalg1 = false;scrollerFalg2 = false;return true;}@Overridepublic void onShowPress(MotionEvent e) {super.onShowPress(e);}@Overridepublic boolean onSingleTapUp(MotionEvent e) {Log.e("onSingleTapUp", "mLeftViewIndex" + (mLeftViewIndex + 1));
//			scrollerFalg=true;return super.onSingleTapUp(e);}@Overridepublic boolean onSingleTapConfirmed(MotionEvent e) {for (int i = 0; i < getChildCount(); i++) {View child = getChildAt(i);if (isEventWithinView(e, child)) {if (mOnItemClicked != null) {mOnItemClicked.onItemClick(HorizontalScrollSelectView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i));}if (mOnItemSelected != null) {mOnItemSelected.onItemSelected(HorizontalScrollSelectView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i));}break;}}return true;}@Overridepublic void onLongPress(MotionEvent e) {int childCount = getChildCount();for (int i = 0; i < childCount; i++) {View child = getChildAt(i);if (isEventWithinView(e, child)) {if (mOnItemLongClicked != null) {mOnItemLongClicked.onItemLongClick(HorizontalScrollSelectView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i));}break;}}}private boolean isEventWithinView(MotionEvent e, View child) {Rect viewRect = new Rect();int[] childPosition = new int[2];child.getLocationOnScreen(childPosition);int left = childPosition[0];int right = left + child.getWidth();int top = childPosition[1];int bottom = top + child.getHeight();viewRect.set(left, top, right, bottom);return viewRect.contains((int) e.getRawX(), (int) e.getRawY());}};</span>
demo截图:


demo地址:http://download.csdn.net/detail/s569646547/9070919


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/s569646547/p/4784664.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/374556.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

excel使用教程_数据分析Excel必备技能:数据透视表使用教程

江米小枣tonylua | 作者掘金 | 来源处理数量较大的数据时&#xff0c;一般分为数据获取、数据筛选&#xff0c;以及结果展示几个步骤。在 Excel 中&#xff0c;我们可以利用数据透视表(Pivot Table)方便快捷的实现这些工作。本文首先手把手的教你如何在 Excel 中手动构建一个基…

java调用webservice_笃学私教:Java开发网站架构演变过程-从单体应用到微服务架构详解...

原标题&#xff1a;笃学私教&#xff1a;Java开发网站架构演变过程-从单体应用到微服务架构详解Java开发网站架构演变过程&#xff0c;到目前为止&#xff0c;大致分为5个阶段&#xff0c;分别为单体架构、集群架构、分布式架构、SOA架构和微服务架构。下面玄武老师来给大家详细…

专2-第二课 Eclipse开发环境搭建

2.1下载Eclipse 2.2 安装C/C版本的Eclipse 2.3 安装JDT插件开发Java程序 2.4 使用Eclipse开发驱动程序 既然安装了eclipse来进行驱动学习&#xff0c;那么我们就先来试试看eclipse开发驱动的大致流程。这里以Linux设备驱动作为示列给读者展示整个流程&#xff0c;Android底层的…

tkinter 菜单添加事件_Tasker的最新测试劫持了Android 11的电源菜单

流行的Android自动化应用Tasker 最近收到了重大更新&#xff0c;为该应用引入了许多新功能。该更新包括解锁应用程序读取手机上任何传感器以触发任务的功能&#xff0c;使您可以通过任何第三方应用程序自动发送短信或拨打电话的功能&#xff0c;完全请勿打扰自定义功能。通过链…

CLR via C#(18)——Enum

1. Enum定义 枚举类型是经常用的一种“名称/值”的形式&#xff0c;例如&#xff1a; public enum FeedbackStatus { New, Processing, Verify, Closed } 定义枚举类型之后我们在使用时方便了许多&#xff0c;不用再记着0代表什么状态…

电力电子、电机控制系统的建模和仿真_清华团队研发,首款国产电力电子仿真软件来啦~已捐赠哈工大、海工大、清华使用!...

点击上方电气小青年&#xff0c;关注并星标由于微信改版&#xff0c;只有星标才能及时看到我们的消息哦━━━━━━推荐阅读&#xff1a;《膜拜大神&#xff01;清华大学电机系2021年接收推荐免试直硕(博)生拟录取名单公示&#xff01;》《滴滴程序员年薪80万被鄙视不如在二本…

可视化分析之图表选择

转载于:https://www.cnblogs.com/yymn/p/4783631.html

453. 最小操作次数使数组元素相等

给你一个长度为 n 的整数数组&#xff0c;每次操作将会使 n - 1 个元素增加 1 。返回让数组所有元素相等的最小操作次数。 class Solution {public int minMoves(int[] nums) {int res 0;int sum 0;int n nums.length;for(int i 0;i<n;i){sum nums[i];}res sum - min…

第二章 TCP/IP 基础知识

第二章 TCP/IP 基础知识 TCP/IP transmission control protocol and ip internet protocol 是互联网众多通信协议中最为著名的。 2.2 TCP/IP 的标准化 2.2.2 TCP/IP 标准化精髓 TCP/IP 协议始终具有很强的实用性。 相比于TCP/IP &#xff0c;OSI 之所以未能达到普及&#xff0…

gorm preload 搜索_LeetCode刷题笔记|95:不同的二叉搜索树 II

题目描述给定一个整数 n&#xff0c;生成所有由 1 ... n 为节点所组成的 二叉搜索树 。示例输入&#xff1a;3输出&#xff1a;[[1,null,3,2],[3,2,null,1],[3,1,null,null,2],[2,1,3],[1,null,2,null,3]]解释&#xff1a;以上的输出对应以下 5 种不同结构的二叉搜索树&#xf…

GTJ2018如何导出全部工程量_如何成为优秀的造价员?广联达编制内刊手册,造价员算量高手秘籍...

如何成为优秀的造价员&#xff1f;广联达编制内刊手册&#xff0c;造价员算量高手秘籍[高手秘籍]是广联达课程编制委员会暨直播委员会精心打造的&#xff0c;能够“让您深入理解软件计算、设置等原理,遇到问题有处理思路,以常见问题为导向&#xff0c;重点进行原因分析&#xf…

带有Spring,Hibernate,Akka,Twitter Bootstrap,Apache Tiles和jQuery的Maven Web项目Kickstarter代码库...

我很高兴将第二个项目上传到GitHub&#xff0c;以帮助人们尽快开始Java Web App开发。 我正在与Apache License 2.0共享此代码。 这是相同的网址&#xff1a; https://github.com/ykameshrao/spring-hibernate-springdata-springmvc-maven-project-framework 该项目包括以下部…

C++服务器设计(七):聊天系统服务端实现

在之前的章节中&#xff0c;我们对服务端系统的设计实现原理进行了剖析&#xff0c;在这一章中&#xff0c;我们将对服务端框架进行实际运用&#xff0c;实现一款运行于内网环境的聊天系统。该聊天系统由客户端与服务器两部分组成&#xff0c;同时服务端通过数据库维护用户的账…

高校实验室管理系统_史上最全面的实验室信息管理系统(LIMS)全解

1. LIMS的基本概念和发展状况1.1 概括LIMS实验室管理系统是为实验、检测等业务板块提供流程化、模块化、标准化操作管理系统&#xff0c;打造基于行业法规的实验室全流程质量控制管理系统&#xff0c;实现实验室“人、机、料、法、环”关键环节管理。1.2 发展状况随着科研规范化…

配置了坐标还是找不到serv_为什么老人家总是这疼那疼,还找不到原因?是矫情还是另有原因...

“哎呀&#xff0c;怎么回事&#xff0c;腰痛腿痛的&#xff0c;痛的我一晚上都没怎么睡觉&#xff0c;怎么回事昨天也没干嘛啊&#xff01;”对于这一句话&#xff0c;相信很多朋友都不是很陌生。这句话是来自于一位网友的留言&#xff0c;而说这话的真是她的妈妈。这也是很多…

qopenglwidget 透明_廊坊透明真空袋用途-祺泰包装

功能方面&#xff1a;平面真空袋抽真空后易形成不平整&#xff0c;不均匀的现象。目前&#xff0c;真空包装主要用于食品的包装&#xff0c;如肉类、谷类加工食品以及易氧化变质的食品&#xff0c;也可用于机械零件、仪器和羽绒制品、毛制品等蓬松制品的包装。在超shi中&#x…

使用Spring Data Neo4j进行领域建模

大家好&#xff0c;威利在这里。 上次我告诉您&#xff0c;我正在使用Neo4j和Spring Data Neo4j构建Skybase CMDB&#xff0c;我很高兴收到很多对此的积极反馈。 我展示了一些代码&#xff0c;但没有那么多。 在本文中&#xff0c;我将向您展示如何使用Spring Data Neo4j在Skyb…

linux多线程_Java+Linux,深入内核源码讲解多线程之进程

之前写了两篇文章&#xff0c;都是针对Linux这个系统的&#xff0c;为什么?我为什么这么喜欢写这个系统的知识&#xff0c;可能就是为了今天的内容多线程系列&#xff0c;现在多线程不是一个面试重点 啊&#xff0c;那如果你能深入系统内核回答这个知识点&#xff0c;面试官会…

ipad连接电脑_这些应用让iPad生产力分分钟UP

IT时报见习记者 钱奕昀用iPad办公这件事&#xff0c;多年前网友就在讨论&#xff0c;最常见的还是那句“买前生产力&#xff0c;买后爱奇艺”。很长一段时间里&#xff0c;它的生产力属性都是弱于娱乐属性的。其实&#xff0c;作为PC端和移动端的形态中和&#xff0c;iPad可以…

Mac OSX 快捷键命令行

ctrlshift 快速放大dock的图标会暂时放大&#xff0c;而如果你开启了dock放大CommandOptionW 将所有窗口关闭CommandW 将当前窗口关闭(可以关闭Safari标签栏,很实用) CommandOptionM …