Scrolling listview with buttons - android

I want to scroll my listview by pressing the buttons. Am using two buttons up and down and when i press up button the listview should move one row up and when i press down button the listview should move one row down.
My listview is based on this link. I found a good example of scrollview here. Now my question is instead of scrollview i need to use my listview and it should increase/decrease by one row. As am new to android anyone help me in solving this issue. Thanks in advance.

You can try either of the following:
For a direct scroll:
getListView().setSelection(int);
For a smooth scroll:
getListView().smoothScrollToPosition(int);
Sample Code:
public View.OnClickListener onChk = new View.OnClickListener() {
public void onClick(View v) {
int index = list.getFirstVisiblePosition();
getListView().smoothScrollToPosition(index+1); // For increment.
}
});
However you need to handle one case, that is if only half or part of the view at the top is visible.

Have you even checked for IDE suggestions? ;)
ListView lv = (ListView) findViewById(R.id.myListView);
// to scroll to a given position
lv.scrollTo(int x, int y);
// to scroll by a given number of dp
lv.scrollBy(int x, int y);

Scroll with smooth 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.

Related

Scrolling GridView only on Button Click

I want to achieve the following 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.
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..!!
You could try giving corresponding list input to adapter at each click. And then call the notifydatasetchanged to reflect the changes on the View

How to programmatically snap to position on Recycler view with LinearSnapHelper

I have implemented a horizontal recyclerView with LinearSnapHelper, to implement a UI input that selects a particular configuration. Kinda like the old school number picker/selector or spinner. The item in the center is the selected position.
it works fine and all, but here's the problem. On initial start up, I need to programmatically set the position of the recycler view such that the selected item (the index of which was loaded from disk) is position in the center.
.scrollToPosition() wont work becuase it places the selected item in the begining.
now I know I can do all the math and calculate the x coordinate and manually set it, but thats a lot of redundant work because LinearSnapHelper is already doing this, and I feel like there should be a way to just reuse that logic, but with actually initiating a fling.
I need something like LinearSnapHelper.snapToPosition()
More general solution:
First scroll RecyclerView to make target item visible.
Than, take the object of target View and use SnapHelper to determine
distance for the final snap.
Finally scroll to target position.
NOTE: This works only because programmatically you are scrolling at the exact position & covering the missing distance by exact value using scrollBy instead of doing smooth scrolling
Code snippet:
mRecyclerView.scrollToPosition(selectedPosition);
mRecyclerView.post(() -> {
View view = mLayoutManager.findViewByPosition(selectedPosition);
if (view == null) {
Log.e(WingPickerView.class.getSimpleName(), "Cant find target View for initial Snap");
return;
}
int[] snapDistance = mSnapHelper.calculateDistanceToFinalSnap(mLayoutManager, view);
if (snapDistance[0] != 0 || snapDistance[1] != 0) {
mRecyclerView.scrollBy(snapDistance[0], snapDistance[1]);
}
}
});
Try calling smoothScrollToPosition on the RecyclerView object, and passing the position index (int)
mRecyclerView.smoothScrollToPosition(position);
Worked for me with a LinearLayoutManager and LinearSnapHelper. It animates the initial scroll, but at least snaps the item in position.
This is my first post on the stack, hope it helps :)
I have a recyclerView which I have added padding at the left and right with dummy views in the adapter. So that the first "actual" item can be snapped to.
I couldn't get smoothScrollToPosition(0) to work though for the initial snap. I used the following
recycler.scrollBy(snapHelper.calculateDistanceToFinalSnap(binding.recycler.getLayoutManager(), recycler.getChildAt(1))[0], 0);
Isn't the nicest looking way, but seems to work!

Android: scroll to the bottom of the view

I have a ListView containing some custom Views, and at the end of my list, a FooterView containing a Button.
When I click the button, it adds a view at the end of my ListView. But the cursor of the scrollbar stays at its position.
Now, what I want is to auto-scroll to the end of the list (so the user can see the new item).
Do you have any idea on how I can do that?
i think you will find you happyness here
ListView has a method just for this: smoothScrollToPosition
Once you've added the new item, just calculate the length of the list and pass that (remembering zero-indexing) to the above method
Try this one.. it will solve your problem, i tried it and it works great.
listView.post(new Runnable(){
public void run() {
listView.setSelection(listView.getCount() - 1);
}});

ListView scroll to selected item

I have a ListView with an edit text and a button below it. When I click on a listView item the keyboard appears and push up the edit text and the button. I want the list to scroll to the selected item. Any idea? Thanks
You can use ListView's setSelection(int position) method to scroll to a row.
You could use ListView's smoothScrollToPosition(int position) to scroll to a particular location in the list.
For a direct scroll:
getListView().setSelection(11);
For a smooth scroll:
getListView().smoothScrollToPosition(11);
To Scroll to top
getListView().setSelectionAfterHeaderView();
Note
try to call it in post because sometime listview is not yet created while you calling it's method
getListView().postDelayed(new Runnable() {
#Override
public void run() {
lst.setSelection(15);
}
},100L);
You should use transcript mode:
getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
Setup a listener on your the list item being clicked, then use View.getTop() or View.getBottom() when clicked to get it's position within the parent. You can then use ListView.scrollTo(x, y) to scroll to the list item.
Yo can look For
listView.setSelectionFromTop(position, distanceFromHeader);
It will position the Item at position , specified pixels below the top of listview
You can use
smoothScrollToPosition(position)
Just increase the position of item with 1, and you will get the view of item.
getListView().smoothScrollToPosition(position + 1);
Using duration gives a better user experience. Use this, with duration added. Will scroll the item in position smoothly to the top of the listview.
int duration = 500; //miliseconds
int offset = 0; //fromListTop
listview.smoothScrollToPositionFromTop(position,offset,duration);
descrease duration to make scrolling faster

Minimum scroll amount when programmatically selecting ListItem in android ListView

refering to this question: how does scrolling in android listview work?
on change from state 2 to 3 the clicked listitem is increased in its size to show more information. if the clicked item was the last one on screen, the extended listitem is not fully visible because the new content flows out at the bottom of the screen.
my current solution is to call "setSelection(index)", if the last visible listitem was clicked. this results in a state were the selection is brought to the top of the screen. this is annoying because the listitem moves from bottom to top.
is there a way to avoid this? this means, is there something to let the listview know, that is should scroll only the minimum amount of pixels, so that the clickeditem is fully visible?
or have i to programm this functionality on my own?
Add a custom OnItemClickListener to the ListView. Implement OnItemClickListener.onItemClick like this:
public void onItemClick(final AdapterView<?> parent, final View view,
int position, long id) {
View hiddenContent = view.findViewById(R.id.hiddenContent);
hiddenContent.setVisibility(View.VISIBLE);
// At this point the layout hasn't be redone and you don't have reliable
// measurements on the view. It would be nice to do something after the view
// has gone through another layout
view.post(new Runnable() {
public void run() {
Rect r = new Rect();
view.getDrawingRect(r);
parent.requestChildRectangleOnScreen(view, r, false);
}
});
}
Whenever you click on a list item to show the hidden content the ListView control will go ahead and do whatever scrolling is needed (or no scrolling if none is needed) to completely show the row.

Categories

Resources