scrollview嵌套listview确实有方法实现,但是并不为官方推荐。
You should never use a ScrollView with a ListView
, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.
至于应该怎么做最合适我大概有个思路还没有实现,就是只用一个listview,填充不同的布局,这只是我的想法,我并不知道这种做法是否是最恰当的做法,但这种做法确实没有用到scrollview嵌套listview。
但是我还是总结了网上分享的方法以备不时之需:
原文:http://www.lai18.com/content/2146670.html
[code] /** * 动态改变ListView的高度 * @param listView */ public static void setListViewHeightBasedOnChildren(ListView listView) { if(listView == null) return; ListAdapter adapter = listView.getAdapter(); if (adapter == null) { return; } int totalHeight = 0; // 开始计算ListView里所有Item加起来的总高度 for (int i = 0; i < adapter.getCount(); i++) { View listItem = adapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); // 高度 = 所有分割线高度 + Item总高度 params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1)); listView.setLayoutParams(params); }
[code] @Override public View getView(int position, View convertView, ViewGroup parent) { int viewType = this.getItemViewType(position); switch (viewType) { case TYPE_ONE: TypeOneHolder holderOne; View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.layout_mylistlist_item_type_1, parent, false); holderOne = new TypeOneHolder(); holderOne.text = (TextView) v.findViewById(R.id.mylist_itemname); v.setTag(holderOne); } else { holderOne = (TypeOneHolder) v.getTag(); } MyListItem myItem = adapterData.get(position); if (myItem != null) { if (holderOne.text != null) { holderOne.text.setText(myItem.getItemName()); } } return v; case TYPE_TWO: TypeTwoHolder holder2; View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.layout_mylistlist_item_type_2, parent, false); holder2 = new TypeTwoHolder(); holder2.text = (TextView) v.findViewById(R.id.mylist_itemname); holder2.icon = (ImageView) v.findViewById(R.id.mylist_itemicon); v.setTag(holderOne); } else { holder2 = (TypeTwoHolder) v.getTag(); } MyListItem myItem = adapterData.get(position); if (myItem != null) { if (holder2.text != null) { holder2.text.setText(myItem.getItemName()); } if (holder2.icon != null) holder2.icon.setDrawable(R.drawable.icon1); } return v; default: //Throw exception, unknown data type break; } }
[code]public class ListViewLayout extends LinearLayout { private BaseAdapter adapter; private OnClickListener onClickListener; public ListViewLayout(Context context) { super(context); } public ListViewLayout(Context context, AttributeSet attrs) { super(context, attrs); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public ListViewLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public ListViewLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } /** * 设置BaseAdapter和OnClickListener * * @param adapter * @param onClickListener */ public void setAdapterAndListener(BaseAdapter adapter, OnClickListener onClickListener) { this.init(adapter, onClickListener); int count = this.adapter.getCount(); this.removeAllViews(); // 初始化LinearLayout内容 for (int i = 0; i < count; i++) { View v = this.adapter.getView(i, null, null); v.setOnClickListener(this.onClickListener); this.addView(v, i); } } private void init(BaseAdapter adapter, OnClickListener onClickListener) { this.adapter = adapter; this.onClickListener = onClickListener; } }
[code]ListViewLayout listViewLayout = (ListViewLayout) this.findViewById(R.id.listViewLayout); listViewLayout.setAdapterAndListener(adapter,listener);
[code]public class SVListView extends ListView { public SVListView(Context context) { super(context); } public SVListView(Context context, AttributeSet attrs) { super(context, attrs); } public SVListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public SVListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } /** * 重新算高度,适应ScrollView的效果 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
[code]ScrollView sv = (ScrollView) findViewById(R.id.scrollview); // 手动把ScrollView滚动至顶端 sv.smoothScrollTo(0, 0);