Android笔记 四种方案解决ScrollView嵌套ListView问题之动态设置ListView高度代码整理

我用的是动态更改ListView高度自适应ScrollView:

步骤:

创建Java文件DynaSetListViewHeight,代码如下:

package com.zhb.ph.fragment;

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

public class DynaSetListViewHeight {
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}
}

在设置ListView适配器后调用该方法,我用的是假数据,整体Java代码如下:

package com.zhb.ph.fragment;

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

import com.zhb.app.R;
import com.zhb.ph.entity.SActivities;
import com.zhb.ph.ui.SSearchActivity;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SearchFragment extends Fragment {

	private ListView search_sactivities_lv;
	private RelativeLayout header_center_rl;
	private List sactivities = new ArrayList();
	private SActivitiesAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_index_search, null);

		// 点击搜索跳转事件
		header_center_rl = (RelativeLayout) view
				.findViewById(R.id.search_header_center);
		header_center_rl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				startActivity(new Intent(getActivity(), SSearchActivity.class));
			}
		});

		// 优惠活动
		search_sactivities_lv = (ListView) view
				.findViewById(R.id.search_sactivities_lv);
		adapter = new SActivitiesAdapter(getActivity());
		search_sactivities_lv.setAdapter(adapter);
		SActivities sactivitie = new SActivities(1, "大众美发", "优惠活动", "50000",
				"5", null);
		SActivities sactivitie2 = new SActivities(2, "大众美发", "优惠活动", "50000",
				"5", null);
		SActivities sactivitie3 = new SActivities(3, "大众美发", "优惠活动", "50000",
				"5", null);
		SActivities sactivitie4 = new SActivities(4, "大众美发", "优惠活动", "50000",
				"5", null);

		sactivities.add(sactivitie);
		sactivities.add(sactivitie2);
		sactivities.add(sactivitie3);
		sactivities.add(sactivitie4);
		adapter.setSActivities(sactivities);

		// ////////////////动态设置ListView高度方法调用代码

		DynaSetListViewHeight
				.setListViewHeightBasedOnChildren(search_sactivities_lv);

		// ////////////////
		adapter.notifyDataSetChanged();

		search_sactivities_lv
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView parent, View view,
							int position, long id) {

						// 获取当前点击的选项对应的数据
						SActivities sactivitiesdet = sactivities.get(position);
						Toast.makeText(getActivity(),
								"当前ID" + sactivitiesdet.getShopid(),
								Toast.LENGTH_SHORT).show();
					}

				});

		return view;

	}
}

// 创建自定义适配器方法
class SActivitiesAdapter extends BaseAdapter {

	private Context ctx;
	private LayoutInflater inflater;
	private List sactivities;

	public SActivitiesAdapter(Context ctx) {
		this.ctx = ctx;
		inflater = LayoutInflater.from(ctx);
	}

	public void setSActivities(List sactivities) {
		this.sactivities = sactivities;
	}

	@Override
	public int getCount() {
		return sactivities == null ? 0 : sactivities.size();
	}

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

	// 选项数据的ID
	@Override
	public long getItemId(int position) {
		return sactivities.get(position).getShopid().intValue();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.listview_search_sactivities_model, null);

			/*
			 * ImageView shopimg = (ImageView) convertView
			 * .findViewById(R.id.searchmodel_sactivities_shopimg);
			 */
			vh = new ViewHolder();

			vh.shopname = (TextView) convertView
					.findViewById(R.id.searchmodel_sactivities_shopname);
			vh.shopactivity = (TextView) convertView
					.findViewById(R.id.searchmodel_sactivities_shopactivity);
			vh.shoptrafficnum = (TextView) convertView
					.findViewById(R.id.searchmodel_sactivities_shoptrafficnum);
			vh.shopscordnum = (TextView) convertView
					.findViewById(R.id.searchmodel_sactivities_shopscordnum);

			// 把视图钩子存放到convertView
			convertView.setTag(vh);

		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		SActivities item = sactivities.get(position);

		/* vh.shopimg.setImageURI(item.getShopimg()); */
		vh.shopname.setText(item.getShopname());
		vh.shopactivity.setText(item.getShopactivity());
		vh.shoptrafficnum.setText(item.getTraffic());
		vh.shopscordnum.setText(item.getScord());

		return convertView;

	}

	private class ViewHolder {
		/*
		 * ImageView shopimg;
		 */

		TextView shopname;
		TextView shopactivity;
		TextView shoptrafficnum;
		TextView shopscordnum;
	}

}

效果图:

Android笔记 四种方案解决ScrollView嵌套ListView问题之动态设置ListView高度代码整理_第1张图片

*注意:子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。

以下是我的三个xml文件:

fragment_index_search.xml:



     
     
     
     

listview_search_sactivities.xml:



listview_search_sactivities_model.xml:



另外三种还未来得及整理,这里选出了一篇转载后的关于另外三种解决方法的博客,因为原博客(http://www.apkbus.com/android-11540-1.html)需要注册才能查看相关图片等,所以选择了这个,地址如下:

http://blog.sina.com.cn/s/blog_46798aa80101lxbk.html#cmt_2776271

你可能感兴趣的:(移动端笔记)