當ScrowView巢狀listView載入完資料後列表自動滾動到最頂端的問題
阿新 • • 發佈:2019-02-11
當ScrowView巢狀listView時,而且在listview上端還有內容的時候,導致listview載入完資料後,自動滾動到最頂端的問題,以下是解決方案,即在外層再加一個LinearLayout ,就能解決,以下是例項
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_backgroud"
android:descendantFocusability="blocksDescendants"
android:id="@+id/ll_fragment_home">
<ScrollView
android:id="@+id/sv_histrack"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.mxnavi.drivemanager.demo.ui.MyListView
android:id="@+id/lv_ranking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/normal_list_bg"
android:divider="@color/list_divider"
android:dividerHeight="1dip"
android:gravity="center"
android:scrollbars="none"
android:background="@color/list_backgroud"
>
</com.mxnavi.drivemanager.demo.ui.MyListView>
</ScrollView>
</LinearLayout>
```
MyListView 的程式碼:
```
public class MyListView extends ListView {
public MyListView(Context context)
{
super(context);
}
public MyListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expandSpec= View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
View.MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}