标签:
周末看了Recylerview的使用,RecyclerView在android标准的sdk中并没有收录,如果使用的话需要导入android.support.v7.widget.RecyclerView 这个包,这个view当中包含了一个ViewHolder相当于listview的升级版本,它所解决的问题就是itemview的复用,它并不关心怎样去显示。这其实是完全脱耦合的一种架构思想,布局的显示方式可以通过 **recyclerView.setLayoutManager(LayoutManager)**来设置,备选的方案有很多,可以设置横向listview、纵向listview、以及横纵向的gridview方式,甚至支持瀑布流的方式显示。但是这个控件本身并没有分割线,需要开发者自己去手动添加 **recyclerView.addItemDecoration(dividerItemDecoration)**这个即是添加分割线。**recyclerView.setItemAnimator( new DefaultItemAnimator())**这个是添加默认动画的。可以看到这个控件除了提供一个viewHolder之外什么都没有,需要什么你就自己去添加什么。下面就贴代码了:
activity_simple_recycler.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mhyuam.reccler.SimpleRecyclerActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
layout_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp">
<TextView
android:id="@+id/item_tv"
android:gravity="center"
android:background="@drawable/item_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize0sp"
/>
</FrameLayout>
这里写代码片
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class SimpleRecyclerActivity extends ActionBarActivity {
RecyclerView recyclerView;
LinearLayoutManager horizontalManager;
LinearLayoutManager vercitalManager;
GridLayoutManager verticalGridManager;
GridLayoutManager horiziontalGridManager;
DividerItemDecoration dividerItemDecoration;
SimpleItemAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_recycler);
init();
}
private void init(){
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
horizontalManager = new LinearLayoutManager(SimpleRecyclerActivity.this,LinearLayoutManager.HORIZONTAL,false);
vercitalManager = new LinearLayoutManager(SimpleRecyclerActivity.this,LinearLayoutManager.VERTICAL,false);
verticalGridManager = new GridLayoutManager(SimpleRecyclerActivity.this,3,GridLayoutManager.VERTICAL,false);
horiziontalGridManager = new GridLayoutManager(SimpleRecyclerActivity.this,5,GridLayoutManager.HORIZONTAL,false);
List<String> datas = new ArrayList<String>();
for (int i = 0; i < 200; i++){
datas.add(i+"");
}
adapter = new SimpleItemAdapter(SimpleRecyclerActivity.this,datas);
adapter.setOnItemClickListener(new SimpleItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(SimpleRecyclerActivity.this,"onclick: "+position,Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(SimpleRecyclerActivity.this,"onlongclick: "+position,Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator( new DefaultItemAnimator());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return setSelectItemLayout(item);
}
private boolean setSelectItemLayout(MenuItem item){
switch (item.getItemId()){
case R.id.action_add:
adapter.addItem("one");
return true;
case R.id.action_delete:
adapter.deleteItem();
return true;
case R.id.action_horizontal:
recyclerView.setLayoutManager(horizontalManager);
return true;
case R.id.action_verictal:
recyclerView.setLayoutManager(vercitalManager);
return true;
case R.id.action_grid_hor:
recyclerView.setLayoutManager(horiziontalGridManager);
return true;
case R.id.action_grid_ver:
recyclerView.setLayoutManager(verticalGridManager);
return true;
case R.id.action_stroggle:
Intent intent = new Intent(this,WaterfallActivity.class);
startActivity(intent);
return true;
default:
return false;
}
}
}
SimpleItemAdapter
package com.example.mhyuam.reccler;
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;
import java.util.List;
/**
* Created by mhyuam on 2016/7/16.
*/
public class SimpleItemAdapter extends RecyclerView.Adapter<SimpleItemAdapter.MyHolder> {
private List<String> datas;
private LayoutInflater inflater;
private Context context;
private OnItemClickListener itemClickListener;
public SimpleItemAdapter(Context context, List list) {
datas = list;
this.context = context;
inflater = LayoutInflater.from(context);
}
public void setOnItemClickListener(OnItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.layout_item,parent,false);
MyHolder holder = new MyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyHolder holder, int position) {
holder.textView.setText(datas.get(position).toString());
if (itemClickListener != null){
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();
itemClickListener.onItemClick(holder.textView,pos);
}
});
holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int pos = holder.getLayoutPosition();
itemClickListener.onItemLongClick(holder.textView,pos);
return false;
}
});
}
}
public void addItem(String string){
datas.add(1,string);
notifyItemInserted(1);
notifyItemRemoved(1);
}
@Override
public int get }
public void deleteItem(){
datas.remove(1);
ItemCount() {
return datas.size();
}
public interface OnItemClickListener {
public void onItemClick(View view,int position);
public void onItemLongClick(View view,int position);
}
public static class MyHolder extends RecyclerView.ViewHolder{
TextView textView;
public MyHolder(ViewetemView) {
super(itemView);
textView = xtV ew) itemView.findViewById(R.id.item_tv);
}
}
}
DividerItemDe中复该属性,以达到定制分割线的目的,定制时同时注意放置style的values-xx,如果没有和该版本对应得values,默认是从上往下取值
package com.example.mhyuam.reccler;
/*
* 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("recyclerview - itemdecoration", "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();
mDivi`
der.setBounds(leftSright, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition,
RecyclerView parent)
{
`
if (mOrieT)
{
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else
{
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
下面是瀑布流的实现
package com.example.mhyuam.reccler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class WaterfallActivity extends ActionBarActivity {
RecyclerView recyclerView;
DividerItemDecoration dividerItemDecoration;//分割线
StaggeredGridLayoutManager staggeredGridLayoutManager;
WaterfallAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_recycler);
init();
}
private void init(){
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
List<String> datas = new ArrayList<String>();
for (int i = 0; i < 200; i++){
datas.add(i+"");
}
adapter = new WaterfallAdapter(WaterfallActivity.this,datas);
adapter.setOnItemClickListener(new WaterfallAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(WaterfallActivity.this,"onclick: "+position,Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(WaterfallActivity.this,"onlongclick: "+position,Toast.LENGTH_SHORT).show();
}
});
dividerItemDecoration = new DividerItemDecoration(WaterfallActivity.this,StaggeredGridLayoutManager.VERTICAL);
staggeredGridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
/**
* 设置分割线,分割线的样式有android.R.attr.listDivider决定,
* 可以在style中复写该属性,以达到定制分割线的目的
* 定制时同时注意放置style的values-xx,如果没有和该版本对应得values,默认是从上往下取值
*/
recyclerView.addItemDecoration(dividerItemDecoration);
/**
* 设置item变动时的动画
*/
recyclerView.setItemAnimator( new DefaultItemAnimator());
}
}
package com.example.mhyuam.reccler;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mhyuam on 2016/7/16.
*/
public class WaterfallAdapter extends RecyclerView.Adapter<WaterfallAdapter.MyHolder> {
private List<String> datas;
private LayoutInflater inflater;
private Context context;
private OnItemClickListener itemClickListener;
private List<Integer> height;
public WaterfallAdapter(Context context, List list) {
datas = list;
this.context = context;
inflater = LayoutInflater.from(context);
height = new ArrayList<Integer>();
for (int i = 0; i < datas.size();i++){
height.add((int)( 200 + Math.random()*200));
}
}
public void setOnItemClickListener(OnItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.layout_item,parent,false);
MyHolder holder = new MyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyHolder holder, int position) {
holder.textView.setText(datas.get(position).toString());
ViewGroup.LayoutParams lp = holder.textView.getLayoutParams();
lp.height = height.get(position);
holder.textView.setLayoutParams(lp);
if (itemClickListener != null){
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* pos取值时需要注意,由于在add和delete item时会引起位置信息的变动
* 这里一定要动态去获取当前item在整个布局中的位置
*/
int pos = holder.getLayoutPosition();
itemClickListener.onItemClick(holder.textView,pos);
}
});
holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int pos = holder.getLayoutPosition();
itemClickListener.onItemLongClick(holder.textView,pos);
return false;
}
});
}
}
@Override
public int getItemCount() {
return datas.size();
}
public interface OnItemClickListener {
public void onItemClick(View view,int position);
public void onItemLongClick(View view,int position);
}
public static class MyHolder extends RecyclerView.ViewHolder{
TextView textView;
public MyHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.item_tv);
}
}
}
标签:
原文地址:http://blog.csdn.net/android_daily/article/details/51946399