HorizontalScrollView的使用

本文详细介绍HorizontalScrollView在Android应用中如何实现水平滑动效果,包括XML布局配置、Java代码使用及自定义HorizontalScrollView的方法,适用于数据展示受限于屏幕宽度时的场景。

                                          HorizontalScrollView的使用

应用场景:

       有一些数据要用纵向的列表显示出来,但是由于空间的限制,每一行并不能完全显示出来,那这个时候就希望按左右键可以滑动列表行的数据,这时HorizontalScrollView就派上用场了。

使用方法:

1)XML里的定义

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/px_zero"
        android:layout_weight="5">

        <com.amlogic.dvb_custom_view.MyHorizontalScrollView
            android:id="@+id/hs_scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/px_10.0"
            android:layout_marginRight="@dimen/px_10.0"
            android:scrollbars="horizontal">

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

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/library_recyclerview"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_margin="@dimen/px_20.0"
                    android:background="@color/transparent" />
            </LinearLayout>
        </com.amlogic.dvb_custom_view.MyHorizontalScrollView>

        <Button
            android:id="@+id/btn_space"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/transparent" />

        <RelativeLayout
            android:id="@+id/buffer_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:orientation="horizontal"
            android:visibility="invisible">

            <ProgressBar
                android:id="@+id/progress_bar_load"
                style="@style/MyProgressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/progress_bar_load"
                android:layout_centerHorizontal="true"
                android:textSize="@dimen/px_33.0" />
        </RelativeLayout>
    </RelativeLayout>

需要注意的是要用RelativeLayout包住HorizontalScrollView。

2)java里的使用

//初始化
mLibraryRecyclerView = getView().findViewById(R.id.library_recyclerview);
LinearLayoutManager layoutManager_items = new LinearLayoutManager(getContext());
layoutManager_items.setOrientation(LinearLayoutManager.VERTICAL);
mLibraryRecyclerView.setLayoutManager(layoutManager_items);
mLibraryRecyclerViewAdapter = new LibraryRecycleViewAdapter(getContext(), mServiceList, MODE_NONE);

//滚动视图
mHorizontalScrollView = getView().findViewById(R.id.hs_scroll);
//recyvlerView的按键监听
mLibraryRecyclerViewAdapter.setOnKeyListener(new OnRecyclerViewItemKeyListener() {
            @Override
            public boolean recyclerViewItemOnKey(View view, int keyCode, KeyEvent event, int position) {
                if (event.getAction() == KeyEvent.ACTION_UP) {
                    return false;
                }

                switch (keyCode) {
                    //已经滑动到了最左侧
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        if (mHorizontalScrollView.isScrolledLeftEnd()) {
                            return true;
                        } else {
                            return false;
                        }

                    //已经滑动到了最右侧
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        if (mHorizontalScrollView.isScrolledRightEnd()) {
                            return true;
                        } else {
                            return false;
                        }

                    default:
                        return false;
                }
                
                return false;
              }
            };

3)自定义的HorizontalScrollView

package com.amlogic.dvb_custom_view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

public class MyHorizontalScrollView extends HorizontalScrollView {
    private boolean mScrolledLeftEnd = false;
    private boolean mScrolledRightEnd = false;

    public MyHorizontalScrollView(Context context) {
        super(context);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyHorizontalScrollView(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);

        int maxScrollX = getChildAt(0).getMeasuredWidth() - getMeasuredWidth();

        if (getScrollX() == 0) {
            //滑到最左
            mScrolledRightEnd = false;
            mScrolledLeftEnd = true;
        } else if (getScrollX() == maxScrollX) {
            //滑到最右
            mScrolledLeftEnd = false;
            mScrolledRightEnd = true;
        } else {  //滑到中间
            mScrolledLeftEnd = false;
            mScrolledRightEnd = false;
        }
    }

    public boolean isScrolledLeftEnd() {
        return mScrolledLeftEnd;
    }

    public boolean isScrolledRightEnd() {
        return mScrolledRightEnd;
    }
}

如果对你有帮助,那就点个赞,谢谢!

                                                                               THE            END

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值