問題出現原因:佈局中用到了ListView,但是無奈界面上又不是隻有ListView 當內容變多時,需要滾動界面。大致佈局如下
- <?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" >
- <ScrollView
- android:id="@+id/scSpace"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/shadow_title" />
- <ListView
- android:id="@+id/listview_personalhealthindex"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/app_bg" >
- </ListView>
- </LinearLayout>
- </ScrollView>
- </LinearLayout>
滾動的解決辦法1:在
- listView.setAdapter(indexAdapter);
- public static void setListViewHeightBasedOnChildren(ListView listView) {
- ListAdapter listAdapter = listView.getAdapter();
- if (listAdapter == null) {
- // pre-condition
- return;
- }
- int totalHeight = 0;
- for (int i = 0; i < listAdapter.getCount(); i++) {
- View listItem = listAdapter.getView(i, null, listView);
- listItem.measure(0, 0);
- totalHeight += listItem.getMeasuredHeight();
- }
- ViewGroup.LayoutParams params = listView.getLayoutParams();
- params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
- listView.setLayoutParams(params);
- }
即可解決,但是這樣會造成一個小的問題打開該界面時,會顯示到底部而不是從頂部顯示
滾動的解決辦法2:
實現原理 重寫onMeasure代碼,停用GridView的滾動功能
這裏以GridView爲例,重寫GridView,佈局代碼如下:ListView同樣的原理
- <com.example.ui.MyGridView
- android:id="@+id/healthhomereson_gv"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="@dimen/margin_medium"
- android:background="@color/white"
- android:descendantFocusability="blocksDescendants"
- android:numColumns="3" >
- </com.example.ui.MyGridView>
- package com.example.ui;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.widget.GridView;
- public class MyGridView extends GridView {
- public MyGridView(Context context) {
- super(context);
- }
- public MyGridView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyGridView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- //該自定義控件只是重寫了GridView的onMeasure方法,使其不會出現滾動條,ScrollView嵌套ListView也是同樣的道理,不再贅述。
- @Override
- public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int expandSpec = MeasureSpec.makeMeasureSpec(
- Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
- super.onMeasure(widthMeasureSpec, expandSpec);
- }
- }
現實頂部的解決辦法1:初始化的時候就讓該界面的頂部的某一個控件獲得焦點,滾動條自然就顯示到頂部了,代碼如下:
- txtBaseMsg.setFocusable(true);
- txtBaseMsg.setFocusableInTouchMode(true);
- txtBaseMsg.requestFocus();
- listview.post(new Runnable() {
- @Override
- public void run() {
- ((ScrollView)findViewById(R.id.scSpace)).scrollTo(0, 0);
- }
- });