博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ScrollView中嵌套ListView
阅读量:6712 次
发布时间:2019-06-25

本文共 5756 字,大约阅读时间需要 19 分钟。

第一种方法:

在计算listview总高度并设置

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

ScrollView下只能有一个字布局

item布局

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

MainActivity.java(红色为方法)

package com.bawei.day06_scrollview;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.MeasureSpec;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ListView;public class MainActivity extends Activity {    private ListView listView;    private List
list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); list = new ArrayList
(); for (int i = 0; i < 200; i++) { list.add(i+""); } BaseAdapter adapter=new Adper(MainActivity.this,list); listView.setAdapter(adapter); } //界面加载完 @Overridepublic void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); setListViewHeightBasedOnChildren(listView);} public void setListViewHeightBasedOnChildren(ListView listView) { //BaseAdapter为listview布局 BaseAdapter listAdapter = (BaseAdapter) listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if(listItem != null){ listItem.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); listItem.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + listView.getPaddingTop() + listView.getPaddingBottom(); listView.setLayoutParams(params); } }

适配器

package com.bawei.day06_scrollview;import java.util.List;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class Adper extends BaseAdapter {    Context context;    List
list; public Adper(Context context, List
list) { // TODO Auto-generated constructor stub this.context=context; this.list=list; } public int getCount() { // TODO Auto-generated method stub return list.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder viewHolder; if(convertView==null){ convertView=View.inflate(context, R.layout.listview, null); viewHolder=new ViewHolder(); viewHolder.textView=(TextView) convertView.findViewById(R.id.text); convertView.setTag(viewHolder); }else{ viewHolder=(ViewHolder) convertView.getTag(); } viewHolder.textView.setText(list.get(position)); return convertView; }class ViewHolder{ TextView textView;}}

第二种方法

 自定义ListView,重载onMeasure()方法,设置全部显示

MainActivity.java

package com.bawei.day06;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ListView;public class MainActivity extends Activity {    private ListView listView;    private List
list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); list = new ArrayList
(); for (int i = 0; i < 200; i++) { list.add(i+""); } ArrayAdapter
adapter=new ArrayAdapter
(this, android.R.layout.simple_expandable_list_item_1,list); listView.setAdapter(adapter); } }

重写listview

ListViewForScrollView.java

package com.bawei.day06;import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;public class ListViewForScrollView extends ListView {    public ListViewForScrollView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    @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);    }}

 

转载于:https://www.cnblogs.com/1426837364qqcom/p/5388618.html

你可能感兴趣的文章
关于sql语句的优化问题
查看>>
SQL Server 2016新特性:数据库级别配置
查看>>
中美贸易战升级 医疗器械行业影响大
查看>>
嵌入的iframe框架自适应宽度代码
查看>>
IPTABLES常用命令之配置生产环境IPTABLES及优化
查看>>
linux服务ssh详解
查看>>
cat命令一些不常用但很有用的参数
查看>>
双色球 脱壳加去效验
查看>>
#22 系统进程调度、at、batch、mail、crontab
查看>>
Intellij IDEA Debug调试技巧
查看>>
PHP Warning: PHP Startup: unable to load dynamic library
查看>>
metasploit(MSF)终端命令大全
查看>>
Linux下php安装Redis扩展
查看>>
管理信息系统测试方法总结(二)
查看>>
EMC与NetApp NAS对比
查看>>
linux系统的启动流程
查看>>
CRLF注入漏洞
查看>>
(一) virtualenv虚拟环境安装
查看>>
利用ssh+rsync+inotify实现数据的异地实时同步
查看>>
Android官方开发文档Training系列课程中文版:分享简单数据之从其它APP接收简单数据...
查看>>