編輯:關於Android編程
在實際項目中,我們通常回遇到Scroll嵌套listview,listview顯示不全的問題,有以下三種方案,推薦使用第三種;
項目截圖:
方案一:手動設置listview的高度
1.布局文件:first.xml
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); } }
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(){ 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); Utitily.setListViewHeightBasedOnChildren(listView); } }
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 Listdata; 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
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); } }
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 Listdata; 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
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); } }
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); } }
[TOC]剛好項目中要實現這個布局效果,做完後在這裡分享出來給大家學習~效果圖:實現的功能:1、單行多行切換顯示2、單選和取消選擇源碼/** * * @author j
使用PopupWindow來實現彈出框,並且帶有動畫效果首先自定義PopupWindowpublic class LostPopupWindow extends Popu
Android 虛化圖片 模糊圖片 圖片毛玻璃效果。 效果如圖: 在Android可以用RenderScript方便的實現這個方法: private void blu
菜單是用戶界面中最常見的元素之一,使用非常頻繁,在Android中,菜單被分為如下三種,選項菜單(OptionsMenu)、上下文菜單(ContextMenu)和子菜單(