RecyclerView的出现是为了取代ListView和GirdView,因为其可以实现ListView和GirdView的效果,也可以拓展很多效果,而且RecyclerView也做了很多优化。
其使用方法是先通过设置布局管理器来确定布局,然后在通过适配器来将数据和试图进行绑定。这里我举个类似于瀑布流的小例子。
项目地址:http://download.csdn.net/detail/l_vaule/9518356
package com.example.recycleviewtest; /* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; /** * This class is from the v7 samples of the Android SDK. It's not by me! * <p/> * See the license above for details. */ public class DividerItemDecoration extends RecyclerView.ItemDecoration { private static final int[] ATTRS = new int[] { android.R.attr.listDivider }; public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; private Drawable mDivider; private int mOrientation; public DividerItemDecoration(Context context, int orientation) { final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); a.recycle(); setOrientation(orientation); } public void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientation"); } mOrientation = orientation; } @Override public void onDraw(Canvas c, RecyclerView parent) { Log.v("tz", "onDraw()"); if (mOrientation == VERTICAL_LIST) { drawVertical(c, parent); } else { drawHorizontal(c, parent); } } public void drawVertical(Canvas c, RecyclerView parent) { final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); RecyclerView v = new RecyclerView( parent.getContext()); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } public void drawHorizontal(Canvas c, RecyclerView parent) { final int top = parent.getPaddingTop(); final int bottom = parent.getHeight() - parent.getPaddingBottom(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int left = child.getRight() + params.rightMargin; final int right = left + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } /** * 为我们的Item预留空间 因为我们的recyclerView默认item间是不留空间的 * @param outRect * @param itemPosition * @param parent */ @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { if (mOrientation == VERTICAL_LIST) { outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); } else { outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); } } }
package com.example.recycleviewtest; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity { private RecyclerView recyclerView; private List<String> mData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); initData(); initView(); } private void initView() { //1,设置线性布局容器 recyclerView.setLayoutManager(new LinearLayoutManager(this)); //2,设置gridView布局容器 // recyclerView.setLayoutManager(new GridLayoutManager(this,4)); recyclerView.setAdapter(new MyAdapter(mData, this)); //3,添加分割线 gitHub DividerItemDecoration recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); } private void initData() { mData = new ArrayList<>(); for (int i = 0; i < 10; i++) { mData.add("I'm handsome"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { recyclerView.removeAllViews(); recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)); recyclerView.setAdapter(new StaggerAdapter(this, mData)); //添加条目或者删除条目的动画 ----- 系统已经提供了默认的动画 我们也可以自己写 gitHub recyclerView.setItemAnimator(new DefaultItemAnimator()); return true; } return super.onOptionsItemSelected(item); } }
package com.example.recycleviewtest; import java.util.List; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * @author ctt: * @version 创建时间:2016年5月12日 * 类说明 */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ private List<String> mData; private Context context; public MyAdapter(List<String> mData, Context context) { super(); this.mData = mData; this.context = context; } @Override public int getItemCount() { // TODO Auto-generated method stub return mData.size(); } @Override public void onBindViewHolder(ViewHolder arg0, int arg1) { arg0.textView.setText(mData.get(arg1)); } @Override public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) { View view = LayoutInflater.from(context).inflate(R.layout.layout_item, arg0,false); return new ViewHolder(view); } class ViewHolder extends RecyclerView.ViewHolder{ TextView textView; public ViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.tv); } } }
package com.example.recycleviewtest; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * @author ctt: * @version 创建时间:2016年5月12日 * 类说明 */ public class StaggerAdapter extends RecyclerView.Adapter<StaggerAdapter.StgViewHolder>{ private List<String> mDatas; private LayoutInflater inflater; private List<Integer> mHeights; public StaggerAdapter(Context context,List<String> mDatas){ this.mDatas = mDatas; this.inflater = LayoutInflater.from(context); mHeights = new ArrayList<Integer>(); //随机为每一个item设置高度 for(int i=0;i<mDatas.size();i++){ mHeights.add((int)(100+Math.random()*300)); } } @Override public StaggerAdapter.StgViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.layout_item,parent,false); return new StgViewHolder(view); } @Override public void onBindViewHolder(StgViewHolder arg0, int arg1) { //设置每一个item随机高度 ViewGroup.LayoutParams params = arg0.textView.getLayoutParams(); params.height = mHeights.get(arg1); arg0.textView.setLayoutParams(params); arg0.textView.setText(mDatas.get(arg1)); } @Override public int getItemCount() { return mDatas.size(); } /** * listView :ViewHolder * * 条目的布局:base_item */ class StgViewHolder extends RecyclerView.ViewHolder{ TextView textView; public StgViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.tv); } } }