Why position allways scroll to gridview's position inside my ScrollView? - android

I use a vertical ScrollView to carry ViewPager and Gridview. ViewPager's position is above the Gridview,and Gridview is use "expand all" to keep it behaviors well inside scrollview.
But. After I use adapter to fill content to these two view. The position is always scroll to Gridview's position at first.User need to scroll it up to see Viewpager themself.
I want its position can at the first viewpager, not the gridview.
Could you give me some suggestions.
Thanks.
The following is my layout file.
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<LinearLayout
android:id="#+id/linearlayout_dynamicRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/viewpager_fl"
android:layout_width="fill_parent"
android:layout_height="160dp" >
<myViewPager
android:id="#+id/vp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#B0000000"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/ll_dot_dynamic"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="0dp" />
</LinearLayout>
<TextView
android:id="#+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:ellipsize="end"
android:gravity="center_vertical|center_horizontal"
android:maxLines="1"
android:textColor="#FF7700"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="GridView_Header1"
android:textColor="#ff7700"
android:textSize="20dp"
android:visibility="gone" />
<GridView
android:id="#+id/gv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</GridView>
</LinearLayout>
</LinearLayout>
</ScrollView>

Further reading has revealed that there are issues with scrolling components inside scrolling components.
One solution is to 'disable' the vertical scrolling of the ScrollView on the area of the contained scrollable component, in my case a ViewPager.
The code for this solution is detailed here(HorizontalScrollView within ScrollView Touch Handling) (and its simply brilliant!)

Related

Scrolling two listview inside one LinearLayout

I am trying to scroll the listview inside one Linear Layout but it is not working as i want. I am attaching one image for the more explanation.
This is my Layout
<android.support.v7.widget.CardView
android:id="#+id/card"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:clickable="false"
app:cardBackgroundColor="#android:color/white"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical">
<Button
android:id="#+id/findSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:background="#drawable/flat_selector_green"
android:text="Next"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
Image explanation
I know it s not the good approach to use the listview but i have to do this with this way.
Just remove the ScrollView from your xml code, Because ListView have their own Scroll doesn't require ScrollView.Then code will work as you want. Second List will be shown after showing every item of first ListView.
According to android docmentation
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.
First you user card view as top parent it seem you will use it as child of list view as I guess , if my guessing is correct this not wise to use two list view as child.
Second In any case you want use two listview in same view , its recommended to separate between them with view (TextView , ... etc).
the solution
1- remove scroll as it useless.
2- add weight to your list view as wrap content in your case mean whole view.
3- recommend add view to separate between list.
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:clickable="false"
app:cardBackgroundColor="#android:color/white"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wholeview"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<!-- recommended to add view here -->
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/findSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="Next"
android:textColor="#android:color/white"
/>
</LinearLayout>
</RelativeLayout>

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>

Scroll PullToRefreshScrollview automatically in android

I have a Listview, FrameLayout inside PullToRefreshScrollview. And I'm loading a fragment into framelayout. Data is loading into listview using lazy adapter. I'm able to see the fragment/framelayout while listview is loading. But Scrollview is automatically scrolling to the postion of listview after data is loaded into listview and framelayout is not visible. How can I scroll the scrollview to top? Please help me. Thanks in advance
<?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:background="#color/app_bg"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/ptrScrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:paddingBottom="5dp"
ptr:ptrMode="pullUpFromBottom" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<com.xxxx.yyyy.CustomListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/app_bg"
android:focusable="false" />
<ProgressBar
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
Here is the aproach I took
Lets say you have a reference to the PullToRefreshScrollView like so
PullToRefreshScrollView myRefreshScrollView = (PullToRefreshScrollView)findViewById(R.id. ptrScrollView);
To get the ACTUAL ScrollView object you need to call getRefreshableView() like so
ScrollView myScrollView = myRefreshScrollView.getRefreshableView();
and then scroll this object
myScrollView.scrollTo(0,0);
or
myScrollView.smoothScrollTo(0,0)
There are 2 teps to make it works:
you should set android:smoothScrollbar="true" in layout xml :
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="#+id/news_list_listView_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EEE"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="5dp"
android:layout_marginTop="2dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:smoothScrollbar="true"/>
and Then,
myRefreshScrollView.getRefreshableView().smoothScrollToPosition(lastScollCount);

ScrollView with LinearLayout inside does not work

I have a scrollView with two inside LinearLayout but it does not work, the listView does not show all items but only a few. How can I do?
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/welcomeView"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/image_User_welcome"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="70"
android:src="#drawable/user" />
<TextView
android:id="#+id/private_area_welcome"
style="#style/CoopLabel"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="30"
android:text="#string/private_area_welcome" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/welcomeView">
<TextView
android:id="#+id/private_area_lists"
style="#style/CoopLabel"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/linearLayout1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:text="#string/private_area_lists" />
<ListView
android:id="#+id/privareaList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:textColor="#000000" >
</ListView>
<ImageButton
android:id="#+id/logoutBtn"
android:layout_width="160dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="0dp"
android:scaleType="centerCrop"
android:src="#drawable/tasto_logout"
/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
It is not recommended to use scrollables containing other scrollables - your layout contains ListView inside Scrollview.
You can design your layout as a ListView - you can add header and footer views(welcomeView, logoutBtn, etc..) to the top and bottom of listview - see methods ListView.addHeaderView and addFooterView) - scrolling will be managed by ListView,
or you can replace listview in your original layout with simple LinearLayout and populate it manually row by row(do not forget to assign click listeners to rows etc..) - scrolling will be managed by ScrollView.
To find more information about this problem, try to google "android listview inside scrollview", i am sure it will help you. :-)

How to stretch my listview to scrollview's heigth?

How to stretch the listView in this given code to match his grandparents frame(parent = linearlayout, grandparent = scrollview). so far, my listview stretches upto 3 items only. :(
here's the code:
<ScrollView android:layout_span="2" android:layout_width="fill_parent" android:layout_height="90pt">
<LinearLayout android:id="#+id/linearLayout1"
android:orientation="vertical" android:layout_span="2"
android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:layout_height="match_parent">
<TextView android:id="#android:id/empty" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:typeface="monospace" android:layout_height="0dip" android:text="#string/immu_no_list_data_text" android:layout_weight="1.0" android:textSize="20dp"></TextView>
<ListView android:layout_weight="1.0" android:layout_width="wrap_content" android:id="#+id/list_immu" android:layout_gravity="center_vertical" android:layout_height="fill_parent"></ListView>
</LinearLayout>
</ScrollView>
Any particular reason why you need a scrollview?
ListView and ScrollView don't play well together. The list view will handle scrolling itself. Try:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView ... />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>

Categories

Resources