How to create scrollable page of carousels in Android?

后端 未结 5 950
再見小時候
再見小時候 2020-12-12 15:36

I am attempting to build a UI for my Android app which contains a vertically scrollable page of horizontally scrollable carousels (something like what the Netflix app does).

5条回答
  •  伪装坚强ぢ
    2020-12-12 16:12

    You can use ListView with a custom OnTouchListener (for snapping items) for the vertical scrolling and TwoWayGridView again with a custom OnTouchListener (for snapping items)

    enter image description here

    main.xml

    
    

    list_item_hgrid.xml

    
    

    And the Activity code will be something like the following

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
    
        ListView containerList = (ListView) findViewById(R.id.containerList);
        containerList.setAdapter(new DummyGridsAdapter(this));
        containerList.setOnTouchListener(mContainerListOnTouchListener);
    }
    
    private View.OnTouchListener mContainerListOnTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    View itemView = ((ListView) view).getChildAt(0);
                    int top = itemView.getTop();
                    if (Math.abs(top) >= itemView.getHeight() / 2) {
                        top = itemView.getHeight() - Math.abs(top);
                    }
    
                    ((ListView) view).smoothScrollBy(top, 400);
            }
    
            return false;
        }
    };
    

    And here are the test adapters

    private static class DummyGridsAdapter extends BaseAdapter {
    
        private Context mContext;
    
        private TwoWayGridView[] mChildGrid;
    
        public DummyGridsAdapter(Context context) {
            mContext = context;
    
            mChildGrid = new TwoWayGridView[getCount()];
            for (int i = 0; i < mChildGrid.length; i++) {
                mChildGrid[i] = (TwoWayGridView) LayoutInflater.from(context).
                        inflate(R.layout.list_item_hgrid, null);
                mChildGrid[i].setAdapter(new DummyImageAdapter(context));
                mChildGrid[i].setOnTouchListener(mChildGridOnTouchListener);
            }
        }
    
        @Override
        public int getCount() {
            return 8;
        }
    
        @Override
        public Object getItem(int position) {
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return mChildGrid[position];
        }
    
        private View.OnTouchListener mChildGridOnTouchListener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        View itemView = ((TwoWayGridView) view).getChildAt(0);
                        int left = itemView.getLeft();
                        if (Math.abs(left) >= itemView.getWidth() / 2) {
                            left = itemView.getWidth() - Math.abs(left);
                        }
    
                        ((TwoWayGridView) view).smoothScrollBy(left, 400);
                }
    
                return false;
            }
        };
    
    }
    
    private static class DummyImageAdapter extends BaseAdapter {
    
        private Context mContext;
    
        private final int mDummyViewWidthHeight;
    
        public DummyImageAdapter(Context context) {
            mContext = context;
    
            mDummyViewWidthHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 128,
                    context.getResources().getDisplayMetrics());
        }
    
        @Override
        public int getCount() {
            return 16;
        }
    
        @Override
        public Object getItem(int position) {
            int component = (getCount() - position - 1) * 255 / getCount();
            return Color.argb(255, 255, component, component);
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setBackgroundColor((Integer) getItem(position));
            imageView.setLayoutParams(new TwoWayGridView.LayoutParams(mDummyViewWidthHeight, mDummyViewWidthHeight));
            return imageView;
        }
    
    }
    

提交回复
热议问题