300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Android使用自定义ListView+ScrollView实现股票界面上下左右滑动

Android使用自定义ListView+ScrollView实现股票界面上下左右滑动

时间:2019-07-01 04:56:52

相关推荐

Android使用自定义ListView+ScrollView实现股票界面上下左右滑动

最近公司做股票相关软件,界面需求是这样的,横向无限长,可以横向滚动,而且最左侧的那一竖栏要定住,网上找了写例子,都太复杂,这里来一个简单的。上图:

一、图片有点瑕疵,不要在意细节。QAQ!自定义ScrollView,是先View的联动,需要重写onScrollChanged方法,然后对外暴露scrollTo方法,这样就可以让两个view联动,至于具体说法,可以去百度一下scrollTo的用法,上代码:

public class LinkageScrollView extends HorizontalScrollView{private View mView;public LinkageScrollView(Context context) {super(context);}public LinkageScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public LinkageScrollView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);if (mView != null){mView.scrollTo(l,t);}}public void setScrollView(View view){mView = view;}}

二、因为scrollView与ListView会有滑动冲突,需要重写onMeasure方法,上代码:

public class MyListView extends ListView{public MyListView(Context context) {super(context);}public MyListView(Context context, AttributeSet attrs) {super(context, attrs);}public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expendSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expendSpec);}}

三,最后就是关键的布局了,上代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.szsftxx.linkagescroll.MainActivity"android:orientation="vertical"><!-- 此部分是标题部分 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><!-- 左侧标题的父容器 --><LinearLayoutandroid:id="@+id/left_title_container"android:layout_width="100dp"android:layout_height="40dp"android:orientation="vertical"><include layout="@layout/layout_left_title" /></LinearLayout><!-- 右侧标题的父容器可实现水平滚动 --><com.szsftxx.linkagescroll.LinkageScrollViewandroid:id="@+id/sc_title"android:layout_width="match_parent"android:layout_height="40dp"android:scrollbars="none"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><include layout="@layout/layout_right_tab" /></LinearLayout></com.szsftxx.linkagescroll.LinkageScrollView></LinearLayout><!-- 此部分是内容部分 用ScrollView实现上下滚动效果 --><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:scrollbars="none"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><!-- 左侧内容的父容器 --><LinearLayoutandroid:id="@+id/left_container"android:layout_width="100dp"android:layout_height="match_parent"android:gravity="center_vertical"android:orientation="vertical"><com.szsftxx.linkagescroll.MyListViewandroid:id="@+id/left_list"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout><!-- 右侧内容的父容器 实现水平滚动 --><com.szsftxx.linkagescroll.LinkageScrollViewandroid:id="@+id/sc_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:scrollbars="none"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_vertical"android:orientation="horizontal"><com.szsftxx.linkagescroll.MyListViewandroid:id="@+id/content_list"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout></com.szsftxx.linkagescroll.LinkageScrollView></LinearLayout></ScrollView></LinearLayout>

好了,就这么简单,其他的布局以及ListView的Adapter就不贴了,都是老司机自己领会。

附源码

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。