Snap Horizo​​ntalScrollView

时间:2018-03-28 10:52:18

标签: java android xml

我从here获得了以下代码。

此代码snaps Horizo​​ntalScrollView的项目。我尝试了很多在我的布局中实现这个customView但我无法弄清楚如何将我的布局附加到它。

此示例确实以编程方式添加视图并将其称为“功能”。

在XML格式中,我已完成"my package name.view",但我无法弄清楚如何调用setFeatureItems以便可以将我的视图附加到其中。

使用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;
    }
}}

1 个答案:

答案 0 :(得分:1)

在你的recycleview适配器中,你可以参考 HomeFeatureLayout ,你可以在那里调用

homeFeatureLayout.setFeatureItems(items);

修改

        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);
            }

        }