GridView and ListView inside a ScrollView - android

I have made a gridview and listview inside a LinearLayout. data that display is dynamic. gridview fill the 80% of the screen height.and listview have only 20% of screen height and in it items are scrollable but due to availability of screen height listview display only 2 items at a time..
now i want parent of listview and gridview is scrollable but gridview and listview are not so that list view can show more than 2 items at a time.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<include layout="#layout/app_headerview_imageview_textview" />
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:listSelector="#null"
android:numColumns="3"
android:layout_weight="1" >
</GridView>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#112F51"
android:orientation="horizontal"
android:padding="20dip" >
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/diagnose_button_selector"
android:text="Auto Diagnose"
android:textColor="#ffffff"
android:textSize="#dimen/small_text_size"
android:padding="#dimen/padding_medium"/>
<View
android:layout_width="0dip"
android:layout_height="1dip"
android:layout_weight=".2" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/diagnose_button_selector"
android:text="Diagnose Now"
android:textColor="#ffffff"
android:textSize="#dimen/small_text_size"
android:padding="#dimen/padding_medium" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E6E6E6"
android:orientation="vertical" >
<include layout="#layout/bottom_action_bar" />
</LinearLayout>

A scollview should only have one direct child.
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.
If you want to have more of the listview available on the screen, giving the fact that gridview is also scrollable, you should use the weight property for both children of the LinearLayout so that they take up similar amounts of space on the screen

Related

2 gridview in one scroll view

