Sachin Aggarwal
Sachin Aggarwal

Reputation: 1135

Scrolling GridView only on Button Click

I want to achieve the following layout -

Required Layout

In this layout, I want to show all the available items to the user. Pressing right arrow will show the next 6 available items. This should happen via horizontal scrolling type motion. Since items can be many, I want to use GridView for this because of its recycling optimisation. Optimisation is necessary because every item is a Linear Layout in itself.

Now, I can't use GridView because I want to show next 6 items using the arrows only. Also, it will be good if this happens in smooth scrolling type motion.

Can I make any tweak to Gridview so that I can use arrows to scroll GridView to show next 6 items or Is there any other ViewGroup available to achieve this along with the recycling optimisation.

Upvotes: 0

Views: 1045

Answers (2)

Ritesh Bhavsar
Ritesh Bhavsar

Reputation: 1355

This example is for listview you can refer for gridview also.

For a SmoothScroll with Scroll duration:

getListView().smoothScrollToPositionFromTop(position,offset,duration);

Parameters position -> Position to scroll to offset ---->Desired distance in pixels of position from the top of the view when scrolling is finished duration-> Number of milliseconds to use for the scroll Note: From API 11.

listview is quite lengthy and also with alphabet scroller. Then I found that the same function can take other parameters as well :)

To position the current selection:

int h1 = mListView.getHeight();
int h2 = v.getHeight();

mListView.smoothScrollToPositionFromTop(position, h1/2 - h2/2, duration); 

Or

You can use RecyclerView : You have to make use of LayoutManager for that. Follow the below steps.

1). First of all, declare LayoutManager in your Activity/Fragment. For example, I have taken LinearLayoutManager

private LinearLayoutManager mLinearLayoutManager;

2). Initialise the LinearLayoutManager and set that to your RecyclerView

mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);

3). On your Button onClick, do this to scroll to the bottom of your RecyclerView.

mLinearLayoutManager.scrollToPosition(yourList.size() - 1); // yourList is the ArrayList that you are passing to your RecyclerView Adapter.

Hope this will help..!!

Upvotes: 2

josh_gom3z
josh_gom3z

Reputation: 294

You could try giving corresponding list input to adapter at each click. And then call the notifydatasetchanged to reflect the changes on the View

Upvotes: 1

Related Questions