简体   繁体   English

Snap Horizo​​ntalScrollView

[英]Snap HorizontalScrollView

I got the code below from here .我从这里得到了下面的代码。

This code snaps the items of HorizontalScrollView.此代码捕捉Horizo​​ntalScrollView 的项目。 I tried a lot to implement this customView inside my layout but I am not able to figure out to how to attach my layout to it.我尝试了很多来在我的布局中实现这个 customView,但我无法弄清楚如何将我的布局附加到它。

This example does add the views programmatically and calls them Features.此示例确实以编程方式添加视图并将它们称为功能。

In XML I have done "my package name.view" but I cannot figure out how to call setFeatureItems so that my Views can be attached to it.在 XML 中,我已经完成了"my package name.view"但我无法弄清楚如何调用setFeatureItems以便我的视图可以附加到它。

The snapping feature can be applied on RecyclerView easily by using SnapHelper but I haven't found anything for HorizontalScrollView.使用 SnapHelper 可以轻松地在 RecyclerView 上应用捕捉功能,但我没有找到 Horizo​​ntalScrollView 的任何内容。

public class HomeFeatureLayout extends HorizontalScrollView {
    private static final int SWIPE_MIN_DISTANCE = 5;
    private static final int SWIPE_THRESHOLD_VELOCITY = 300;

    private ArrayList mItems = null;
    private GestureDetector mGestureDetector;
    private int mActiveFeature = 0;

    public HomeFeatureLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

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

    public void setFeatureItems(ArrayList items){
        LinearLayout internalWrapper = new LinearLayout(getContext());
        internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
        addView(internalWrapper);
        this.mItems = items;
        for(int i = 0; i< items.size();i++){
            LinearLayout featureLayout = (LinearLayout) View.inflate(this.getContext(),R.layout.homefeature,null);
            //...
          //Create the view for each screen in the scroll view
            //...
            internalWrapper.addView(featureLayout);
        }
        setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //If the user swipes
                if (mGestureDetector.onTouchEvent(event)) {
                    return true;
                }
                else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL ){
                    int scrollX = getScrollX();
                    int featureWidth = v.getMeasuredWidth();
                    mActiveFeature = ((scrollX + (featureWidth/2))/featureWidth);
                    int scrollTo = mActiveFeature*featureWidth;
                    smoothScrollTo(scrollTo, 0);
                    return true;
                }
                else{
                    return false;
                }
            }
        });
        mGestureDetector = new GestureDetector(new MyGestureDetector());
    }
        class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                //right to left
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    int featureWidth = getMeasuredWidth();
                    mActiveFeature = (mActiveFeature < (mItems.size() - 1))? mActiveFeature + 1:mItems.size() -1;
                    smoothScrollTo(mActiveFeature*featureWidth, 0);
                    return true;
                }
                //left to right
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    int featureWidth = getMeasuredWidth();
                    mActiveFeature = (mActiveFeature > 0)? mActiveFeature - 1:0;
                    smoothScrollTo(mActiveFeature*featureWidth, 0);
                    return true;
                }
            } catch (Exception e) {
                    Log.e("Fling", "There was an error processing the Fling event:" + e.getMessage());
            }
            return false;
        }
    }
}

in your recycleview adapter you will have reference to HomeFeatureLayout there you can call在您的 recycleview 适配器中,您将参考HomeFeatureLayout在那里您可以调用

homeFeatureLayout.setFeatureItems(items);

EDIT编辑

        public void setFeatureItems(ArrayList items){

            for(int i = 0; i< items.size();i++){

    // here you need to provide layout of individual items in your horizontal scrollview 
       LinearLayout featureLayout = (LinearLayout) View.inflate(this.getContext(),R.layout.anyLayout,null);
                //...
              //display information here
                //...
    TextView title = (TextView)featureLayout.findviewById(R.id.title);
    title.setText("view title "+item.get(i).getTitle());
    ImageView image = (ImageView)featureLayout.findviewById(R.id.icon);
    image.setResourceId(R.drawable.icon);

internalWrapper.addView(featureLayout);
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM