上拉刷新列表(自定义列表项布局)的实现
2015-04-20 20:06
274 查看
listView是应用非常广泛的一个控件,当我们要用ListView来自定义列表项,并且从服务器下载数据更新到ListView时,我们通常会新开一个线程进行异步下载,效果图如下:
实现的代码如下:
对应的布局文件 layout_my_card 如下
下面是列表项的布局
实现的代码如下:
/** * 本类用来显示Vip卡的信息 */ public class ActyMyCard extends Activity implements AbsListView.OnScrollListener{ private ListView listview; private Thread mThread; LinearLayout loadingLayout; //lsitview的页脚布局 private ListViewAdapter adapter ; private ProgressBar progressBar; private ImageButton back; /** * 设置布局显示属性 */ private LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); /** * 设置布局显示目标最大化属性 */ private LinearLayout.LayoutParams FFlayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_my_card); init(); } private void init() { adapter = new ListViewAdapter(ActyMyCard.this, getData());//设置适配器 listview = (ListView) findViewById(R.id.list_card); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.HORIZONTAL); progressBar = new ProgressBar(this); progressBar.setPadding(0,0,15,0); layout.addView(progressBar, mLayoutParams); TextView tv = new TextView(this); tv.setText("加载中..."); tv.setGravity(Gravity.CENTER_VERTICAL); layout.addView(tv, FFlayoutParams); layout.setGravity(Gravity.CENTER); //设置listview页脚layout loadingLayout = new LinearLayout(this); loadingLayout.addView(layout,mLayoutParams); loadingLayout.setGravity(Gravity.CENTER); listview.addFooterView(loadingLayout); listview.setAdapter(adapter); //设置适配器 listview.setOnScrollListener(this); back = (ImageButton) findViewById(R.id.back_card); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if(firstVisibleItem+visibleItemCount == totalItemCount){ //开线程下载网络数据 if(mThread == null || !mThread.isAlive()){ mThread = new Thread(){ @Override public void run() { //TODO: 这里请求服务器获取卡包信息,然后将信息封装成list,卡包信息封装在类CardMsg中 try{ Thread.sleep(4000); }catch (InterruptedException e ){ e.printStackTrace(); } Message message = new Message(); message.what = Config.REFLASHLISTVIEW; handler.sendMessage(message); } }; mThread.start(); } } } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what){ case Config.REFLASHLISTVIEW: if(adapter.count <=100){ adapter.count+=10; int currentPage = adapter.count / 10; //TODO:这里是把上面封装的list传入adapter adapter.addList(getData()); Toast.makeText(getApplicationContext(), "第" + currentPage + "页", Toast.LENGTH_LONG).show(); } else { listview.removeFooterView(loadingLayout); } adapter.notifyDataSetChanged(); break; default: break; } } }; /* 这个方法是测试用的 */ public List<Map<String,Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for(int i = 0;i< 10;i++){ Map<String,Object> map = new HashMap<String,Object>(); map.put("cardmsg",new CardMsg("name","location",i)); list.add(map); } return list; } } class ListViewAdapter extends BaseAdapter { int count = 10; LayoutInflater listContainer; private List<Map<String, Object>> listItems; private Context context; public final class ListItemView { //自定义控件集合,即列表项的内容控件 public TextView companyName, location; public ImageView cardImage; } public ListViewAdapter(Context context,List<Map<String, Object>> listItems){ this.context = context; this.listContainer = LayoutInflater.from(context); this.listItems = listItems; } /* 向listItems中添加新的数据 */ public void addList(List<Map<String,Object>> newList) { listItems.addAll(newList); } @Override public int getCount(){ return listItems.size(); } @Override public Object getItem(int pos) { return listItems.get(pos); } @Override public long getItemId(int position) { return position; } /* * 自宝义列表项时,最重要的就是实现getView方法 */ @Override public View getView(int position, View v, ViewGroup parent) { ListItemView listItemView = null; if(v==null){ listItemView = new ListItemView(); v = listContainer.inflate(R.layout.list_cell_my_card,null); //获取控件对象 listItemView.companyName = (TextView) v.findViewById(R.id.com_location_list_cus); listItemView.location = (TextView) v.findViewById(R.id.com_location_list_cus); listItemView.cardImage = (ImageView) v.findViewById(R.id.card_image_list_cus); v.setTag(listItemView); } else { listItemView = (ListItemView) v.getTag(); } //TODO: 获取数据,将listItems里的CardMsg更新到控件中,目前这里是用了三个控件 listItemView.companyName.setText(((CardMsg) listItems.get(position).get("cardmsg")).getCompanyName()+position); listItemView.location.setText(((CardMsg) listItems.get(position).get("cardmsg")).getLocation()+position); return v; } }
对应的布局文件 layout_my_card 如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:gravity="center_vertical" android:background="#fff" android:orientation="horizontal"> <ImageButton android:id="@+id/back_card" android:layout_width="@dimen/back_btn_width" android:layout_height="@dimen/back_btn_width" android:layout_marginLeft="@dimen/padding_left_custom" android:background="@drawable/back" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="我的卡包" android:layout_weight="1" android:textSize="@dimen/big_text_size" android:gravity="center" /> <ImageView android:layout_width="30dp" android:layout_marginRight="@dimen/padding_left_custom" android:layout_height="wrap_content" android:background="@android:color/transparent" /> </LinearLayout> <ListView android:id="@+id/list_card" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
下面是列表项的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="80dp" > <ImageView android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/default_img" android:id="@+id/card_image_list_cus" /> <TextView android:id="@+id/com_name_list_cus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商家名字" android:textSize="@dimen/textsize_25_sp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/card_image_list_cus" android:layout_toEndOf="@+id/card_image_list_cus" /> <TextView android:id="@+id/com_location_list_cus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="@dimen/padding_left_custom" android:text="店铺位置" android:textSize="@dimen/textsize_25_sp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> </LinearLayout>
相关文章推荐
- 2014-10-27Android学习------布局处理(八)------自定义ListView的监听事件和Adapter的实现-----城市列表应用程序
- Android自定义listview布局实现上拉加载下拉刷新功能
- 使用自定义的RelateLayout实现列表布局
- Android -- 自定义实现横竖双向滚动的列表(ListView)布局
- Xamarin自定义布局系列——ListView的一个自定义实现ItemsControl(横向列表)
- 安卓开发中实现页面越界回弹和上下拉的刷新自定义动画的操作(TwinklingRefreshLayout布局的使用)
- 自定义ListView适配器Adapter引用布局文件的情况下实现点击列表项时背景颜色为灰色
- RecyclerView条目跳转+SpringView数据刷新加载+MVP+OKhttp+拦截器+自定义view 实现请求网络数据的二级列表购物车
- 自定义上拉加载更多的ListView配合SwipeRefreshLayout实现列表刷新加载
- 自定义viewGroup 实现 流式布局
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- Dialog自定义布局的实现
- android应用开发-从设计到实现 4-9 天气指数列表的布局
- Android 自定义ListView实现底部分页刷新与顶部下拉刷新 .
- iOS - UICollectionView 自定义布局之风火轮[译] (原版为swift,我这里的实现改为OC)
- Android自定义布局实现仿qq侧滑部分代码
- ActionBar(4) 自定义布局及下拉列表导航
- Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面
- salesforce 零基础学习(四十九)自定义列表分页之使用Pagination实现分页效果 ※※※
- 完全自定义dialog布局的实现