Focus ScrollView On Top Of Page in android

2019-08-09 05:58发布

问题:

I am having an Activity in which I am having some View that is textViews ImageViews and MapView and last is listview on the page.

All these controlls are in a scroll view. The problem is that when the list view populates the scroll scrollview is also scrolls to the listview. The focus is not on the top of page is always on bottom of the page.

I also used android:focusable="false" for Listview in xml and in code ListView.setFocusable(false); But this is not working, so please suggest any alternatives.

回答1:

Vipin is correct in that you can't do a listview inside a scroll view. You could also setup a tablelayout that is made dynamic in Java based off how many items are in your cursor (for loop).

So in other words, setup your XML with a scroll view. Add items to it that will show at the top, then at bottom put your table. Build table in Java with buildRow(). Then call it after you call your setContentView(R.layout.xmllayout);



回答2:

Two things you can have a ListView in a ScrollView, you just have to make the ListView a permanent size(without scrolling) when using it.

great example here NonScrollListView

For the ScrollView starting on the ListView, I recommend using android:focusable="false" like you already have in the ListView XML then using

    ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
    scrollView.requestFocus(View.FOCUS_UP);
    scrollView.scrollTo(0,0);


回答3:

you can not use list view inside scroll view you can do two things to get the same

(1)decrease the height of you views so that they can be fit to actual screen and remove scroll view and defaul scroller of views will work.

(2)intead of list view try to create layout having rows as list view

LinearLayout parent = new LinearLayout(this);
parent.setOrientation(VERTICAL);
parent.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);//

for(number of rows in your list)
{
LinearLayout row= new LinearLayout(this);
row.setOrientation(Horizontal);
row.setLayoutParams(new LayoutParams(row_width,row_height);

TextView text= new TextView (this);
text.setText("your text");
text.setLayoutParams(new LayoutParams(text_width,text_height);
row.addView(text);
.
.//other view you want to add as check box etc
.
.
parent.AddView(row);
}

this.addView(parent);

hope this will help you

note:but in case you have a large number of rows it may cause memory issues


回答4:

I tried many alternatives but only this worked for me:

    ScrollView scroll = (ScrollView) findViewById(R.id.addresses_scroll);

    scroll.setFocusableInTouchMode(true);
    scroll.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);


回答5:

In case you have listview as child in scrollview, scrollview will move its focus to listview. To avoid so i tried many options, but this one worked perfectly for me:

 scroll_main.pageScroll(View.FOCUS_UP);