Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android新控件RecyclerView淺析及上拉和下拉刷新

Android新控件RecyclerView淺析及上拉和下拉刷新

編輯:關於Android編程

概述:

RecyclerView是android-support-v7-21版本中新增的一個Widgets,RecyclerView是ListView的升級版本,更加先進和靈活。在以後的開發中我們就可以直接使用RecyclerView來替換ListView。

 

特點介紹:

1. 可橫向展示

2. 消除  錯位問題

3. 標准化了ViewHolder

 

橫向:

 

private void initHorizaontal(List 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 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 {

	private List mList = null;
	
	private Context mContext = null;
	
	private LayoutInflater mInflater = null;
	
	public RecyclerViewAdapter(Context context, List 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上。

 

布局:

 



    

 

 

監聽:

 

@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();
            }

        });

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved