RecyclerView在android5.0之后被引进,可以通过导入support-v7对其进行使用。 该控件用于在有限的窗口中展示大量数据集。提供了一种插拔式的体验,高度的解耦,异常的灵活,通过LayoutManager,ItemDecoration , ItemAnimator可以实现多样的效果。
效果图:
实例一:
public class RecyclerActivity01 extends AppCompatActivity { private RecyclerView mRecyclerView; private List<String> mDatas; private BasicAdapter baseAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler01); mRecyclerView = (RecyclerView) findViewById(R.id.rv); initData(); baseAdapter = new BasicAdapter(mDatas, RecyclerActivity01.this); mRecyclerView.setAdapter(baseAdapter); //声明布局管理器,设置最终的展示效果 LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayout.VERTICAL, false); mRecyclerView.setLayoutManager(manager); //设置增加条目的动画 mRecyclerView.setItemAnimator(new DefaultItemAnimator()); baseAdapter.setOnItemClickListener(new BasicAdapter.OnItemClickListener() { @Override public void onItemClick(View itemView, int position) { Toast.makeText(RecyclerActivity01.this,"你点击了:"+mDatas.get(position),Toast.LENGTH_SHORT).show(); } @Override public void onLongItemClick(View itemView, int position) { } }); } private void initData() { mDatas = new ArrayList<>(); for (int i = 0; i < 48; i++) { mDatas.add("第" + i + "条数据"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add: baseAdapter.addData(1); break; case R.id.delete: baseAdapter.removeData(1); break; case R.id.diplay_lv: LinearLayoutManager manger = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); mRecyclerView.setLayoutManager(manger); break; case R.id.diplay_gv: //第二个参数:定义列数 GridLayoutManager manager = new GridLayoutManager(this, 3); mRecyclerView.setLayoutManager(manager); break; case R.id.diplay_hlv: StaggeredGridLayoutManager sManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(sManager); break; case R.id.diplay_hgv: sManager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(sManager); break; case R.id.diplay_staggered: Intent intent = new Intent(this, StaggeredActivity.class); startActivity(intent); break; } return super.onOptionsItemSelected(item); } }
<android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"/>
public class BasicAdapter extends RecyclerView.Adapter<BasicAdapter.BasicViewHolder> { private List<String> mdatas; private Context context; private OnItemClickListener listener; public BasicAdapter(List<String> mdatas, Context context) { this.mdatas = mdatas; this.context = context; } /** * des:添加监听 */ public void setOnItemClickListener(OnItemClickListener listener) { this.listener = listener; } public interface OnItemClickListener { void onItemClick(View itemView, int position); void onLongItemClick(View itemView, int position); } @Override public BasicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_recycler01, parent, false); BasicViewHolder holder = new BasicViewHolder(view); return holder; } public void addData(int pos) { mdatas.add(pos, "add_data"); notifyItemInserted(pos); } public void removeData(int pos) { mdatas.remove(pos); notifyItemRemoved(pos); } @Override public void onBindViewHolder(BasicViewHolder holder, int position) { holder.tv.setText(mdatas.get(position)); setUpEvent(holder); } protected void setUpEvent(final BasicViewHolder holder) { if (listener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getLayoutPosition(); listener.onItemClick(holder.itemView, position); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int position = holder.getLayoutPosition(); listener.onLongItemClick(holder.itemView, position); return false; } }); } } @Override public int getItemCount() { return mdatas.size(); } class BasicViewHolder extends RecyclerView.ViewHolder { TextView tv; public BasicViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.id_tv1); } } }
效果图:
实例二:实现照片墙,宽度一定,高度不确定
public class PhoneWallActivity extends AppCompatActivity { private RecyclerView phrv; private PhoneWalladapter walladapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone_wall); phrv = (RecyclerView) findViewById(R.id.pwrv); walladapter = new PhoneWalladapter(this); phrv.setAdapter(walladapter); StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2 ,StaggeredGridLayoutManager.VERTICAL); phrv.setLayoutManager(manager); } }
<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pwrv"/>
public class PhoneWalladapter extends RecyclerView.Adapter<PhoneWalladapter.PhotoViewHolder> { private Context context; private String imgUrls[]; private List<Integer>mHeight; public PhoneWalladapter(Context context) { this.context = context; imgUrls = ImageUrls.imageurls; mHeight = new ArrayList<>(); for (int i = 0; i < imgUrls.length; i++) { mHeight.add((int)(300+Math.random()*400)); } } @Override public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(context).inflate(R.layout.item_phone,parent,false); PhotoViewHolder holder = new PhotoViewHolder(itemView); return holder; } @Override public void onBindViewHolder(PhotoViewHolder holder, int position) { ViewGroup.LayoutParams lp = holder.iv.getLayoutParams(); lp.height = mHeight.get(position); holder.iv.setLayoutParams(lp); ImageOptions options = new ImageOptions.Builder().setFailureDrawableId(R.mipmap.ic_launcher) .setLoadingDrawableId(R.mipmap.ic_launcher).build(); x.image().bind(holder.iv,imgUrls[position],options); } @Override public int getItemCount() { return imgUrls.length; } class PhotoViewHolder extends RecyclerView.ViewHolder{ ImageView iv; public PhotoViewHolder(View itemView) { super(itemView); iv = (ImageView)itemView.findViewById(R.id.item_iv); } } }效果图:
实例三:实现下载刷新上拉加载效果:
public class RecyclerActivity04 extends AppCompatActivity { private RecyclerView mRecyclerView; private SwipeRefreshLayout swipeRefreshLayout; private List<Map<String,String>>mDatas; private LinearLayoutManager manager; private RecyclerAdapter4 adapter; private Handler handler = new Handler(); private boolean isLoading = false; //设置是否处于上啦加载状态 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler04); initView(); setEvent(); } private void initView(){ mRecyclerView = (RecyclerView) findViewById(R.id.id_rv); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl); swipeRefreshLayout.setColorSchemeColors(Color.RED,Color.GREEN); manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); mRecyclerView.setLayoutManager(manager); mDatas = new ArrayList<>(); getData(); adapter = new RecyclerAdapter4(this,mDatas); mRecyclerView.setAdapter(adapter); } private void setEvent(){ //设置下拉刷新的操作 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { mDatas.clear(); getData(); adapter.notifyDataSetChanged(); swipeRefreshLayout.setRefreshing(false); } },3000); } }); //设置上啦加载的监听器 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); //获得可见条目最后一条的位置 int lastVisibleItemPosition = manager.findLastVisibleItemPosition(); if (lastVisibleItemPosition+1==adapter.getItemCount()){ //获取是否在下拉刷新 boolean isRefresh = swipeRefreshLayout.isRefreshing(); if (isRefresh){ return; } if (!isLoading){ isLoading = true; //没有处于加载状态,然后需要加载数据,就可以分页加载了 handler.postDelayed(new Runnable() { @Override public void run() { getData(); adapter.notifyDataSetChanged(); adapter.notifyItemRemoved(adapter.getItemCount()); isLoading = false; } },4000); } } } }); } /** * 加载数据 * */ private void getData(){ int size = mDatas.size(); for (int i = 0; i <20 ; i++) { Map<String,String>map = new HashMap<>(); map.put("title","title======"+(i+size)); map.put("summary","summary--------"+(i+size)); mDatas.add(map); } } }
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/srl" android:layout_height="match_parent" android:layout_width="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/id_rv" android:layout_height="match_parent" android:layout_width="match_parent"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
public class RecyclerAdapter4 extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private Context context; private List<Map<String,String>>mDatas; private final int TYPE_COMMON = 0; private final int TYPE_FOOTER = 1; public RecyclerAdapter4(Context context, List<Map<String, String>> mDatas) { this.context = context; this.mDatas = mDatas; } /** * 获得指定的位置上的显示类型 * */ @Override public int getItemViewType(int position) { if (position==getItemCount()-1){ return TYPE_FOOTER; } return TYPE_COMMON; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_COMMON) { View itemView = LayoutInflater.from(context).inflate(R.layout.item_recycler04,parent,false); ItemViewHolder itemViewHolder = new ItemViewHolder(itemView); return itemViewHolder; }else { View footerView = LayoutInflater.from(context).inflate(R.layout.item_footer,parent,false); FooterViewHolder holder = new FooterViewHolder(footerView); return holder; } } @Override public int getItemCount() { return mDatas.size()==0?0:mDatas.size()+1; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof ItemViewHolder) { ((ItemViewHolder)holder).tv_name.setText(mDatas.get(position).get("title")); ((ItemViewHolder)holder).tv_summary.setText(mDatas.get(position).get("summary")); } } class ItemViewHolder extends RecyclerView.ViewHolder{ TextView tv_name,tv_summary; public ItemViewHolder(View itemView) { super(itemView); tv_name = (TextView)itemView.findViewById(R.id.tv_name); tv_summary = (TextView)itemView.findViewById(R.id.tv_summary); } } class FooterViewHolder extends RecyclerView.ViewHolder{ public FooterViewHolder(View itemView) { super(itemView); } } }效果:
实例四;CardView的效果
<android.support.v7.widget.CardView android:id="@+id/id_card" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardBackgroundColor="#669900" app:cardCornerRadius="10dp" app:cardElevation="20dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:layout_centerVertical="true" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="match_parent" android:layout_height="70dp" android:layout_margin="10dp" android:gravity="center" android:padding="10dp" android:text="显示效果" android:textColor="#000000" android:textSize="20sp" /> </RelativeLayout> </android.support.v7.widget.CardView>
作者:qq_29882585 发表于2016/10/22 11:47:18 原文链接
阅读:62 评论:0 查看评论