//Main
public class MainActivity extends AppCompatActivity {
private int[] imgId = new int[]{
R.drawable.aaaa,
R.drawable.abc,
R.drawable.b,
R.drawable.c
};
private RecyclerView rv;
private int[] imgId = new int[]{
R.drawable.aaaa,
R.drawable.abc,
R.drawable.b,
R.drawable.c
};
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//RcyclerView找到控件,设置适配器
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) {
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);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//RcyclerView找到控件,设置适配器
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) {
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;
}
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);
}
}
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
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private ArrayList<User> list;
private MyItemClick myItemClick;//监听器
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) {
//调用接口里面的方法
myItemClick.itemClick(holder.itemView,position);
}
});
}
}
//条目数量
@Override
public int getItemCount() {
return list.size();
}
//定义接口,实现条目点击事件
public interface MyItemClick{
void itemClick(View view,int postion);
}
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) {
//调用接口里面的方法
myItemClick.itemClick(holder.itemView,position);
}
});
}
}
//条目数量
@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);//参数,位置,在那里插入了数据
}
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 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;
}
return imageView;
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
this.imageView = imageView;
}
public TextView getTextView() {
return textView;
}
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
this.textView = textView;
}
}
//User
public class User {
private int imageId;
private String content;
public class User {
private int imageId;
private String content;
public User(int imageId, String content) {
this.imageId = imageId;
this.content = content;
}
this.imageId = imageId;
this.content = content;
}
public User() {
}
}
public int getImageId() {
return imageId;
}
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
this.imageId = imageId;
}
public String getContent() {
return content;
}
return content;
}
public void setContent(String content) {
this.content = content;
}
}
this.content = content;
}
}
//item布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:scaleType="centerCrop"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aaaa"/>
android:scaleType="centerCrop"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aaaa"/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center"
android:text="测试"
android:textSize="30sp"/>
</LinearLayout>
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center"
android:text="测试"
android:textSize="30sp"/>
</LinearLayout>
//main布局LinearLayout
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>