zoukankan      html  css  js  c++  java
  • Android新控件RecyclerView浅析及上拉和下拉刷新

    概述:

    RecyclerView是android-support-v7-21版本中新增的一个Widgets,RecyclerView是ListView的升级版本,更加先进和灵活。在以后的开发中我们就可以直接使用RecyclerView来替换ListView。


    特点介绍:

    1. 可横向展示

    2. 消除错位问题

    3. 标准化了ViewHolder


    横向:

    private void initHorizaontal(List<ItemModel> models) {
    		RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_main_horizontal_recyclerview);
    
    		LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
    		layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    		recyclerView.setLayoutManager(layoutManager);
    
    		RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainActivity.this, models);
    		recyclerView.setAdapter(adapter);
    	}

    竖向:

    public void initVertical(List<ItemModel> models) {
    		RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_main_vertical_recyclerview);
    
    		LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
    		layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    		recyclerView.setLayoutManager(layoutManager);
    
    		RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainActivity.this, models);
    		recyclerView.setAdapter(adapter);
    	}

    Adapter的变化:

    Google在对RecyclerView的改进中还为我们解决了一个初学者比较头痛的问题:错位。

    public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {
    
    	private List<ItemModel> mList = null;
    	
    	private Context mContext = null;
    	
    	private LayoutInflater mInflater = null;
    	
    	public RecyclerViewAdapter(Context context, List<ItemModel> list) {
    		mContext = context;
    		mList = list;
    		mInflater = LayoutInflater.from(mContext);
    	}
    	
    	@Override
    	public int getItemCount() {
    		if (mList != null) {
    			return mList.size();
    		}
    		return 0;
    	}
    
    	@Override
    	public void onBindViewHolder(ViewHolder viewHolder, int position) {
    		((ItemViewHolder)viewHolder).mLabelTextView.setText(mList.get(position).getLabel());
    	}
    
    	@Override
    	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
    		View view = mInflater.inflate(R.layout.list_item, viewGroup, false);
    		ItemViewHolder holder = new ItemViewHolder(view);
    		
    		holder.mLabelTextView = (TextView) view.findViewById(R.id.list_item_textview);
    		
    		return holder;
    	}
    
    	public static class ItemViewHolder extends ViewHolder{
    
            public ItemViewHolder(View itemView) {
                super(itemView);
            }
            
            private TextView mLabelTextView = null;
        }
    }


    上拉或下拉刷新:

    对于下拉刷新,Android自身有一个控件SwipeRefreshLayout已经有所实现。我们把SwipeRefreshLayout包在RecyclerView的外面,然后再设置其绑定到一个OnRefreshListener上。


    布局:

    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/swipe_refresh_widget"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <android.support.v7.widget.RecyclerView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="@null"
            android:scrollbars="vertical" />
    
    </android.support.v4.widget.SwipeRefreshLayout>


    监听:

    @Override
        public void onRefresh() {
            handler.sendEmptyMessageDelayed(0, 3000);
        }

    而对于上拉刷新,我们则是自定义一个FooterView和线程来进行的。

    mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
    
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView,
                        int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                        handler.sendEmptyMessageDelayed(1, 3000);
                    }
                }
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
                }
    
            });


    相关源码下载:

    RecyclerView初步使用

    RecyclerView的上拉和下拉刷新


  • 相关阅读:
    VB.NET程序设计笔记
    ASP.NET动态的网页增删查改
    一般处理程序加简单三层实现增删查改(2)
    一般处理程序加简单三层实现增删查改(1)
    设计模式六大原则(1):单一职责原则
    Android Https相关完全解析 当OkHttp遇到Https
    Androrid应用打包release版时关闭log日志输出
    Fragment结合ViewPager之懒加载
    多线程基础
    Android Studio wifi调试
  • 原文地址:https://www.cnblogs.com/fengju/p/6336091.html
Copyright © 2011-2022 走看看