How I Can Create Vertical Gallery In Android?
Solution 1:
Assume that you want to have a single column of images and want to scroll vertically. Check the following example
<GridViewandroid: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>
Solution 2:
I've managed to create a simple solution using a ListView
, and scrolling it to the closest position when the scroll is stopped. Simply create a ListView
and add this OnScrollListener
:
EDIT: I've updated the code to a better implementation
lv.setOnScrollListener(newOnScrollListener(){
privatebooleanhandleScrollChange=true;
publicvoidonScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
publicvoidonScrollStateChanged(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 middleViewbottomview= view.getChildAt(view.getChildCount()-1);
if (bottomview.getTop() < bottomview.getHeight() / 2) {
centerView = bottomview;
}
else {
centerView = view.getChildAt(0);
}
}
intwantedOffset= (view.getHeight() - centerView.getHeight()) / 2 ;
finalintscrollby= (int) centerView.getY() - wantedOffset;
//we want to prevent the smoothScroll from calling this function again
handleScrollChange = false;
view.post(newRunnable() {
@Overridepublicvoidrun() {
view.smoothScrollBy(scrollby,300);
view.postDelayed(newRunnable() {
@Overridepublicvoidrun() {
handleScrollChange = true;
}
}, 1000);
}
});
}
}
});
Solution 3:
You have to extend the Gallery class and in the Draw procedure rotate the canvas for 90 degrees. Then just a few adoptions like modifying the onTouch event and a few more is required. After this there will be a few problems with the layout (since it still wants to draw in the layout in its parameters). So I put it inside a LinearLayout and fixed the layout size in that. So the final vertical gallery is actually a linear layout which has a gallery put inside it. I have implemented it, and it works quite well. You will only need to rotate everything you put in it for 90 degrees to the other direction. The trade off is really a little thus you can extend every view you want to put inside it and just rotate it to the other direction in the draw procedure.
Post a Comment for "How I Can Create Vertical Gallery In Android?"