I have created a layout containing many linear layouts, and I have, in one of the linear layouts, 3 ListViews set side by side. I want to make each one of them scrollable, but singularily. For example, I want to be able to scroll one of them without anything else being affected.
Right now, if I set "android:nestedScrollingEnabled="true"" for any one of the lists, the entire screen scrolls. I want the scrolling of the list to be confined to the layout the list is in (so I can scroll the list and the rest of the screen stays in place).
How can I do that? So far, it's either no scrolling at all, or scrolling the entire screen. I just want the list to scroll.
Thanks!
PS: Here's how the relevant part of the XML looks like (nothing special about it):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginBottom="#dimen/list_vertical_margin">
<ListView
android:id="#+id/realtime_vehicle_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ListView
android:id="#+id/realtime_errors_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ListView
android:id="#+id/realtime_depot_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
One of your LinearLayouts is in a ScrollView, I guess. So remove the ScrollView.
Try to add the next following code in the activity that contains those Listviews:
ListView mylistview = (ListView) findViewById(R.id.mylistview);
mylistview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
I've figured it out. Apparently, the layout "continues" beyond the screen. I only put a few more elements to test if it scrolls, but in reality, the reason why only the 3rd list was scrolling and the other two weren't is because the 3rd list had more elements, and it went beyond its limits. That's why it was scrolling.
I was equating the "going out of the screen" with the idea that the list should scroll, but it's just a design problem - the operating system didn't think that the list was going off the screen.
As soon as I put more elements into the other two lists, they are now all scrolling.
Related
This might be a very beginner question, but I'm yet unable to find myself around the android jungle.
I've already got a RecyclerView working to show a list of items (with data binding and Room database and DiffUtil.ItemCallback and all).
I'd like to put 2 links after the list: "missing something?" and "add new entry" that will lead to other fragments.
What I have:
When I put 2 buttons (I don't know yet how to put links, but this is not the point of this question) after the RecyclerView, all in a LinearLayout, they stay fixed near the screen bottom. I mean, the RecyclerView is scrollable by itself, scrolling "beneath" the two buttons, the entire LinearLayout expanding to fill the screen (match_parent).
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Missing something?"
android:onClick="#{...}" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add new item"
android:onClick="#{...}" />
</LinearLayout>
What I want
I'd like the 2 buttons to scroll along with the list, so that they are always positioned after the last item (think as if they were items themselves, albeit an heterogeneous list with different types/RecyclerView.ViewHolder).
For a big enough list the buttons will be initially off screen; to be scrolled in if the user happen to scroll to the bottom of the list.
What I tried
I tried with ScrollView around the LinearLayout, and it works, but everywhere everybody say that one should never put a RecyclerView inside a ScrollView (maybe because it is scrollable itself).
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/routines_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<!-- buttons -->
</LinearLayout>
</ScrollView>
Being really a beginner in android programming, I'd like to know how usually this kind of layout should be done. Only main directions will be enough for me.
NB. I don't know if I really need a RecyclerView because I don't expect this list to be lengthy. Maybe usually something around 4 to 8 items, possibly 10. But I really don't expect it to be much bigger than that. For many users the two links will even be visible all the time (i.e. no scroll at all).
RecyclerView is always the most efficient to show a list especially if you are getting the data from a database or an API. Don't put your recyclerview in a scrollview. You can add two items to the bottom of the list as your links and program your recyclerview to exhibit different properties for last two items. That is the best way I can think of. Good Luck!
Also, Recyclerview is very difficult to work with when you are working with complex data. With small lists such as in your case, it can seem inconvenient to create a whole adapter class and do everything you are supposed to do. When you have grasped the concepts on xml android and have plenty experience with that. You can move to jetpack compose and lazy column will make your life easy.
I have a ListView inside ScrollView, the ListView works fine (it scrolls) but the ScrollView is not scrolling
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I want the full page to scroll (The textview should also scroll)
TextView
ListView
item1
item2
.
.
The full page must scroll along with the textview!
This will never work because your listView is effectively a scrollView, so you have a scrollView within a scrollView. Is the textView of fixed size or can it be a large amount of text? I would consider some redesign here as this is not a very good way to design a UI.
If the textView is big which means you only see a small bit of the list then you should be able to scroll by touching the textView only, but i would make the scrollView the Parent and remove the first LinearLayout, it is not needed
Instead of ListView switch to RecyclerView and inside your MainActivity.java in onCreate() do this recyclerView.setNestedScrollingEnabled(false);
You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.
The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.
I have a vertical layout (it fills the screen) that when simplified, looks like this:
<LinearLayout android:orientation="vertical" >
<ImageView />
<EditText />
<ListView />
</LinearLayout>
The user can scroll this up and down, revealing more or less of the ImageView. But if I use a GestureDetector to interpret scroll, I don't know how to delegate some of them to the children. Especially so that if the user touches the ListView, it should first slide up to hide the ImageView completely, and only after that the ListView should scroll to the end.
My app supports 4.2 and higher.
The solution is based on StickyListHeaders. I put ImageView as header view of the list, and the EditText as sticky header. Now the ListView handles the touch events for me.
I am not able to scroll in a scrollview which contains a listview and is filled dynamically as I get data from the webservice.
I am able to do scrolling in emulator through mouse wheel, but in avtual device I can not scroll the list.
The attributes of scrollview are
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:layout_weight="0.6"
android:fillViewport="true"
android:orientation="vertical"
android:padding="6.0dip"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarFadeDuration="5000"
android:scrollbarSize="20dp"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="2dp" >
<ListView
android:id="#+id/listbox_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="599.84"
android:minHeight="250dp" >
</ListView>
</LinearLayout>
</ScrollView>
Please help me soon
by just looking at the layout_width and layout_height of your elements, it's clear that your scrollview will not scroll. unless you have a fixed height listview, never put a listview inside a scrollview (or in this case, a listview inside a layout that sits inside a scrollview).
I don't have any links to back this up right now, but it's not possible, and a well-known 'problem'. If you google a bit, or search here on SO, you'll find a number of topics covering this.
The problem arises in most cases when you have a scrolling view inside another scrolling view in the same direction. Consider the following example:
You have Two lists inside of a ScrollView.
Both lists are exactly one screen tall.
How do you scroll down to the second list?
When scrolling, how will your layout know if you are scrolling the list or the container?
This is basically the question that is the cause, and the only official solution is that it is as it should be, and there won't be a fix. Usually it is enough to have either a ListView or a ScrollView, but I have faced cases when you must have a listview in a scrollview (in my case a client wanted an iPhone-like datespinner in a scrolling page).
I solved it by using a FrameLayout, containing a custom ScrollView, and a ListView on top of that. Then in the code for the custom ScrollView, I added a line in the onScroll method that updated the top margin of the ListView, to psuh it upwards or downwards as the user scrolled. Surprisingly it worked.
NOTE: remember that:
The ListView handles its own scroll. If all you need is a scrolling
list, you do not need a ScrollView.
If you need a layout with a list and space for buttons or other
views, consider creating your layout so that the list only covers
enough space for you to fit your other views below/above without
scrolling.
Add following in your linear layout
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
I would like to use horizontall scrolling items in a vertically scrolling Listview.
My naive take on this was to put the contents of the listview items inside a scrollView. The items are wider horizontally than the scrollview, but not higher than the scrollview. Since the listview is a normal vertically scrolling listview, I figured that dragging vertically would scroll in the list, while dragging horizontally would scroll in the items.
However that didn't work. The list scrolls fine vertically and shows the items correctly, but scrolling horizontally does not work (nothing happens). Unfortunately I am really not sure where to go from here.
Note that the items should scroll horizontally independently of the other items, i.e the whole list should not scroll sideways when dragging sideways.
As a reference, I would like the list to behave similar to what it does in the app 'Pulse', in case you have seen it.
Make ordinary ListView with any adapter you like but design the item Layout something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:id="#+id/hor_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/lin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6.0dip"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:focusable="false" />
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:gravity="center_vertical"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="true"
android:layout_toRightOf="#id/icon"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
You'll have Vertically Scrollable ListView with Horizontally Scrollable items. And the items are scrolled independantly from other items.
You need to use a HorizontalScrollView. Droidstack is an open source app (I wrote) that does just this (in the questions lists you can scroll the tags on each question), if you want a concrete example.
if you follow the good practices of android development, you should never put a ScrollView inside a ListView, is unrecomended bu Romain Guy and other people from android development, to read the arguments read here: Android ScrollView layout problem
from the android docs:
"You should never use a HorizontalScrollView with a ListView, since ListView takes care of its own scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by HorizontalScrollView."
EDIT: it seems that the warning posted above is an error from the android documentation, talking with some colleagues they told me its possible. The issue in the documentation is here, http://code.google.com/p/android/issues/detail?id=2781.
my appologies
You can use "ViewPager" each element in the list can be a ViewPager