android实现上下左右滑动界面布局

android实现上下左右滑动界面布局,第1张

概述android实现上下左右滑动界面布局 本文实例为大家分享了android实现滑动界面布局的具体代码,供大家参考,具体内容如下 1.我使用的是ScrollView嵌套HorizontalScrollView让ScrollView负责上下滑动HorizontalScrollView负责左右滑动 2.以下代码提供了思路和完成手段,请根据具体业务去进行修改,我试过使用recyclerview进行自定义,发现一旦有了复杂业务之后会掉帧卡顿所以使用了这种方法 XML布局 <?xml version="1.0" enco ...

本文实例为大家分享了androID实现滑动界面布局的具体代码,供大家参考,具体内容如下

1.我使用的是ScrollVIEw嵌套horizontalscrollview让ScrollVIEw负责上下滑动horizontalscrollview负责左右滑动

2.以下代码提供了思路和完成手段,请根据具体业务去进行修改,我试过使用recyclervIEw进行自定义,发现一旦有了复杂业务之后会掉帧卡顿所以使用了这种方法

XML布局

<?xml version="1.0" enCoding="utf-8"?>  <test.smartonet.com.myapplication.PagerScrollVIEw    xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    androID:ID="@+ID/main"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent">    <linearLayout      androID:layout_wIDth="match_parent"      androID:layout_height="match_parent"      androID:orIEntation="vertical">      <horizontalscrollview        androID:ID="@+ID/hor"        androID:layout_wIDth="match_parent"        androID:background="#e1f1f1"        androID:layout_height="160dp">        <linearLayout          androID:layout_wIDth="match_parent"          androID:layout_height="match_parent"          androID:layout_gravity="center|left"          androID:gravity="center"          androID:orIEntation="horizontal">          <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A2"/> <TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A3"/> <TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A4"/> <TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A5"/><TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A6"/><TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A1"/> <TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A2"/> <TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A3"/> <TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A4"/> <TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A5"/><TextVIEw          androID:layout_wIDth="160dp"          androID:layout_height="60dp"          androID:gravity="center"          androID:text="A6"/>        </linearLayout>      </horizontalscrollview>      <horizontalscrollview        androID:ID="@+ID/hor1"        androID:layout_wIDth="match_parent"        androID:scrollbars="none"        androID:background="#a2f2f2"        androID:layout_height="160dp">        <linearLayout          androID:layout_wIDth="match_parent"          androID:layout_height="match_parent"          androID:layout_gravity="center|left"          androID:gravity="center"          androID:orIEntation="horizontal">          <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/>        </linearLayout>      </horizontalscrollview> <horizontalscrollview        androID:ID="@+ID/hor2"        androID:layout_wIDth="match_parent"        androID:scrollbars="none"        androID:background="#a2a2f2"        androID:layout_height="160dp">        <linearLayout          androID:layout_wIDth="match_parent"          androID:layout_height="match_parent"          androID:layout_gravity="center|left"          androID:gravity="center"          androID:orIEntation="horizontal">          <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/>        </linearLayout>      </horizontalscrollview>      <horizontalscrollview        androID:ID="@+ID/hor3"        androID:layout_wIDth="match_parent"        androID:scrollbars="none"        androID:background="#f3f3f3"        androID:layout_height="160dp">        <linearLayout          androID:layout_wIDth="match_parent"          androID:layout_height="match_parent"          androID:layout_gravity="center|left"          androID:gravity="center"          androID:orIEntation="horizontal">          <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/>        </linearLayout>      </horizontalscrollview>      <horizontalscrollview        androID:ID="@+ID/hor4"        androID:layout_wIDth="match_parent"        androID:scrollbars="none"        androID:background="#a4f4f4"        androID:layout_height="200dp">        <linearLayout          androID:layout_wIDth="match_parent"          androID:layout_height="match_parent"          androID:layout_gravity="center|left"          androID:gravity="center"          androID:orIEntation="horizontal">          <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/>        </linearLayout>      </horizontalscrollview>      <horizontalscrollview        androID:ID="@+ID/hor5"        androID:layout_wIDth="match_parent"        androID:scrollbars="none"        androID:background="#f5f5f5"        androID:layout_height="200dp">        <linearLayout          androID:layout_wIDth="match_parent"          androID:layout_height="match_parent"          androID:layout_gravity="center|left"          androID:gravity="center"          androID:orIEntation="horizontal">          <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/>        </linearLayout>      </horizontalscrollview>      <horizontalscrollview        androID:ID="@+ID/hor6"        androID:layout_wIDth="match_parent"        androID:scrollbars="none"        androID:background="#a5f5f5"        androID:layout_height="200dp">        <linearLayout          androID:layout_wIDth="match_parent"          androID:layout_height="match_parent"          androID:layout_gravity="center|left"          androID:gravity="center"          androID:orIEntation="horizontal">          <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A1"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A2"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A3"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A4"/> <TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A5"/><TextVIEw            androID:layout_wIDth="160dp"            androID:layout_height="60dp"            androID:gravity="center"            androID:text="A6"/>        </linearLayout>      </horizontalscrollview>    </linearLayout></test.smartonet.com.myapplication.PagerScrollVIEw>

