导依赖:
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
item_decoration.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="#ff9966"
android:centerColor="#cccfff"
android:endColor="#ff6699"
android:type="linear"
/>
<size android:height="2dp" android:width="2dp"/>
</shape>
item_grid_heng.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="#ff9966"
android:centerColor="#cccfff"
android:endColor="#ff6699"
android:type="linear"
/>
<size android:height="2dp"
/>
</shape>
item_grid_shu.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="#ff9966"
android:centerColor="#cccfff"
android:endColor="#ff6699"
android:type="linear"
/>
<size android:height="2dp" android:width="2dp"
/>
</shape>
activity_grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".activity.GridActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/grid_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
item_grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/grid_image"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/grid_image"
app:layout_constraintRight_toRightOf="@id/grid_image"
app:layout_constraintTop_toBottomOf="@id/grid_image"
/>
</android.support.constraint.ConstraintLayout>
GridActivity:
public class GridActivity extends AppCompatActivity implements IView {
private IpresenterImpl mIpresenterImpl;
private RecyclerView grid_view;
//一行显示多少个
private final int ITEM_COUNT=2;
private GridAdapter gridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid);
grid_view = findViewById(R.id.grid_view);
mIpresenterImpl=new IpresenterImpl(this);
mIpresenterImpl.requestter(Apis.TYPE_URL,new HashMap<String, String>(),Bean.class);
//设置布局管理器
GridLayoutManager gridLayoutManager=new GridLayoutManager(this,ITEM_COUNT);
//设置方向
gridLayoutManager.setOrientation(OrientationHelper.VERTICAL);
grid_view.setLayoutManager(gridLayoutManager);
//设置适配器
gridAdapter = new GridAdapter(this);
grid_view.setAdapter(gridAdapter);
//设置增加或删除条目的动画
grid_view.setItemAnimator(new DefaultItemAnimator());
//自定义分割线
/* DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.item_grid_shu));
DividerItemDecoration dividerItemDecoration1=new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL);
dividerItemDecoration1.setDrawable(ContextCompat.getDrawable(this,R.drawable.item_grid_heng));
grid_view.addItemDecoration(dividerItemDecoration);
grid_view.addItemDecoration(dividerItemDecoration1);*/
DividerDecoration dividerDecoration=new DividerDecoration(this);
grid_view.addItemDecoration(dividerDecoration);
}
@Override
protected void onDestroy() {
super.onDestroy();
mIpresenterImpl.Deatch();
}
@Override
public void getdata(Object object) {
Bean bean= (Bean) object;
final List<Bean.DataBean> data = bean.getData();
gridAdapter.setList(bean.getData());
gridAdapter.setCliclListener(new GridAdapter.Click() {
@Override
public void OnClick(int position) {
gridAdapter.removeList(position);
}
});
}
}
GridAdapter:
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
private List<Bean.DataBean> list;
private Context context;
public GridAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<Bean.DataBean> list) {
this.list = list;
notifyDataSetChanged();
}
public Bean.DataBean getitem(int position){
return list.get(position);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_grid, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.getdata(getitem(i),context,i);
}
//删除数据
public void removeList(int position){
list.remove(position);
//必须使用notifyItemRemoved 才能加载移除动画
notifyItemRemoved(position);
notifyItemRangeRemoved(position,list.size());
}
//添加数据
public void additem(int position, Bean.DataBean bean){
list.add(position,bean);
notifyItemInserted(position);
}
static Click mClick;
public void setCliclListener(Click click){
mClick=click;
}
public interface Click{
void OnClick(int position);
}
@Override
public int getItemCount() {
return list.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.grid_text);
imageView=itemView.findViewById(R.id.grid_image);
}
public void getdata(Bean.DataBean getitem, Context context, final int i) {
textView.setText(getitem.getName());
Glide.with(context).load(getitem.getIcon()).into(imageView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mClick.OnClick(i);
}
});
}
}
}
自定义分割线:
自定义View:
DividerDecoration:
public class DividerDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public DividerDecoration(Context context) {
//在构造方法中,讲分割线的样子拿到
mDivider=ContextCompat.getDrawable(context,R.drawable.item_decoration);
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
//画垂直和水平的两种分割线
drawHorizontal(c, parent);
drawVertical(c, parent);
}
private void drawHorizontal(Canvas c, RecyclerView parent) {
//拿到所有孩子
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.getLeft() - params.leftMargin;
final int right = child.getRight() + params.rightMargin
+ mDivider.getIntrinsicWidth();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
private void drawVertical(Canvas c, RecyclerView parent) {
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 top = child.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin;
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition,
RecyclerView parent) {
outRect.set(0, 0, mDivider.getIntrinsicWidth(),
mDivider.getIntrinsicHeight());
}
}