android - How to preven the gridview scroll by default once dataset changed -
i have activity contains scrollview, , have gridview inside scrollview, layout:
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root"> <relativelayout android:padding="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> ......... <com.test.android.view.scrollablegridview android:layout_width="match_parent" android:layout_height="wrap_content" android:numcolumns="auto_fit" android:columnwidth="100dp" android:verticalspacing="2dp" android:focusable="false" android:clickable="false"> </com.test.android.view.scrollablegridview> </relativelayout> </scrollview> public class scrollablegridview extends gridview { @override public void onmeasure(int widthmeasurespec, int heightmeasurespec) { int expandspec = measurespec.makemeasurespec(integer.max_value >> 2, measurespec.at_most); super.onmeasure(widthmeasurespec, expandspec); viewgroup.layoutparams params = getlayoutparams(); params.height = getmeasuredheight(); } } why use custome gridview make sure gridview can expand max height(check this).
now once activity loaded, load data server, call the:
gridadapter.notifydatasetchanged(); then activity scroll grid view means user can not see content above gridview.
i have tried that:
mscrollview.scrollto(0,mscrollview.getbottom()); but not work.
any idea fix it?
issue:
gridview being scrolled automatically.
reason:
when screen loaded, finds the first focusable view viewhierarchy , sets focus view.
solution:
you can set focus other view android not focus gridview @ first,so won't scroll automatically.
example:
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root"> <relativelayout android:padding="10dp" android:id="@+id/rlcontainer" //you may set id , requestfocus java code android:focusable="true" //add android:focusableintouchmode="true" //add android:layout_width="match_parent" android:layout_height="match_parent"> ......... //you can add focusable,focusableintouchmode other view before gridview <com.test.android.view.scrollablegridview android:layout_width="match_parent" android:layout_height="wrap_content" android:numcolumns="auto_fit" android:columnwidth="100dp" android:verticalspacing="2dp" android:focusable="false" android:clickable="false"> </com.test.android.view.scrollablegridview> </relativelayout> from java code,
inside oncreate() method of activity
relativelayout rlcontainer = (relativelayout) findviewbyid(r.id.rlcontainer); rlcontainer.requestfocus();
Comments
Post a Comment