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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章