小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

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的上拉和下拉刷新


posted on 2015-05-18 15:10  王峰炬  阅读(154)  评论(0编辑  收藏  举报

导航