mainAvtivity的代码

package test.smartonet.com.myapplication;import androID.annotation.Suppresslint;import androID.content.Context;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.horizontalscrollview;import androID.Widget.linearLayout;import androID.Widget.ScrollVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import org.w3c.dom.Text;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.List;/** * zhaohan creat 2019/3/6 */public class MainActivity extends AppCompatActivity {  String TAG = "主程序";  float mPosX;  float mPosY;  float mCurPosX;  float mCurPosY;  int offset=10;//偏移量  int friction = 10;//摩擦力  final List<horizontalscrollview> List = new ArrayList();  @Suppresslint("ClickableVIEwAccessibility")  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    final PagerScrollVIEw scrollVIEw = findVIEwByID(R.ID.main);    List.add((horizontalscrollview) findVIEwByID(R.ID.hor));    List.add((horizontalscrollview) findVIEwByID(R.ID.hor1));    List.add((horizontalscrollview) findVIEwByID(R.ID.hor2));    List.add((horizontalscrollview) findVIEwByID(R.ID.hor3));    List.add((horizontalscrollview) findVIEwByID(R.ID.hor4));    List.add((horizontalscrollview) findVIEwByID(R.ID.hor5));    List.add((horizontalscrollview) findVIEwByID(R.ID.hor6));    for(horizontalscrollview horizontalscrollview:List){      horizontalscrollview.setontouchListener(new VIEw.OntouchListener() {        @OverrIDe        public boolean ontouch(VIEw v,MotionEvent event) {          switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:              mPosX = event.getX();              mPosY = event.getY();              Log.w(TAG,mPosY+"");              break;            case MotionEvent.ACTION_MOVE:              mCurPosX = event.getX();              mCurPosY = event.getY();            case MotionEvent.ACTION_UP:              if (mCurPosX - mPosX > 0 && (Math.abs(mCurPosX - mPosX) > offset)) {                int speed = 10;//速度                int distance = 20;//滑动距离                if((Math.abs(mCurPosX - mPosX))>50){                  speed = (int) Math.abs(mCurPosX - mPosX)/friction;                  distance = (int) Math.abs(mCurPosX - mPosX)/3;//滑动距离                }                //向右滑動                Log.w(TAG,"向右滑动了!");                while(distance>0) {                  distance = distance - speed;                  for(horizontalscrollview horizontalscrollview:List){                    horizontalscrollview.smoothScrollBy(distance,0);                  }                }                return true;//true为屏蔽范围内的其他滑动监听              } else if (mCurPosX - mPosX < 0 && (Math.abs(mCurPosX - mPosX) > offset)) {                int speed = 10;//速度                int distance = 20;//滑动距离                if((Math.abs(mCurPosX - mPosX))>50){                  speed = (int) Math.abs(mCurPosX - mPosX)/friction;                  distance = (int) Math.abs(mCurPosX - mPosX)/3;//滑动距离                }                //向左滑动                Log.w(TAG,"向左滑动了!");                //改为滑动                while(distance>0){                  distance=distance-speed;                  for(horizontalscrollview horizontalscrollview:List){                    horizontalscrollview.smoothScrollBy(-distance,0 );                  }                }                return true;//true为屏蔽范围内的其他滑动监听              }              break;          }          return true;//true为屏蔽范围内的其他滑动监听        }      });    }  }}class PagerScrollVIEw extends ScrollVIEw {  private float xdistance,ydistance,xLast,yLast;  public PagerScrollVIEw(Context context) {    super(context);  }  public PagerScrollVIEw(Context context,AttributeSet attrs) {    super(context,attrs);  }  public PagerScrollVIEw(Context context,AttributeSet attrs,int defStyle) {    super(context,attrs,defStyle);  }  @OverrIDe  public boolean onIntercepttouchEvent(MotionEvent ev) {    switch (ev.getAction()) {      case MotionEvent.ACTION_DOWN:        xdistance = ydistance = 0f;        xLast = ev.getX();        yLast = ev.getY();        break;      case MotionEvent.ACTION_MOVE:        final float curX = ev.getX();        final float curY = ev.getY();        xdistance += Math.abs(curX - xLast);        ydistance += Math.abs(curY - yLast);        xLast = curX;        yLast = curY;        /**         * X轴滑动距离大于Y轴滑动距离,也就是用户横向滑动时,返回false,ScrollVIEw不处理这次事件,         * 让子控件中的touchEvent去处理,所以横向滑动的事件交由VIEwPager处理,         * ScrollVIEw只处理纵向滑动事件         */        if (xdistance > ydistance) {          return false;        }    }    return super.onIntercepttouchEvent(ev);  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

总结

以上是内存溢出为你收集整理的android实现上下左右滑动界面布局全部内容,希望文章能够帮你解决android实现上下左右滑动界面布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144248.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存