Android ScrollView與ListView,GridView共存衝突解決方案

我們在真實項目中通常會遇到ListView或者GridView嵌套在ScrollView中問題。但是做的時候會發現,一旦兩者進行嵌套,即會發生衝突。得不到我們希望的效果。由於ListView和GridView本身都繼承於ScrollView,一旦在ScrollView中嵌套ScrollView,那麼裏面的ScrollView高度計算就會出現問題。我們也就無法得到想要的效果。下面進入正題,我們將分別討論ScrollView中嵌套ListView和FGridView的情況:

 

     核心解決方案: 重寫ListView或者GridView的OnMesure 方法:

  

[java] 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // TODO Auto-generated method stub 
    int expandSpec = MeasureSpec.makeMeasureSpec(  
               Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);  
     
    super.onMeasure(widthMeasureSpec, expandSpec); 

 

 

    一、ScrollView中嵌套ListView

  BlogScrollViewActivity.java代碼:

[java] 
package com.csdn.blog.scrollview; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.ScrollView; 
import android.widget.LinearLayout.LayoutParams; 
import android.widget.TextView; 
 
public class BlogScrollViewActivity extends Activity { 
    /** Called when the activity is first created. */ 
//  MyGridView  grid; 
    ImageView image; 
    ScrollView scroll; 
    String[] texts=new String[]{"無線","通話設置","聲音","顯示","位置", 
                                "應用","賬戶","隱私權","存儲","語言","遊戲","娛樂","電影","音樂", 
                                "輔助功能","日期"}; 
/*  ArrayAdapter<String> adapter;*/ 
    TestListView list; 
    LinearLayout.LayoutParams lp; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        init(); 
    } 
    void init(){ 
        list=(TestListView)findViewById(R.id.list); 
        image=(ImageView)findViewById(R.id.image); 
        list.setAdapter(new GridAdapter(this)); 
        scroll=(ScrollView)findViewById(R.id.scroll); 
        scroll.requestChildFocus(image, null); 
 
    } 
    private class GridAdapter extends BaseAdapter{ 
 
        Activity context; 
        public GridAdapter(Activity context){ 
            this.context=context; 
        } 
        @Override 
        public int getCount() { 
            // TODO Auto-generated method stub 
            return texts.length; 
        } 
 
        @Override 
        public Object getItem(int position) { 
            // TODO Auto-generated method stub 
            return null; 
        } 
 
        @Override 
        public long getItemId(int position) { 
            // TODO Auto-generated method stub 
            return 0; 
        } 
 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            // TODO Auto-generated method stub 
            ViewHolder holder=null; 
            if(convertView==null){ 
                convertView=context.getLayoutInflater().inflate(R.layout.item, null); 
                holder=new ViewHolder(); 
                holder.text=(TextView)convertView.findViewById(R.id.grid_text); 
                convertView.setTag(holder); 
            } 
            else{ 
                holder=(ViewHolder)convertView.getTag(); 
            } 
            holder.text.setText(texts[position]); 
            return convertView; 
        } 
        class ViewHolder { 
            TextView text; 
        } 
         
    } 

TestListView.java代碼如下:

[java]
package com.csdn.blog.scrollview; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View.MeasureSpec; 
import android.widget.ListView; 
 
public class TestListView extends ListView{ 
 
    public TestListView(Context context) { 
        super(context); 
        // TODO Auto-generated constructor stub 
    } 
    public TestListView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        // TODO Auto-generated constructor stub 
    } 
    public TestListView(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
        // TODO Auto-generated constructor stub 
    } 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
        // TODO Auto-generated method stub 
        int expandSpec = MeasureSpec.makeMeasureSpec(  
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);  
         
        super.onMeasure(widthMeasureSpec, expandSpec); 
    } 
 

 

main.xml代碼:

[html]
<p><?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"  
    android:background="#FFFFFF" 
    > 
    <ScrollView 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:fadingEdgeLength="0dp" 
        android:scrollbars="none" 
        android:id="@+id/scroll"> 
        <LinearLayout 
            android:layout_height="fill_parent" 
            android:layout_width="fill_parent" 
            android:orientation="vertical" 
            > 
      <ImageView 
          android:id="@+id/image" 
          android:layout_height="150dp" 
          android:layout_width="fill_parent" 
          android:padding="2dp" 
          android:scaleType="centerCrop" 
          android:src="@drawable/fruit"</p><p>          /> 
      <com.csdn.blog.scrollview.TestListView 
          android:id="@+id/list" 
          android:layout_height="fill_parent" 
          android:layout_width="fill_parent" 
          android:fadingEdgeLength="0dp" 
          android:scrollbars="none" 
          /> 
        </LinearLayout> 
    </ScrollView></p><p></LinearLayout></p> 
 

