Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 解決ScrollView嵌套ListView問題的三種方法

解決ScrollView嵌套ListView問題的三種方法

編輯:關於Android編程

在實際項目中,我們通常回遇到Scroll嵌套listview,listview顯示不全的問題,有以下三種方案,推薦使用第三種;

項目截圖:

\\

方案一:手動設置listview的高度

1.布局文件:first.xml

 




    
        
            
            
            
            
        

    


2.動態設置listview的高度的類:Utitily
package com.zhw.scrollviewdemo.weight;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * 動態設置ListView的高度
 */
public class Utitily {

        public static void setListViewHeightBasedOnChildren(ListView listView) {
            //獲取ListView對應的Adapter
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }

            int totalHeight = 0;
            for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   //listAdapter.getCount()返回數據項的數目
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);  //計算子項View 的寬高
                totalHeight += listItem.getMeasuredHeight();  //統計所有子項的總高度
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            //listView.getDividerHeight()獲取子項間分隔符占用的高度
            //params.height最後得到整個ListView完整顯示需要的高度
            listView.setLayoutParams(params);
        }


}

3.FirstActivity

 

 

package com.zhw.scrollviewdemo.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.zhw.scrollviewdemo.R;
import com.zhw.scrollviewdemo.adapter.FirstAdapter;
import com.zhw.scrollviewdemo.vo.firstVo;
import com.zhw.scrollviewdemo.weight.Utitily;

import java.util.ArrayList;
import java.util.List;

/**
 * 方案一
 */
public class FirstActivity extends Activity{

    private ListView listView;
    private FirstAdapter fAdapter;
    private Listlist=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        listView= (ListView) findViewById(R.id.listView_first);
        initData();



    }

    /**
     * 准備數據
     */
    private void initData(){
        Listdata=new ArrayList<>();
        for (int i=0;i<20;i++){
            firstVo vo=new firstVo();
            vo.setName("item"+i);
            data.add(vo);
        }
        list.clear();
        list.addAll(data);
        Log.i("info","initData==="+list);
        fAdapter=new FirstAdapter(list,this);
        listView.setAdapter(fAdapter);
        Utitily.setListViewHeightBasedOnChildren(listView);
    }
}

4.FirstAdapter

 

 

package com.zhw.scrollviewdemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhw.scrollviewdemo.R;
import com.zhw.scrollviewdemo.vo.firstVo;

import java.util.List;

/**
 * 方案一的適配器
 */
public class FirstAdapter extends BaseAdapter {

    private Context mContext;
    private List data;

    public FirstAdapter(List data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        Log.i("info","getCount=="+ data.size());
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.list_item,null);

            mHolder=new ViewHolder();

            mHolder.textView= (TextView) convertView.findViewById(R.id.textView);

            convertView.setTag(mHolder);
        }else{
            mHolder= (ViewHolder) convertView.getTag();
        }

        firstVo fir=data.get(position);
        Log.i("info","data=="+data.get(position)+position);
        mHolder.textView.setText(fir.getName());

        return convertView;
    }

    class ViewHolder{
        TextView textView;
    }
}


 

方案二:使用單個ListView取代ScrollView中所有內容

1.two.xml

 



    
    


2.TwoActivity

 

 

package com.zhw.scrollviewdemo.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.zhw.scrollviewdemo.R;
import com.zhw.scrollviewdemo.adapter.TwoAdapter;
import com.zhw.scrollviewdemo.vo.firstVo;
import java.util.ArrayList;
import java.util.List;

/**
 * 方案二
 */
public class Twoctivity extends Activity{

    private ListView listView;
    private Listlist=new ArrayList<>();
    private TwoAdapter twoAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);

        listView= (ListView) findViewById(R.id.listview_two);
        initData();
    }

    /**
     * 准備數據
     */
    private void initData(){
        List data=new ArrayList<>();
        for (int i=0;i<20;i++){
            firstVo vo=new firstVo();
            vo.setName("item"+i);
            data.add(vo);
        }
        list.clear();
        list.addAll(data);
        Log.i("info","initData==="+list);
        twoAdapter=new TwoAdapter(this,list);
        listView.setAdapter(twoAdapter);

    }
}

3.TwoAdapter

 

 

package com.zhw.scrollviewdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.zhw.scrollviewdemo.R;
import com.zhw.scrollviewdemo.vo.firstVo;
import java.util.List;

/**
 * 方案二的適配器
 */
public class TwoAdapter extends BaseAdapter {

    private Context mContext;
    private List data;

    public TwoAdapter(Context mContext, List data) {
        this.data = data;
        this.mContext = mContext;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        if(position == 0){
            convertView = LayoutInflater.from(mContext)
                    .inflate(R.layout.two_list_item01, null);
            return convertView;
        }
        //列表最後一項
        else if(position == 19){
            convertView = LayoutInflater.from(mContext)
                    .inflate(R.layout.two_list_item03, null);
            return convertView;
        }

        ViewHolder viewHolder;
        if (convertView == null || convertView.getTag() == null){
            convertView = LayoutInflater.from(mContext)
                    .inflate(R.layout.two_list_item02, null);
            viewHolder=new ViewHolder();
            viewHolder.textView= (TextView) convertView.findViewById(R.id.two_textView);

            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }

        firstVo fir=data.get(position);

        viewHolder.textView.setText(fir.getName());
        return convertView;


    }

    class ViewHolder{
        TextView textView;
    }
}


 

方案三:自定義可適應ScrollView的ListView(推薦:自定義的listview)

1.four.xml

 




    
        
            

            
            

            

        


    


2.自定義的 listview

 

 

package com.zhw.scrollviewdemo.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 *自定義的listview
 */
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
    /**
     * 重寫該方法,達到使ListView適應ScrollView的效果
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

3.FourActivity

 

 

package com.zhw.scrollviewdemo.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;

import com.zhw.scrollviewdemo.R;
import com.zhw.scrollviewdemo.adapter.FirstAdapter;
import com.zhw.scrollviewdemo.vo.firstVo;
import com.zhw.scrollviewdemo.weight.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 方案三
 */
public class FourActivity extends Activity{

    private MyListView listView;
    private FirstAdapter fAdapter;
    private Listlist=new ArrayList<>();
    private ScrollView scrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.four);
        listView= (MyListView) findViewById(R.id.listView_four);
        scrollView= (ScrollView) findViewById(R.id.scroll_four);
        scrollView.smoothScrollTo(0,0);
        initData();
    }
    /**
    * 准備數據
    */
    private void initData(){
        List data=new ArrayList<>();
        for (int i=0;i<20;i++){
            firstVo vo=new firstVo();
            vo.setName("item"+i);
            data.add(vo);
        }
        list.clear();
        list.addAll(data);
        Log.i("info","initData==="+list);
        fAdapter=new FirstAdapter(list,this);
        listView.setAdapter(fAdapter);
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved