-
2021-05-26 14:41:39
记录一下线性布动态添加textview横向滑动
数据及判断
public void data() {
int width = 0;
list = new ArrayList<>();
for (int i = 0; i < 50; i++) {
list.add("14223355");
}
LayoutInflater inflater = getLayoutInflater();
int dex = 10;
for (int i = 0; i < list.size(); i++) {
TextView textView = (TextView) inflater.inflate(R.layout.tv_item_layout, null);
textView.setText(list.get(i));
textView.setOnClickListener(this);
container.addView(textView, i);
textView.measure(0, 0);
int temp = textView.getMeasuredWidth();
textView.setX(dex);
textView.setY(10);//控件之间的间隔,累计加10
dex += 10;
width += temp;
}
ViewGroup.LayoutParams params = container.getLayoutParams();
params.width = dex + width;
container.setLayoutParams(params);
}
点击事件:
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
TextView childView = (TextView) container.getChildAt(i);
if (v == childView) {
Toast.makeText(JsoupHtml.this, i + "", Toast.LENGTH_SHORT).show();
}
}
}
布局:
简单记录!
更多相关内容 -
Android 自定义LinearLayout实现滑动下拉抽屉的功能
2020-08-31 15:17:10所以,我们的目标就是自定义一个可滑动的LinearLayout,并且设置它的子布局都向上移动一个自定义LinearLayout的高度 一、自定义控件的测量和布局 自定义LinearLayout,假设为MyPullDownLayout: public class M先看效果图:
先来说说思路:我们把该页面分为两部分,分别是头部的抽屉布局(海洋色背景)和主内容布局(白色背景),这两部分的布局是呈线性关系,即抽屉在上,主页面在下,并且它们的父布局应该是一个可滑动的LinearLayout线性布局
所以,我们的目标就是自定义一个可滑动的LinearLayout,并且设置它的子布局都向上移动一个自定义LinearLayout的高度
一、自定义控件的测量和布局
自定义LinearLayout,假设为MyPullDownLayout:
public class MyPullDownLayout extends LinearLayout { public MyPullDownLayout(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } }
我们先对子控件进行简单的测量:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //测量子控件的大小 for(int i=0;i<getChildCount();i++){ View view=getChildAt(i); measureChild(view,widthMeasureSpec,heightMeasureSpec); } }
在onLayout中设置每一个子控件的位置:
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l,t, r, b); int height=getVerticalHeight(); //设置子控件的位置 for(int i=0;i<getChildCount();i++){ View view=getChildAt(i); view.layout(view.getLeft(),view.getTop()-height,view.getRight(),view.getBottom()-height); } } private int getVerticalHeight(){ return getHeight()-getPaddingBottom()-getPaddingTop(); }
int height = getVerticalHeight() 即为这个自定义控件的高度,并让每个子控件都向上移动一个height的高度
二、自定义控件的滑动实现
我们创建一个手势检测器(GestureDetector)来辅助我们滑动控件:
private int currentPos=0; private GestureDetector detector=new GestureDetector(new GestureDetector.SimpleOnGestureListener(){ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { //相对滑动:Y方向滑动多少距离,view就跟着滑动多少距离 //手指向上滑动 if(e2.getY()<e1.getY()&&getScrollY()>=0){ //滑到的y值超过0时,反弹回去 if(getScrollY()>0)scrollBy(0,-getScrollY()); //否则什么也不做 } else if(getScrollY()<=0){ //如果手指向下滑动并且没有超过抽屉页的滑动范围,就滑动页面 if(!(e2.getY()>e1.getY()&&getScrollY()==-getVerticalHeight())){ scrollBy(0,(int)distanceY); //手指向下滑动 if(e2.getY()>e1.getY())currentPos=getScrollY()/(getVerticalHeight()/4); //手指向上滑动 else currentPos=getScrollY()/(4*getVerticalHeight()/5); if(currentPos>0)currentPos=0; if(currentPos<-1)currentPos=-1; } } return super.onScroll(e1,e2,distanceX,distanceY); } }); @Override public boolean onTouchEvent(MotionEvent event) { //将event信息传给detector; detector.onTouchEvent(event); } private int getVerticalHeight(){ return getHeight()-getPaddingBottom()-getPaddingTop(); }
getScrollY():y轴方向已经滑动的距离。如果在抽屉页,则getScrollY()为负数
currentPos:手指离开屏幕后,控件所在的位置(抽屉页或者主页面)
currentPos=getScrollY()/(getVerticalHeight()/4):只需滑动1/4控件高度的距离就可以自动滑动到抽屉页
currentPos=getScrollY()/(4*getVerticalHeight()/5):只需滑动1-4/5=1/5控件高度的距离就可以自动滑动到主页面
创建Scorller对象来实现页面的自动滑动效果:
private Scroller scroller; public MyPullDownLayout(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } private void init(){ scroller=new Scroller(getContext()); } @Override public boolean onTouchEvent(MotionEvent event) { //将event信息传给detector; detector.onTouchEvent(event); switch (event.getAction()){ case MotionEvent.ACTION_UP: //手指离开屏幕后开始自动滑动 scroller.startScroll(0,getScrollY(),0,getVerticalHeight()*currentPos-getScrollY()); invalidate(); break; } return true; } @Override public void computeScroll() { if(scroller.computeScrollOffset()){ scrollTo(0,scroller.getCurrY()); postInvalidateDelayed(10); } }
scroller.startScroll():前两个参数是x,y方向已经滑动的距离,后两个参数是x,y方向还需要滑动的距离
computeScroll():利用scroller.getCurrY()每次滑动一点点距离,调用postInvalidateDelayed(10)后会回调该方法
三、XML布局文件注意事项
为了体现抽屉控件的宽高占整个父控件宽高的效果,需要把抽屉布局(Layout)的width和height设为match_parent。然后主页面布局部分就放在抽屉布局的下方,具体代码如下:
<?xml version="1.0" encoding="utf-8"?> <com.myviewtext.MyPullDownLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@mipmap/bg"> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:text="蜀道之难,难于上青天!" android:textColor="@color/white"/> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:text="蚕丛及鱼凫,开国何茫然!" android:textColor="@color/white"/> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:text="尔来四万八千岁,不与秦塞通人烟。西当太白有鸟道,可以横绝峨眉巅。" android:textColor="@color/white"/> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:text="地崩山摧壮士死,然后天梯石栈相钩连。" android:textColor="@color/white"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="臣本布衣,躬耕南阳,苟全性命于乱世,不求闻达于诸侯。"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,谘臣以当世之事,由是感激,遂许先帝以驱驰。"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="后值倾覆,受任于败军之际,奉命于危难之间:尔来二十有一年矣。"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧虑,恐付托不效,以伤先帝之明;故五月渡泸,深入不毛。"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="今南方已定,甲兵已足,当奖帅三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。"/> </com.hualinfo.myviewtext.MyPullDownLayout>
自定义LinearLayout完整代码如下:
public class MyPullDownLayout extends LinearLayout { private int currentPos=0; private Scroller scroller; public MyPullDownLayout(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //测量子控件的大小 for(int i=0;i<getChildCount();i++){ View view=getChildAt(i); measureChild(view,widthMeasureSpec,heightMeasureSpec); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l,t, r, b); int height=getVerticalHeight(); //设置子控件的位置 for(int i=0;i<getChildCount();i++){ View view=getChildAt(i); view.layout(view.getLeft(),view.getTop()-height,view.getRight(),view.getBottom()-height); } } private void init(){ scroller=new Scroller(getContext()); } private GestureDetector detector=new GestureDetector(new GestureDetector.SimpleOnGestureListener(){ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { //相对滑动:Y方向滑动多少距离,view就跟着滑动多少距离 //手指向上滑动 if(e2.getY()<e1.getY()&&getScrollY()>=0){ //滑到的y值超过0时,反弹回去 if(getScrollY()>0)scrollBy(0,-getScrollY()); //否则什么也不做 } else if(getScrollY()<=0){ //如果手指向下滑动并且没有超过抽屉页的滑动范围,就滑动页面 if(!(e2.getY()>e1.getY()&&getScrollY()==-getVerticalHeight())){ scrollBy(0,(int)distanceY); //手指向下滑动 if(e2.getY()>e1.getY())currentPos=getScrollY()/(getVerticalHeight()/4); //手指向上滑动 else currentPos=getScrollY()/(4*getVerticalHeight()/5); if(currentPos>0)currentPos=0; if(currentPos < -1)currentPos=-1; } } return super.onScroll(e1,e2,distanceX,distanceY); } }); @Override public boolean onTouchEvent(MotionEvent event) { //将event信息传给detector; detector.onTouchEvent(event); switch (event.getAction()){ case MotionEvent.ACTION_UP: scroller.startScroll(0,getScrollY(),0,getVerticalHeight()*currentPos-getScrollY()); invalidate(); break; } return true; } @Override public void computeScroll() { if(scroller.computeScrollOffset()){ scrollTo(0,scroller.getCurrY()); postInvalidateDelayed(10); } } private int getVerticalHeight(){ return getHeight()-getPaddingBottom()-getPaddingTop(); } }
本篇到这里就结束,下一篇我们来解决该自定义Layout与RecyclerView滑动冲突的问题:
-
让LinearLayout实现水平滑动,左右滑动
2015-12-30 17:25:30要做LinearLayout的左右滑动效果,以前知道垂直的可以在外面套一个scollview控件可以实现,本以为把linearLayout设成水平即可,结果发现太天真. 百度了一下只有垂直的答案,后面历经艰难险阻..看到一结果,用...要做LinearLayout的左右滑动效果,以前知道垂直的可以在外面套一个scollview控件可以实现,本以为把linearLayout设成水平即可,结果发现太天真.
百度了一下只有垂直的答案,后面历经艰难险阻..看到一结果,用HorizontalScrollView这个控件实现..于是试了下成功了.所以分享出来!
<HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#55000000" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#55000000" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#55000000" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#55000000" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#55000000" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:background="#55000000" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#55000000" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:background="#55000000" /> </LinearLayout> </HorizontalScrollView>
-
SlideLayout:带有滚动 ViewPager 和 LinearLayout 的滑动布局包含一些标记页面位置的点。 你可以控制
2021-06-05 23:31:15带有滚动 ViewPager 和 LinearLayout 的滑动布局包含一些标记页面位置的点。 您可以通过自定义属性来控制它。 演示 设置 依赖项 { compile project(':slideLayout') } 如何使用 您可以设置是否自动播放、点颜色和... -
【android学习】监听左右滑动的LinearLayout
2021-01-15 15:17:09public class BCustomerLinearLayout extends LinearLayout implements View.OnTouchListener { private static final String TAG = "CustomerLinearLayout"; public OnGestureChangeListener listener; ...public class BCustomerLinearLayout extends LinearLayout implements View.OnTouchListener { private static final String TAG = "CustomerLinearLayout"; public OnGestureChangeListener listener; public interface OnGestureChangeListener { void scrollLeft(); void scrollRight(); // void onClick(); } public void setOnGestureChangeListener(OnGestureChangeListener listener) { this.listener = listener; } public BCustomerLinearLayout(Context context) { this(context, null); } public BCustomerLinearLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BCustomerLinearLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); //setGestureListener(); //设置Touch监听 this.setOnTouchListener(this); //允许长按 this.setLongClickable(true); } @Override public boolean onTouch(View v, MotionEvent event) { final int action = event.getAction(); switch (action){ case MotionEvent.ACTION_DOWN: Log.e(TAG, "event down!"); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: Log.e(TAG, "event : up"); float dx = touchDownX - event.getX(); Log.e(TAG, "dx:" + dx); if (dx < -ViewConfiguration.get( getContext()).getScaledPagingTouchSlop()) { // Fling enough to move left Log.e(TAG, "snap right"); if (null!=listener){ listener.scrollRight(); } return true; } else if (dx > ViewConfiguration.get( getContext()).getScaledPagingTouchSlop()){ // Fling enough to move right Log.e(TAG, "snap left"); if (null!=listener){ listener.scrollLeft(); } return true; } else { Log.e(TAG, "snap click"); return super.onTouchEvent(event); // if (null!=listener){ // listener.onClick(); // } } } return super.onTouchEvent(event); } private boolean mScrolling; private float touchDownX; @Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touchDownX = event.getX(); mScrolling = false; break; case MotionEvent.ACTION_MOVE: //getScaledTouchSlop是一个距离,表示滑动的时候,手的移动要大于这个距离才识别为滑动事件 if (Math.abs(touchDownX - event.getX()) >= ViewConfiguration.get( getContext()).getScaledTouchSlop()) { mScrolling = true; } else { mScrolling = false; } break; case MotionEvent.ACTION_UP: mScrolling = false; break; } return mScrolling; } }
-
嵌套滑动的LinearLayout(滑动隐藏头部的LinearLayout),滑动时先隐藏头部(第一个子控件)再滑动...
2020-04-07 14:59:14recyclerview可滚动,滚动时如果头部仍在可见则先滚动LinearLayout直到头部消失,再开始滚动recyclerview。 思路:自定义LinearLayout实现NestedScrollingParent,getChildAt(0)获取头部, ... -
NestedScrollView嵌套LinearLayout无法滑动到底部问题解决
2020-05-22 16:10:56可以看到我们NestedScrollView里嵌套了LinearLayout,NestedScrollView无法滑动到底部,以至于看不到Button当我们去掉下面一行代码时就可以看到 app:layout_behavior="@string/appbar_scrolling_view_ -
自定义上下滑动的LinearLayout模拟解决内外同方向的滑动冲突
2019-11-01 09:43:43最近重新翻看开发艺术探索,发现内外同方向的滑动冲突案例代码不完整,所以决定补充完整。 activity_main的布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=... -
Android自定义控件ScrollView实现上下滑动功能
2020-08-27 03:45:30主要为大家详细介绍了Android自定义控件ScrollView实现上下滑动功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
Android 自定义LinearLayout实现滑动下拉抽屉的功能(解决滑动冲突)
2020-09-02 10:11:15本篇我们来解决上一篇的遗留问题:自定义LinearLayout与RecyclerView的滑动冲突。 没看过上一篇文章的,可以先去看一下:https://blog.csdn.net/zz51233273/article/details/108320445 先看效果图: 一、解决... -
LinearLayout 垂直滚动条
2015-10-31 15:56:07activity中经常只是一个LinearLayout,但这样的话,如果activity内容超过一屏,无法滚动查看下面的内容。 这时只需在外面嵌套一个ScrollView就可以了,直接贴代码吧 xmlns:android=... -
Android开发,LinearLayout无法滚动问题
2021-05-27 05:39:29刚开始学android,要做一...C是四个子页面,通过ViewPager实现A、B、C三个部分在同一个LinearLayout下现在的问题是,本人的ViewPager的每个page中内容很长,一个屏幕显示不全,所以希望它可以通过下拉来显示完全;同... -
继承Linearlayout的Viewgroup可以左右流畅滑动切换
2014-06-04 09:43:18继承Linearlayout的Viewgroup可以左右流畅滑动切换,类似viewpage -
android LinearLayout 垂直滚动
2021-05-27 05:39:24activity只使用LinearLayout,如果activity内容超过一屏,无法滚动查看下面的内容,这时需在外面嵌套一个ScrollView。xmlns:android=... -
Android实现可滑动的自定义日历控件
2021-01-21 19:57:22最近用到的一个日历控件,记录下,效果如图 ...布局文件 <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_...LinearLayout android:layout_width=match_par -
Android自定义LinearLayout实现左右侧滑菜单,完美兼容ListView、ScrollView、ViewPager等滑动控件
2016-06-14 17:04:20我们再次假象一下,我们现在正处于左菜单,此时我们向左滑动屏幕,如果红色框从0开始向右移动,如果超出了closeX这个临界值,就代表我们要滑出左菜单进入正文内容,这就是closeX的意思。 好了,理解了左... -
LinearLayout滑动页面展示不显示
2019-04-12 13:56:00子View占满全屏,屏幕外面的View宽高为0,需要自己重新测量一下 for (int i=0;i<getChildCount();i++){ View childView = getChildAt(i); measureChild(childView,widthMeasureSpec,heightMeasureSpec);... -
horizontalScrollView嵌套可横向滑动的布局
2015-11-20 10:37:02横向滑动的HorizontalScrollView嵌套GridView,实现单行横向滑动的Demo -
LinearLayout遇到的问题——利用LinearLayout做横向滑动冲突
2019-09-28 10:05:34问题:当我添加两个TextView的时候,然后滑动,发现只生成了一个TextView。 就是 <?xml version="1.0" encoding="utf-8"?> <... -
Linearlayout scrollbars=“vertical”并没有用,Linearlayout滚动的解决办法
2021-05-27 05:38:26Linearlayout 的内容经常会超出屏幕,虽然有scrollbars="vertical"参数可以设置,然而并没有用。正确的做法是在LinearLayout外嵌套一层ScrollView。android:layout_width="match_parent"android:layout_height=... -
解决android多层嵌套界面的滑动监听问题
2021-06-03 01:29:15就是android布局在一个界面上嵌套多层item,每次滑动时切换Activity时,被上层item挡住以至于触发不了滑动事件,触发不了onFling()函数我底层是LinearLayout,然后嵌套了ScrollView,最上面是一个ListView,监听了... -
Android零碎知识点(2)——使LinearLayout布局滚动
2020-06-19 21:06:48代码如下: 即在LinearLayout外面套一个ScrollView即可,而ScrollView里面的内容即可滚动。 运行如下,可以看到一个滚动条: -
纵向滑动的linearlayout动态添加ImageView并且ImageView点击放大
2014-12-05 06:17:38Listview 每个item里边有个linearlayout linearlayout动态添加了不定数量的照片 我要点击图片时放大点击的图片 这个事件要怎么写?