当前位置:操作系统 > 安卓/Android >>

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;&

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,