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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/wb_001/article/details/82910449

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

一、图片有点瑕疵,不要在意细节。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);
    }

    @Override
    protected 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);
    }

    @Override
    protected 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="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.szsftxx.linkagescroll.MainActivity"
    android:orientation="vertical">
    <!-- 此部分是标题部分 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <!-- 左侧标题的父容器 -->

        <LinearLayout
            android: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.LinkageScrollView
            android:id="@+id/sc_title"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:scrollbars="none">

            <LinearLayout
                android: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实现上下滚动效果 -->

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <!-- 左侧内容的父容器 -->

            <LinearLayout
                android:id="@+id/left_container"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="vertical">

                <com.szsftxx.linkagescroll.MyListView
                    android:id="@+id/left_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </LinearLayout>

            <!-- 右侧内容的父容器 实现水平滚动 -->

            <com.szsftxx.linkagescroll.LinkageScrollView
                android:id="@+id/sc_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <com.szsftxx.linkagescroll.MyListView
                        android:id="@+id/content_list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"/>
                </LinearLayout>
            </com.szsftxx.linkagescroll.LinkageScrollView>
        </LinearLayout>
    </ScrollView>


</LinearLayout>

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

猜你喜欢

转载自blog.csdn.net/wb_001/article/details/82910449