介绍:
1.RecylerView是 support-v7 中的一个控件
2.该控件用于在有限的窗口中展示大量数据集, 例如: ListView、 GridView。
RecyclerView架构, 提供了一种插拔式的体验, 高度的解耦, 异常的灵活, 通过设置它提供的不同
LayoutManager, ItemDecoration , ItemAnimator实现令人瞠目的效果。
1>你想要控制其显示的方式, 请通过布局管理器LayoutManager
2>你想要控制Item间的间隔( 可绘制) , 请通过ItemDecoration
3>你想要控制Item增删的动画, 请通过ItemAnimator
4>事件得自己写, 官方没有响应的事件
还用到了菜单栏,就像微信添加好友那块儿的菜单栏
先在build.gradle(第二个)导入依赖
compile ‘com.android.support:recyclerview-v7:26.0.0-alpha1’
//如果依赖的版本是26,而你的版本不是26的,可以试着改为26
接下来是布局
activity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ass.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:scaleType="centerCrop"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a18"/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center"
android:text="测试"
android:textSize="30dp"/>
</LinearLayout>
右上角菜单栏的布局
在菜单栏可以选择展示ListView,GridView,和瀑布流形式
menu_title.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/one"
android:title="ListView"
app:showAsAction="never"></item>
<item
android:id="@+id/two"
android:title="GridView"
app:showAsAction="never"></item>
<item
android:id="@+id/three"
android:title="瀑布流"
app:showAsAction="never"></item>
</menu>
MainActivity
package com.example.ass.myapplication;
import android.app.Activity;
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.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity {
private int[] imgId=new int[]{
R.drawable.a18,
R.drawable.axly,
R.drawable.cat,
R.drawable.nhs,
R.drawable.yyjdf
};
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
rv=(RecyclerView) findViewById(R.id.recyclerView);
ArrayList<User> list = new ArrayList<>();
//手动添加数据
for (int i=0;i<100;i++){
String conent = i + "林志玲";
int img=imgId[i%imgId.length];
User user = new User(img, conent);
list.add(user);
}
//设置布局管理器
rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
//设置动画
rv.setItemAnimator(new DefaultItemAnimator());
//设置适配器
final MyAdapter adapter = new MyAdapter(MainActivity.this, list);
rv.setAdapter(adapter);
//条目的点击事件
adapter.setOnMyItemClickListener(new MyAdapter.MyItemClick() {
@Override
public void itemClick(View view, int postion) {
//吐司点击的条目的id
Toast.makeText(MainActivity.this,"---"+postion,Toast.LENGTH_SHORT).show();
User user = new User(R.mipmap.ic_launcher, "hello 机器人");
//增加条目
//adapter.addUser(user,postion);
//删除条目
adapter.removeUser(postion);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.item,menu);
return true;
}
//菜单栏
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId=item.getItemId();
switch (itemId) {
case R.id.one:
//listView布局
rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
break;
case R.id.two:
//gridview布局
rv.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
break;
case R.id.three:
//流失布局
rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
break;
}
return super.onOptionsItemSelected(item);
}
}
MyAdapter
package com.example.ass.myapplication;
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.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private ArrayList<User> list;
private MyItemClick myItemClick;//监听器
public MyAdapter(Context context, ArrayList<User> list) {
this.context=context;
this.list=list;
}
//创建ViewHolder的时候
//实例化ViewHolder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//条目布局
View view = LayoutInflater.from(context).inflate(R.layout.item, null);
//实例化ViewHolder ,将布局传入
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
//当绑定ViewHolder的时候
//给控件设置数据
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.getTextView().setText(list.get(position).getContent());
holder.getImageView().setImageResource(list.get(position).getImageId());
//条目点击事件
if(myItemClick!=null){
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i=holder.getLayoutPosition();
//调用接口里面的方法
myItemClick.itemClick(holder.itemView,i);
}
});
}
}
//条目数量
@Override
public int getItemCount() {
return list.size();
}
//定义接口,实现条目点击事件
public interface MyItemClick {
void itemClick(View view,int postion);
}
public void setOnMyItemClickListener(MyItemClick myItemClick){
this.myItemClick = myItemClick;
}
//添加数据
public void addUser(User user,int position){
list.add(position,user);
//更新适配器
notifyItemInserted(position);
}
//删除数据
public void removeUser(int position){
list.remove(position);
//更新适配器
notifyItemRemoved(position);
}
//ViewHolder
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
private TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
this.imageView=itemView.findViewById(R.id.image);
this.textView=itemView.findViewById(R.id.content);
}
//getter setter方法
public ImageView getImageView(){
return imageView;
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
}
Bean类User
package com.example.ass.myapplication;
public class User {
private int imageId;
private String content;
public User() {
}
public User(int imageId, String content) {
this.imageId = imageId;
this.content = content;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}