How to create a vertical gallery in Android?

I want to create a widget that behaves like a gallery widget, but scrolls vertically, not horizontally. That is, the images in the gallery should be vertically placed on the screen and can be scrolled vertically. Does anyone help me?

+2
source share
3 answers

Suppose you want to have one column of images and want to scroll vertically. Check out the following example

<GridView android:id="@+id/gridView1" android:layout_width="70dp" android:layout_height="fill_parent" android:verticalSpacing="2dp" android:numColumns="1" android:stretchMode="columnWidth" android:layout_marginLeft="1dp" android:layout_alignParentTop="true" android:scrollingCache="true" android:scrollbars="vertical" android:fadeScrollbars="false" android:scrollbarAlwaysDrawVerticalTrack="true"> </GridView> 
+2
source

I managed to create a simple solution using ListView and scroll it to the nearest position when the scroll is stopped. Just create a ListView and add this OnScrollListener :

EDIT: I updated the code to a better implementation

 lv.setOnScrollListener(new OnScrollListener(){ private boolean handleScrollChange = true; public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } public void onScrollStateChanged(final AbsListView view, int scrollState) { if (!handleScrollChange) return; if(scrollState == OnScrollListener.SCROLL_STATE_IDLE){ View centerView; if (view.getLastVisiblePosition() - view.getFirstVisiblePosition() > 1) { //if shows more than 2 items, display the middle one centerView = view.getChildAt( Math.round((view.getChildCount())/2)); } else { //shows 2 items, check which one is closer to the middle View bottomview = view.getChildAt(view.getChildCount()-1); if (bottomview.getTop() < bottomview.getHeight() / 2) { centerView = bottomview; } else { centerView = view.getChildAt(0); } } int wantedOffset = (view.getHeight() - centerView.getHeight()) / 2 ; final int scrollby = (int) centerView.getY() - wantedOffset; //we want to prevent the smoothScroll from calling this function again handleScrollChange = false; view.post(new Runnable() { @Override public void run() { view.smoothScrollBy(scrollby,300); view.postDelayed(new Runnable() { @Override public void run() { handleScrollChange = true; } }, 1000); } }); } } }); 
+1
source

You must expand the Gallery class, and in the drawing procedure, rotate the canvas 90 degrees. Then a few adoptions are required, such as changing the onTouch event and a few more. After that, there will be several problems with the layout (since it still wants to draw the layout in its parameters). So I put it in LinearLayout and fixed the layout size in this.
Thus, the final vertical gallery is actually the linear layout in which the gallery is placed. I implemented it and it works quite well. You will only need to turn everything that you put into it 90 degrees in a different direction. The compromise is really a bit, so you can expand each view that you want to insert into it, and simply turn it in a different direction in the drawing procedure.

0
source

Source: https://habr.com/ru/post/1345504/


All Articles