HorizontalScrollView的使用

                                          HorizontalScrollView的使用

应用场景:

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

使用方法:

1)XML里的定义

    

        

            

                
            
        

        

需要注意的是要用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

你可能感兴趣的:(android)