MainActivity
package com.example.cjy.recyclerviewdemo;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
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.View;
import android.widget.Button;
import java.util.ArrayList;
public class RecyclerViewActivity extends Activity implements View.OnClickListener {
private Button btn_add;
private Button btn_delete;
private Button btn_list;
private Button btn_grid;
private Button btn_flow;
private RecyclerView recyclerView;
private ArrayList<String> datas;
private MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//准备数据集合
datas = new ArrayList<>();
//准备数据集合
for (int i= 0;i<100;i++){
datas.add("Content_"+i);
}
//设置recyclerView适配器
adapter = new MyRecyclerViewAdapter(RecyclerViewActivity.this,datas);
recyclerView.setAdapter(adapter);
//layoutManager
recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false));
// recyclerView.scrollToPosition(datas.size()-1);
//设置点击某条的监听
}
private void initView() {
btn_add = (Button)findViewById(R.id.btn_add);
btn_delete = (Button)findViewById(R.id.btn_delete);
btn_list = (Button)findViewById(R.id.btn_list);
btn_grid = (Button)findViewById(R.id.btn_grid);
btn_flow = (Button)findViewById(R.id.btn_flow);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//设置点击事件
btn_add.setOnClickListener(this);
btn_delete.setOnClickListener(this);
btn_list.setOnClickListener(this);
btn_grid.setOnClickListener(this);
btn_flow.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_add:
adapter.adddata(0,"New_content");
break;
case R.id.btn_delete:
adapter.removedata(0);
break;
//设置 listView 运行效果
case R.id.btn_list:
recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false));
break;
//设置 gridView 运行效果
case R.id.btn_grid:
recyclerView.setLayoutManager(new GridLayoutManager(
RecyclerViewActivity.this,3,
GridLayoutManager.VERTICAL,false));
break;
case R.id.btn_flow:
//设置瀑布流运行效果
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(
2, StaggeredGridLayoutManager.VERTICAL));
break;
default:
break;
}
}
}
MyRecyclerViewAdapter
package com.example.cjy.recyclerviewdemo;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by cjy on 2017/1/6.
*/
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
private final Context context;
private ArrayList<String> datas;
public MyRecyclerViewAdapter(Context context, ArrayList<String> datas) {
this.context = context;
this.datas = datas;
}
//相当于 getView 方法中创建View和ViewHolder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView =View.inflate(context,R.layout.item_recyclerview,null);
return new MyViewHolder(itemView);
}
//数据和View绑定
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//根据位置得到对应的数据
String data = datas.get(position);
holder.tv_title.setText(data);
}
//得到总条数
@Override
public int getItemCount() {
return datas.size();
}
//添加数据
public void adddata(int i, String new_content) {
datas.add(i,new_content);
//刷新适配器
notifyItemInserted(i);
}
public void removedata(int i) {
datas.remove(i);
//刷新适配器
notifyItemRemoved(i);
}
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView iv_icon;
private TextView tv_title;
public MyViewHolder(View itemView) {
super(itemView);
iv_icon = (ImageView)itemView.findViewById(R.id.ic_icon);
tv_title = (TextView)itemView.findViewById(R.id.tv_title);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context,"data=="+datas.get(getLayoutPosition()),Toast.LENGTH_SHORT).show();
}
});
}
}
//点击RecyclerView 某条监听
public interface onItemClickListener{
//当点
public void onItemClick(View view,String data);
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".RecyclerViewActivity">
<TextView
android:background="#2fbbea"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="RecyclerView"
android:gravity="center"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:textAllCaps="false"
android:text="添加"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_delete"
android:textAllCaps="false"
android:text="删除"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_list"
android:textAllCaps="false"
android:text="List"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_grid"
android:textAllCaps="false"
android:text="Grid"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_flow"
android:textAllCaps="false"
android:text="Flow"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>e
</LinearLayout>
item_recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="5dp"
android:gravity="center"
android:background="#22000000"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ic_icon"
android:background="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_marginLeft="3dp"
android:id="@+id/tv_title"
android:text="Context"
android:textColor="#000000"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>