老规矩,线上代码为敬;
内容:在scrollview中添加listview,gridview并实现scrollview上啦刷新下拉加载。(此代码只是实现了外观,并没有事件的处理,事件处理鉴于demo,由于时间久远,忘记demo原作者是哪位,对此深表歉意,看到后望见谅。)
要点:设置scrollview中listview,gridview的显示高度,如果不设置会导致listview和gridview只显示一行的内容。
设置显示有两种方法,一种如下,手动设置。另一种,自定义listview,gridview,贴上代码:(
自定义一个类,继承listview,实现这个方法-
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
// int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
//int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, mExpandSpec);
}
注:listview和gridview相同。
)
--------------------------------------------------分割线,上面为自定义listview gridview-------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.example.scrollview_listview;
import java.util.ArrayList;
import java.util.List;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
public class MainActivity extends Activity {
PullToRefreshScrollView mPullRefreshScrollView;
ScrollView mScrollView;
ListView listview_1;
ListView listview_2;
listview_adaoter adapter;
List<String> list;
List<Integer> listimage;
adapter_gridview adapter1;
GridView gridview;
Handler hand;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list=new ArrayList<String>();
list.add("1你好呀"+"/n");
list.add("2你好呀"+"/n");
list.add("3你好呀"+"/n");
list.add("4你好呀"+"/n");
list.add("5你好呀");
list.add("6你好呀");
list.add("6你好呀撒旦撒旦撒旦撒旦撒的撒旦撒大声点撒旦撒旦撒旦撒旦撒旦撒旦撒旦撒旦撒旦撒旦撒旦撒旦撒大");
listimage=new ArrayList<Integer>();
setContentView(R.layout.activity_main);
listview_1=(ListView) findViewById(R.id.listview_1);
listview_2=(ListView) findViewById(R.id.listview_2);
adapter=new listview_adaoter(getApplicationContext(), list);
listview_1.setAdapter(adapter);
listview_2.setAdapter(adapter);
setListViewHeightBasedOnChildren(listview_2);
setListViewHeightBasedOnChildren(listview_1);
//listview_2.setAdapter(adapter);
hand=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
adapter1=new adapter_gridview(listimage, getApplicationContext());
gridview=(GridView) findViewById(R.id.gridview);
gridview.setAdapter(adapter1);
setGridViewHeightBasedOnChildren(gridview);
break;
default:
break;
}
super.handleMessage(msg);
}
};
new Thread(){
public void run() {
listimage.add(R.drawable.ic_launcher);
listimage.add(R.drawable.ic_launcher);
listimage.add(R.drawable.ic_launcher);
listimage.add(R.drawable.ic_launcher);
listimage.add(R.drawable.ic_launcher);
listimage.add(R.drawable.ic_launcher);
listimage.add(R.drawable.ic_launcher);
listimage.add(R.drawable.ic_launcher);
Message mess=new Message();
mess.what=1;
hand.sendMessage(mess);
};
};
//setListViewHeightBasedOnChildren(listview_1);
//setListViewHeightBasedOnChildren(listview_2);
}
//设置listview的显示
public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
// 计算子项View 的宽高
listItem.measure(0, 0);
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
//设置gridview显示问题
public void setGridViewHeightBasedOnChildren(GridView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < (len/3+1); i++) {
// listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
// 计算子项View 的宽高
listItem.measure(0, 0);
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getVerticalSpacing() * ((listAdapter.getCount()/3 )-1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
}