效果图
第一步:倒依赖:
implementation 'com.android.support:recyclerview-v7:27.1.1'
第二步:布局 :五个按钮一个RecycleView
<LinearLayout
android:orientation="vertical"
xmlns: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"
tools:context="com.example.rk1114.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="添加"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="删除"/>
<Button
android:id="@+id/btn_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="LIST"/>
<Button
android:id="@+id/btn_grid"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="GRID"/>
<Button
android:id="@+id/btn_flow"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="FLOW"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"></android.support.v7.widget.RecyclerView>
</LinearLayout>
第三步:逻辑Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnAdd;
private Button btnDelete;
private Button btnList;
private Button btnGrid;
private Button btnFlow;
private RecyclerView recyclerView;
private List<String> mList;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//初始化数据
initData();
//创建适配器
adapter = new MyAdapter(mList,this);
//设置适配器
recyclerView.setAdapter(adapter);
//设置布局管理器
recyclerView.setLayoutManager(new GridLayoutManager(this,5));
//设置Item增加、移除动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
//添加分割线
//垂直分割
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
//水平分割
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));
adapter.setItemOnclick(new MyAdapter.itemOnclick() {
@Override
public void itemOnclick(View view, String position) {
Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show();
}
});
}
//初始化30条数据
private void initData() {
mList = new ArrayList<>();
for (int i=0; i<10; i++){
mList.add("条目"+i);
}
}
//初始化控件
private void initView() {
btnAdd = (Button) findViewById(R.id.btn_add);
btnDelete = (Button) findViewById(R.id.btn_delete);
btnList = (Button) findViewById(R.id.btn_list);
btnGrid = (Button) findViewById(R.id.btn_grid);
btnFlow = (Button) findViewById(R.id.btn_flow);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//点击监听
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnList.setOnClickListener(this);
btnGrid.setOnClickListener(this);
btnFlow.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add://添加
adapter.addData(mList.size());
break;
case R.id.btn_delete://删除
adapter.deleteData(1);
break;
case R.id.btn_list://垂直布局
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
break;
case R.id.btn_grid://网格布局 5列
GridLayoutManager manager1 = new GridLayoutManager(this, 5);
recyclerView.setLayoutManager(manager1);
break;
case R.id.btn_flow://流式布局 3列
StaggeredGridLayoutManager manager2 = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager2);
break;
}
}
}
第四步:adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> mList;
private Context mContext;
public MyAdapter(List<String> list, Context context) {
mList = list;
mContext = context;
}
//添加
public void addData(int position){
if (mList.size()<20){
mList.add(position,"条目"+position);
notifyItemInserted(position);
}else {
Toast.makeText(mContext,"别玩添加了,删除试试",Toast.LENGTH_SHORT).show();
}
}
//删除
public void deleteData(int position){
if (mList.size()>0){
mList.remove(position-1);
notifyItemRemoved(position-1);
}else {
Toast.makeText(mContext,"没有数据了,添加试试",Toast.LENGTH_SHORT).show();
}
}
@NonNull
@Override
public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(mContext,R.layout.item,null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, final int position) {
holder.img.setImageResource(R.mipmap.ic_launcher);
holder.tv.setText(mList.get(position));
}
@Override
public int getItemCount()
{
return mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView img;
TextView tv;
public ViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
tv = itemView.findViewById(R.id.tv);
}
}
ok效果就出来了