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<Integer> data; private List<Integer> 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<Integer>(); gridViewData = new ArrayList<Integer>(); 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<Integer> data; /*** * listview item position==2 * * GridView里面的数据 * */ private List<Integer> gridViewData; private GridViewAdapter gridViewAdapter; /** ListView 的数据 **/ public void setData(List<Integer> data) { this.data = data; this.notifyDataSetChanged(); } /*** gridView的数据 **/ public void setGridViewData(List<Integer> 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
原文链接:ScrollView嵌套ListView嵌套GridView的上下拉以及加载更多,转发请注明来源!
备份下载地址:http://download.csdn.net/detail/jdsjlzx/9290193
时间: 2024-09-14 12:35:18