首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快照HorizontalScrollView

快照HorizontalScrollView
EN

Stack Overflow用户
提问于 2018-03-28 18:52:18
回答 1查看 248关注 0票数 0

我从here得到了下面的代码。

这段代码snaps了HorizontalScrollView的各项。我尝试了很多在我的布局中实现这个customView,但我不能弄清楚如何将我的布局附加到它。

此示例确实以编程方式添加了视图,并将其命名为Feature。

在XML语言中,我做过"my package name.view",但是我不知道如何调用setFeatureItems,以便将我的视图附加到它上。

通过使用SnapHelper可以很容易地在RecyclerView上应用捕捉功能,但我还没有找到适用于HorizontalScrollView的任何功能。

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-03-28 19:22:47

在recycleview适配器中,您将引用HomeFeatureLayout,您可以在那里调用

代码语言:javascript
复制
homeFeatureLayout.setFeatureItems(items);

编辑

代码语言:javascript
复制
        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);
            }

        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49532588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档