博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android ScrollView嵌套ListView嵌套GridView的上下拉以及加载更多
阅读量:6071 次
发布时间:2019-06-20

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

ScrollView 效果

ScrollView 说明

一个ScrollView 嵌套ListView 嵌套GridView的上拉加载更多,下拉刷新的demo。

主要是重写了GridView和LsitView中的onMeasure 方法。
写的比较简单,希望对初学者有帮助。

/**** 主要思路是* ScrollView 嵌套ListView 嵌套GridView的上拉加载更多,下拉刷新。* * 逻辑在适配器做了处理* * 我们只让ListView加载2个数据Item,第一个是item对象,第二个是一个对象* */public class MainActivity extends Activity implements OnHeaderRefreshListener,                OnFooterRefreshListener {        MyAdapter myAdapter;        // 自定义的GridView的上下拉动刷新        private PullToRefreshView mPullToRefreshView;        private MyListView listView;        private List
data; private List
gridViewData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPullToRefreshView = (com.example.demo.PullToRefreshView) findViewById(R.id.main_pull_refresh_view); listView = (MyListView) findViewById(R.id.gridView1); myAdapter = new MyAdapter(this); data = new ArrayList
(); gridViewData = new ArrayList
(); for (int i = 0; i < 2; i++) { data.add(i); } for (int i = 0; i < 12; i++) { if (i % 2 == 0) { gridViewData.add(R.drawable.pic1); } else { gridViewData.add(R.drawable.pic2); } } myAdapter.setData(data); myAdapter.setGridViewData(gridViewData); listView.setAdapter(myAdapter); mPullToRefreshView.setOnHeaderRefreshListener(this); mPullToRefreshView.setOnFooterRefreshListener(this); mPullToRefreshView.setLastUpdated(new Date().toLocaleString()); } @Override public void onHeaderRefresh(PullToRefreshView view) { mPullToRefreshView.postDelayed(new Runnable() { @Override public void run() { mPullToRefreshView.onHeaderRefreshComplete("更新于:" + Calendar.getInstance().getTime().toLocaleString()); mPullToRefreshView.onHeaderRefreshComplete(); Toast.makeText(MainActivity.this, "数据刷新完成!", 0).show(); } }, 3000); } @Override public void onFooterRefresh(PullToRefreshView view) { mPullToRefreshView.postDelayed(new Runnable() { @Override public void run() { mPullToRefreshView.onFooterRefreshComplete(); gridViewData.add(R.drawable.pic1); myAdapter.setGridViewData(gridViewData); Toast.makeText(MainActivity.this, "加载更多数据!", 0).show(); } }, 3000); }/*** * lsitView的适配器* * @author lyy* */public class MyAdapter extends BaseAdapter { Context context; private List
data; /*** * listview item position==2 * * GridView里面的数据 * */ private List
gridViewData; private GridViewAdapter gridViewAdapter; /** ListView 的数据 **/ public void setData(List
data) { this.data = data; this.notifyDataSetChanged(); } /*** gridView的数据 **/ public void setGridViewData(List
gridViewData) { this.gridViewData = gridViewData; this.notifyDataSetChanged(); } public MyAdapter(Context context) { this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return 2; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return data.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (position == 0) { return listView(); } else { return gridView(); } } public View listView() { View view = null; if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.item, null); } ImageView imageView1 = (ImageView) view.findViewById(R.id.imageView1); return view; } public View gridView() { View view = LayoutInflater.from(context).inflate(R.layout.item1, null); GridView myGridView = (GridView) view.findViewById(R.id.gridView1); if (gridViewAdapter == null) gridViewAdapter = new GridViewAdapter(context); gridViewAdapter.setData(gridViewData); myGridView.setAdapter(gridViewAdapter); myGridView.setOnItemClickListener(onItemClickListener); return view; } private OnItemClickListener onItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView
arg0, View arg1, int position, long arg3) { Toast.makeText(context, "你点击的是:" + position, 0).show(); } };}

ScrollView下载

 http://yunpan.cn/cAF8SgMWUAXrP  提取码 917c

原文链接:,转发请注明来源!

备份下载地址:

转载地址:http://iibgx.baihongyu.com/

你可能感兴趣的文章
【CodeForces 266C】Below the Diagonal(模拟)
查看>>
电容参数:X5R,X7R,Y5V,COG 详解
查看>>
[学习opencv]图像腐蚀、膨胀、开闭操作
查看>>
让editplus支持sql编辑
查看>>
[置顶] Android AlarmManager实现不间断轮询服务
查看>>
程序员搞笑故事:会哪几门语言?
查看>>
只用200行Go代码写一个自己的区块链!
查看>>
来谈谈云栖大会开源的顶级项目,开发者的福音!
查看>>
哈罗单车确认完成新一轮几十亿融资 春华资本与蚂蚁金服领投
查看>>
重庆构建互联互通新格局 从内陆腹地迈向开放前沿
查看>>
市场监管总局:把校园食品、保健食品作为监管重中之重
查看>>
成都动车段134组动车全面“体检”迎接春运
查看>>
韩国流行家中饮酒 2018年每家每月平均饮酒近6次
查看>>
隐藏黑钻数,修改前十榜单,网易星球给所有相信区块链的人一巴掌
查看>>
通过一个案例理解 JWT
查看>>
独家 | 日本机器学习领军人杉山将:为什么说弱监督学习是未来的热门?
查看>>
【火炉炼AI】机器学习020-使用K-means算法对数据进行聚类分析
查看>>
Python记一次自动脚本历程
查看>>
MVP+Kotlin源码体验
查看>>
makefile--伪目标语法与编程实例
查看>>