I have two gridview in one scroll view, however the only gridview to scroll is the pink one
[
and gridview 3 is fixed, but i want the two gridview to scroll together
how it looks like, i don't know if you can actually see... but when i scroll the page, only the pink gridview scrolls, and this vertical gridview doesn't
[
and then it messes the numbers...
my scrollview code:
<!-- Items -->
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fillViewport="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Content itself -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/grid_content"
android:gravity="left"
android:horizontalSpacing="0dp"
android:verticalSpacing="0dp"
android:numColumns="6"
android:clickable="true"
android:background="#color/colorAccent"
>
</GridView>
</LinearLayout>
<!-- Numbers -->
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="right"
android:background="#color/white"
android:gravity="right"
android:padding="0dp"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/grid_numbers"
android:gravity="right"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="1"
android:clickable="true"
android:background="#drawable/border_vertical"
>
</GridView>
</LinearLayout>
</RelativeLayout>
</ScrollView>
I think that gridview already have a scrollbar. Therefore, it "overrides" your scrollview behavior, and then the gridview 3 does not scroll.
You should consider using only one gridView.
Edit :
If for some reasons you don't want to, I'd suggest to overwrite "onTouch" functions in order to prevent grid 2 and 3 to catch the touch event, and therefore, allowing the scrollview to deal with it.
However, this approach may lead to further problems if you need to catch other behaviors (click, move, ...)

Show 3 scrollable ListViews of equal height in Android Layout

I am trying to show three different vertical sections in my Android layout on one screen, each taking up one third of the screen, one on the top, one in the middle, and one on the bottom. Each section has a TextView and a ListView, and the ListViews can scroll so that you can see all items, but the overall page does not move. I have tried putting each TextView and ListView in a LinearLayout and setting the height of each LinearLayout to one third the total height of the screen, but the first ListView just shows all the items in it, taking up most of the screen, and the other sections are pushed down. I also tried using layout_weights, but for some reason it did not work. (EDIT: Setting layout_weight="1" ended up working, I'm not sure what I did wrong the first time) How can I make this work, or is there a better way to go about this?
<LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#FF0000"/>
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#00FF00">
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#0000FF">
</LinearLayout>
This will give you three equally wide columns: to make it rows, change the orientation to vertical and swap the values of layout_height and layout_width for each listview. If you need to add the TextViews, you'll have to make each listview in this code either a RelativeLayout LinearLayout or FrameLayout, using same width/height/weight and arranging the ListView and TextView inside to taste. To do this most efficiently, use a Framelayout and use a margin on the listview to offset it from the TextView. You can place the TextView relative to the ListView inside the FrameLayout by using the layout_gravity in the TextView.
Ie (swapping the first "column"):
<FrameLayout android:orientation="vertical" android:layout_width="0dp"
android:layout_weight="1" android:layout_height="match_parent"
android:background="#FF0000">
<TextView android:text="Column!" android:background="#3Eff0000"
android:layout_height="40dp" android:layout_width="match_parent">
<ListView android:layout_marginTop="48dp" android:layout_height="match_parent"
android:layout_width="match_parent" android:background="#8Aff0000"/>
</FrameLayout>
Use this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ff89ff91">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#1"
android:id="#+id/textView"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff8177">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#2"
android:id="#+id/textView2"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView2"
android:layout_below="#+id/textView2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffe85d">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#3"
android:id="#+id/textView3"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView3"
android:layout_below="#+id/textView3" />
</RelativeLayout>
</LinearLayout>

Four Listviews in a single layout scroll

Myy linearLayout contains four textviews and four listviews. The problem is that only two listviews are visible on the screen, if i want to scroll down the second listview starts scrolling not the whole layout. I've tried putting them in a scrollView but they got all collapsed.
XML code:
<LinearLayout
android:id="#+id/mainpageContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="#+id/TextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listView3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="#+id/TextView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listView4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="#+id/TextView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listView5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Every listview will contain exactly 4 items only.
Any help guys?
You should use only one ListView and MergeAdapter instead of four ListViews.
MergeAdapter accepts a mix of Adapters and Views and presents them as one contiguous whole to whatever ListView it is poured into. This is good for cases where you have multiple data sources, or if you have a handful of ordinary Views to mix in with lists of data, or the like.
MergeAdapter library: https://github.com/commonsguy/cwac-merge

Android: how to make a listview fill its parent without scrolling?

I want my whole activity to scroll at once, but the ListView which is embedded in my ScrollView won't expand. I just see the first row, but I can scroll through the other rows, when scrolling the listView.
What I need is for the listView to expand in height so every items it contains are displayed without having to scroll the listview, only the main scrollView...
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/header_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
...
</LinearLayout>
<TextView
android:id="#+id/comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/gradient_background"
android:text="#string/comments"
android:textColor="#000000"
android:textStyle="bold" >
</TextView>
<ListView
android:id="#+id/commentsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:divider="#drawable/gradient_divider"
android:dividerHeight="1dp" />
</LinearLayout>
</ScrollView>
One of the main purposes of a ListView is to enable recycling of cells while scrolling. If you want all the cells to exist, then you don't need a ListView. Just use a vertically oriented LinearLayout.

display the elments in grid view in only one row

I want to display the grid view elements in only one row that means i want horizontal scrolling in grid view.Each time i want to display only 5 elements only in one row.For this give me some suggestions.Thanks in advance.
below is the my layout code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/ImageViewlarge"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/TextViewImageName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:editable="false">
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:gravity="bottom" android:layout_height="fill_parent">
<ImageView
android:id="#+id/ImageViewLeft"
android:clickable="true"
android:focusable="true"
android:layout_alignParentLeft="true"
android:src="#drawable/leftarrow" android:layout_height="50sp" android:layout_width="30sp" android:layout_gravity="bottom|left">
</ImageView>
<GridView
android:id="#+id/GridView01"
android:scrollbars="horizontal"
android:layout_height="wrap_content"
android:fadingEdge="horizontal"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:numColumns="5" android:layout_gravity="center_horizontal" android:layout_width="260sp">
</GridView>
<ImageView
android:id="#+id/ImageViewRight"
android:clickable="true"
android:focusable="true"
android:src="#drawable/rightarrow" android:layout_width="30sp" android:layout_height="50sp" android:layout_gravity="right"/>
</LinearLayout>
</LinearLayout>
in that above code now i want to remove the that 2 image view and set the horizontal scroll to that grid view elements.For this give me some suggestions,Thanks in advance
I believe the same effect could be achieved by using a LinearLayout with the orientation set to horizontal. The LinearLayout can be embedded in a HorizontalScrollView
The documentation on HorizontalScrollView actually says this:
A HorizontalScrollView is a
FrameLayout, meaning you should place
one child in it containing the entire
contents to scroll; this child may
itself be a layout manager with a
complex hierarchy of objects. A child
that is often used is a LinearLayout
in a horizontal orientation,
presenting a horizontal array of
top-level items that the user can
scroll through.

Categories

Resources