ExpandableListView onExpand always scroll expanded group to top - android

i`m looking for way to position expandableListView group on top, when expanded.
I tried onGroupExpand, or in performItemClick of ExpandableListView.
setSelectedPositionFromTop(int, int), partialy works, but if it starts in a time when system starts it's own scroll, than list is overscrolled, and group is out of screen.
Sorry for messy description, but it's hard to say without showing exactly what i need.

Change transcriptmode to disabled.
android:transcriptmode="disabled"
in expandable list layout.

Put this in your adapter:
public void onGroupExpanded(final int groupPosition) {
super.onGroupExpanded(groupPosition);
listView.setSelectedGroup(groupPosition);
}

Try setSelection(groupPosition) in onGroupExpand(int groupPosition).

I tried many solutions but the below code works for me.Hope this code will be helpful for some one.Implements the OnGroupExpandListener with in onGroupExpand
use the below code
public void onGroupExpand(final int groupPosition) {
super.onGroupExpand(groupPosition);
expandableListView.post(new Runnable() {
#Override
public void run() {
expandableListView.setSelection(groupPosition);
if(expandableListView.getChildAt(groupPosition)!=null)
expandableListView.requestChildRectangleOnScreen(expandableListView.getChildAt(groupPosition),
new Rect(0, 0, expandableListView.getChildAt(groupPosition).getRight(), expandableListView.getChildAt(groupPosition).getHeight()), false);
}
});
}

I have solution to this problem. It's not gentle one, but it works quite fine.
Expandable listView messures is it should move by child number. It seems, that system thinks that child is same height as parent(group), so if you put different size child it will cause problems with auto scrolling list.
My solution was to trick list to think that it has more children.
If my child is twice as big as group, i just return information that group has 2 children. Now, since it is data constructed list, i make check on my data, is there a data for 2 childs, or only 1, if i don't have data for second child, i know that i can put 0 height view as second child.
It worked fine when i had to make List with small group, and than with 1 big child.
Since there was no answer to my question, i post this one, it may help someone.

Related

How to have a GridLayoutManager with header, without changing its adapter?

Background
I have an app that shows a list of items in a grid-like way, yet not exactly...
The problem
Some of the items have a different height, so the ones near them should gain the same height as they have. This one works on GridLayoutManager.
Some items (actually only the first one in my case) need to span the entire row (which is why I used StaggeredGridLayoutManager ).
Using the normal GridLayoutManager, the first requirement worked fine: each row could have a different height. But because of #2, it actually ruined #1.
The question
Is it possible to use StaggeredGridLayoutManager so that when items have different height, it won't make them move in the Y coordinate?
I was thinking: maybe I could use NestedScrollView and GridLayoutManager (because only the first item is spanned), but still, I would like to know if it's possible in the rest of the cases (and also this solution).
OK, so I've found a possible solution by still using GridLayoutManager:
use getItemViewType and return a specific value for spanned items, and a different value for normal items .
Create the needed view in onCreateViewHolder, according to its type, and cast to the needed viewHolder class in onBindViewHolder according to the type.
use setSpanSizeLookup, and inside getSpanSize, return the spanned cells in case the type is for such items
Example:
#Override
public int getItemViewType(final int position)
{
return position==0?VIEW_TYPE_HEADER:VIEW_TYPE_NORMAL;
}
public ViewHolder onCreateViewHolder(final ViewGroup parent,final int viewType)
{
if(viewType==VIEW_TYPE_HEADER) ... //create header ViewHolder
else ... // create normal item
}
...
_layoutManager=new GridLayoutManager(...,3,LinearLayoutManager.VERTICAL,false);
_layoutManager.setSpanSizeLookup(new SpanSizeLookup()
{
#Override
public int getSpanSize(final int position)
{
return _adapter.getItemViewType(position)==VIEW_TYPE_HEADER?_layoutManager.getSpanCount():1;
}
});
Still, I would like to know how to use the NestedScrollView solution
One issue that I have noticed, is that if I try to set the visibility of the header to GONE, it still takes space.

Dynamically Setting RelativeLayout.LayoutParams.addRule

I have this issue where I have a relative layout that has two child relative layouts (leftpanel and rightpanel). They're inside a custom layout for listview items and each item is updated from a json response from the server. So the size depends on what the server provides.
Issue: I want to have each panel's height to match each other, but it seems that setting layout_height to match_parent doesn't work (actually, if this can be resolved, then no more problems).
What I did: I programmatically set the align top and bottom of each panel to each other -- if the other's bigger, adjust the other one and vice versa. So what I did was to have a view (rightpanel) to listen to rightPanel.getViewTreeObserver().addOnScrollChangedListener(), and call the method below everytime there's a scroll change:
private void updateLayoutAlignmentParams(ViewHolder viewHolder) {
RelativeLayout.LayoutParams leftPanelLayoutParams = (RelativeLayout.LayoutParams)viewHolder.leftPanel.getLayoutParams();
RelativeLayout.LayoutParams rightPanelLayoutParams = (RelativeLayout.LayoutParams)viewHolder.rightPanel.getLayoutParams();
int leftPanelHeight = viewHolder.leftPanel.getHeight();
int rightPanelHeight = viewHolder.rightPanel.getHeight();
if(leftPanelHeight > rightPanelHeight) {
rightPanelLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, 0);
rightPanelLayoutParams.addRule(RelativeLayout.ALIGN_TOP, 0);
leftPanelLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, viewHolder.rightPanel.getId());
leftPanelLayoutParams.addRule(RelativeLayout.ALIGN_TOP, viewHolder.rightPanel.getId());
} else {
leftPanelLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, 0);
leftPanelLayoutParams.addRule(RelativeLayout.ALIGN_TOP, 0);
rightPanelLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, viewHolder.leftPanel.getId());
rightPanelLayoutParams.addRule(RelativeLayout.ALIGN_TOP, viewHolder.leftPanel.getId());
}
}
What happens: not all the views get updated while scrolling; so I get a lop-sided listview item where one is bigger than the other vertically but some do adjust well. Odd thing is, when the item gets out of view, it's lop-sided, then gets corrected consistently.
Note: I also tried
addOnDrawListener() - every item is updated but I get an ArrayList out of bounds index but doesn't point to any line in my code. Wouldn't be the best solution anyway as I need to support devices with API < 16.
setOnGlobalLayoutListener() - Nothing happens.
Please let me know if you know why or have a better solution.
Finally [kindof] fixed it! Replaced the whole method with the code below:
private void updateLayoutAlignmentParams(ViewHolder viewHolder) {
viewHolder.rightPanel.setMinimumHeight(viewHolder.leftPanel.getHeight());
viewHolder.leftPanel.setMinimumHeight(viewHolder.rightPanel.getHeight());
}
Although, I was able to achieve having the left and right panel aligned with each other using the code above. I'm now having issues where the previous view's height and width are retained when I switch views. :(
Edit:
Okay, I ended up using LinearLayout to wrap the whole listview item. Not really sure why RelativeLayout isn't complying with match_parent, though.

How to Autoscroll to bottom on listview?

I have application with ListView. This ListView has a Progressbar.When I am clicking on this Progressbar, then it is showing a hided Layout(This layout contain textview with lyrics). So as I am clicking on the last Progressbar then it is showing the hided Layout, but I need to scroll manually. I want that scrolling must be automatically.
Please help me.
Try something like this:
private void scroll2bottom() {
yourListView.post(new Runnable() {
#Override
public void run() {
yourListView.setSelection(yourListAdapter.getCount() - 1);
}
});
}
You might find the smoothScrollByOffset() or smoothScrollBy() methods of ListView most useful. In particular smoothScrollBy() takes both a distance to scroll and a duration over which the scroll animation should take place. With this method you can control precisely how far to scroll the list.
in your layout of your listview just use the transcriptMode
a listview has builtin auto scroll
android:transcriptMode="normal" witll scroll when its at the bottom but not when you have scrolled away
and the setting alwaysScroll will always scroll to the bottom
and then you can set the android:stackFromBottom="true" to make it grow from the bottom
Try in this way:
lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lv.setStackFromBottom(true);
To scroll a listView programmatically you can use its smoothScrollByOffset(int offset) or smoothScrollToPosition(int position) methods, you can find here the documentation about the ListView widget and how to use those methods.

How to scroll ListView to the bottom?

I though this would be a simple task, but apparently there is no way to scroll listview to the bottom. All solutions that I found are using variations of setSelection(lastItem) method which only sets selection to last item, but does not scrolls to the bottom of it.
In my case I have a empty listview (with a long empty view header set) and I want to scroll to bottom of it.
So, is there a way to do it?
Edit:
So for those who are interested the working solution is:
getListView().setSelectionFromTop(0, -mHeader.getHeight());
and
getListView().scrollTo(mOffset)
This also works, with right offset (calculated based on current scroll position), but might give you some unexpected results.
If you want the list view to be scrolled always at the bottom even when you are updating the list view dynamically then you can add these attributes in list view itself.
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
use the following
lv.setSelection(adapter.getCount() - 1);
If you would like to have a scroll animation programmatically you could easily scroll to the bottom of the list using:
listView.smoothScrollToPosition(adapter.getCount());
scrolling to the top most of the list can be achieved using:
listView.smoothScrollToPosition(0);
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);
}});
private void scrollMyListViewToBottom() {
myListView.post(new Runnable() {
#Override
public void run() {
// Select the last row so it will scroll into view...
myListView.setSelection(myListAdapter.getCount() - 1);
}
});
}
Its working for me
Use following and try.
listview.setSelection(adapter.getCount()-1);
You might want to try myListView.smoothScrollToPosition(myListView.getCount() - 1). This way, you're not forced into selecting the last row. Plus, you get some smooth, beautiful scrolling! (:
I Had the same problem, This works best for me
listView.setSelection(listView.getAdapter().getCount()-1);
if there is no adapter set to your ListView then it has no children at all, empty view is not a child of your ListView
Try using list.scrollTo(0, list.getHeight());

Scrolling issues with a ListView inside a ScrollView

Here's the scenario conceptually (excluding linearlayouts)
ScrollView
Button
Checkboxes
Spinner
ListView (full-size, non-scrolling)
AdMob advert
i.e. a scrolling pane, which has a filtering UI at the top, followed by results, but the advert must always remain visible, and when scrolling down the filter UI must scroll away, leaving maximum space for results.
I'm aware there are issues with a ListView inside a ScrollView, though for me it is working well in many ways (I'm fixing the length of the ListView to stop it collapsing). So the screen scrolls nicely, the ad stays put at the bottom, and it looks good.
But the problem I'm seeing is, inexplicably, when the activity opens, the ScrollView is scrolled down a bit, so the ListView is at the top of the screen. I assume this is a default behaviour, so I set about trying to force the scroll position of the ScrollView to the top, but I've tried various methods, and see no effect:
scrollview.scrollTo(0, 1000/-1000);
scrollview.smoothScrollBy(0, 1000/-1000);
scrollview.fullScroll(View.FOCUS_UP);
Is there any way to force the ScrollView to start with the scroll position at the top?
If not, how can I have an ad that doesn't scroll off the bottom, but a filter UI that always scrolls off the top? Using ListView seems overkill as I don't need scrolling but it does provide many benefits so would be nice to avoid starting from scratch and rendering everything myself.
Use the following method and enjoy!
private void setListViewScrollable(final ListView list) {
list.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
listViewTouchAction = event.getAction();
if (listViewTouchAction == MotionEvent.ACTION_MOVE)
{
list.scrollBy(0, 1);
}
return false;
}
});
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE)
{
list.scrollBy(0, -1);
}
}
});
}
listViewTouchAction is a global integer value.
If you can replace the line
list.scrollBy(0, 1);
with something else please share it with us.
Why are you using a listview if you're not scrolling? Why can't you just use a linearlayout or something more fit to this situation? You mention a filter, you could very easily roll your own filter especially since apparently you just have a few items in your listview.
Use something other that a ListView, you can dinamically generate linearLayouts to show the data you want. You should never use a listview inside a scrollView, it doesnt work for a simple reason, when you scroll, what should scroll, the listview or the scroll view. A couple of people from google have stated not to do this.
I've experienced the issue of a ScrollView starting off scrolled down slightly, the solution was to post a runnable which called the animateTo(0,0) method to get the list to scroll to the top. I found this only worked using anitmatTo(0,0) scrollTo(0,0) didn't seem to work.
Something along the lines of:
mListView.post(new Runnable(){ public void run() { mListView.animateScrollTo(0,0) });
Now as everyone has already stated you shouldn't do the whole ListView inside a ScrollView, but this may be a fix for the problem you had.
I have:
ScrollView
TextView
Button
TextView
ListView
and this work good for me:
scrollView.smoothScrollBy(0, 0);
without this view start from position of listview, after that it start from top
The solution for this issue is to make a request focus to an object in the top of the ScrollView. For example you can use a table layout wrapping the button and request focus to the table layout (If you focus the button it will change its color).
// onCreate method
TableLayout tablelayout = (TableLayout) findViewById(R.id.tablelayout);
tablelayout.setFocusableInTouchMode(true);
tablelayout.requestFocus();

Categories

Resources