效果圖如下:


    

  這裏我的佈局方式是上面一張圖片,下面放置listView。

  對於此種佈局方式,可以通過另外一種方式避免此問題。由於ListView有addHeadView()方法,那麼我們可以直接將上面想加入的View通過  getLayoutInflater().inflate(this,R.layout.***) 加入到ListView的頂部即可。

 

  二、ScrollView中嵌套GridView的解決方案。

      ScrollView中嵌套GridView ,最簡單的方法就是重寫GridView方法,使其在繪製時重新計算GridView高度

     MyGridView.java代碼如下:

[java]
package com.csdn.blog.scrollview; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.GridView; 
 
public class MyGridView extends GridView{ 
 
    public MyGridView(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
        // TODO Auto-generated constructor stub 
    } 
    public MyGridView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        // TODO Auto-generated constructor stub 
    } 
    public MyGridView(Context context) { 
        super(context); 
        // TODO Auto-generated constructor stub 
    } 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
        // TODO Auto-generated method stub 
        int expandSpec = MeasureSpec.makeMeasureSpec(  
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);  
        super.onMeasure(widthMeasureSpec, expandSpec);  
 
    } 

main.xml代碼如下:

[java] 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"  
    android:background="#FFFFFF" 
    > 
    <ScrollView 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:fadingEdgeLength="0dp" 
        android:scrollbars="none" 
        android:id="@+id/scroll"> 
        <LinearLayout 
            android:layout_height="fill_parent" 
            android:layout_width="fill_parent" 
            android:orientation="vertical" 
            > 
            <ImageView 
                android:id="@+id/image" 
                android:layout_height="150dp" 
                android:layout_width="fill_parent" 
                android:padding="2dp" 
                android:scaleType="centerCrop" 
                android:src="@drawable/fruit" 
 
                /> 
            <com.csdn.blog.scrollview.MyGridView 
                android:layout_marginTop="10dp" 
                android:id="@+id/grid" 
                android:layout_height="fill_parent" 
                android:layout_width="fill_parent" 
                android:fadingEdgeLength="0dp" 
                android:scrollbars="none" 
                android:numColumns="3" 
                /> 
        </LinearLayout> 
    </ScrollView> 
 
</LinearLayout> 

主類主要就是GridVIew數據綁定。簡單貼下代碼:

[java]
package com.csdn.blog.scrollview; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.ScrollView; 
import android.widget.LinearLayout.LayoutParams; 
import android.widget.TextView; 
 
public class BlogScrollViewActivity extends Activity { 
    /** Called when the activity is first created. */ 
    MyGridView  grid; 
    ImageView image; 
    ScrollView scroll; 
    String[] texts=new String[]{"無線","通話設置","聲音","顯示","位置", 
                                "應用","賬戶","隱私權","存儲","語言","遊戲","娛樂","電影","音樂", 
                                "輔助功能","日期"}; 
    ArrayAdapter<String> adapter; 
    LinearLayout.LayoutParams lp; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        init(); 
    } 
    void init(){ 
        image=(ImageView)findViewById(R.id.image); 
        grid=(MyGridView)findViewById(R.id.grid); 
        grid.setAdapter(new GridAdapter(this)); 
        scroll=(ScrollView)findViewById(R.id.scroll); 
        scroll.requestChildFocus(image, null); 
 
    } 
    private class GridAdapter extends BaseAdapter{ 
 
        Activity context; 
        public GridAdapter(Activity context){ 
            this.context=context; 
        } 
        @Override 
        public int getCount() { 
            // TODO Auto-generated method stub 
            return texts.length; 
        } 
 
        @Override 
        public Object getItem(int position) { 
            // TODO Auto-generated method stub 
            return null; 
        } 
 
        @Override 
        public long getItemId(int position) { 
            // TODO Auto-generated method stub 
            return 0; 
        } 
 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            // TODO Auto-generated method stub 
            ViewHolder holder=null; 
            if(convertView==null){ 
                convertView=context.getLayoutInflater().inflate(R.layout.item, null); 
                holder=new ViewHolder(); 
                holder.image=(ImageView)convertView.findViewById(R.id.grid_image); 
                holder.text=(TextView)convertView.findViewById(R.id.grid_text); 
                convertView.setTag(holder); 
            } 
            else{ 
                holder=(ViewHolder)convertView.getTag(); 
            } 
            holder.image.setImageResource(R.drawable.meinv); 
            holder.text.setText(texts[position]); 
            return convertView; 
        } 
        class ViewHolder { 
            ImageView image; 
            TextView text; 
        } 
         
    } 

   上述代碼中  scroll.requestChildFocus(image, null); 此句主要是修復了程序進入時GridView會滑動到頂端的小bug。

   效果圖如下:

 

發佈了27 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章