导依赖:
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'
drawable文件夹下:
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_pubu.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.PubuActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/pubu_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_pubu.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/pubu_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/pubu_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/pubu_image"
app:layout_constraintRight_toRightOf="@id/pubu_image"
app:layout_constraintTop_toBottomOf="@id/pubu_image"
/>
</android.support.constraint.ConstraintLayout>
PubuActivity:
public class PubuActivity extends AppCompatActivity implements IView {
private IpresenterImpl mIpresenterImpl;
private RecyclerView pubu_view;
private final int ITEM_COUNT=2;
private StaggAdapter staggAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pubu);
//获取资源ID
pubu_view = findViewById(R.id.pubu_view);
mIpresenterImpl=new IpresenterImpl(this);
mIpresenterImpl.requestter(Apis.TYPE_URL,new HashMap<String, String>(),Bean.class);
//获得布局管理器
//使用瀑布流布局,第一个参数 spanCount 一行几个,第二个参数 orentation 排列方向
StaggeredGridLayoutManager manager=new StaggeredGridLayoutManager(ITEM_COUNT,OrientationHelper.VERTICAL);
pubu_view.setLayoutManager(manager);
staggAdapter = new StaggAdapter(this);
pubu_view.setAdapter(staggAdapter);
//自定义分割线
DividerDecoration dividerDecoration=new DividerDecoration(this);
pubu_view.addItemDecoration(dividerDecoration);
}
//解绑
@Override
protected void onDestroy() {
super.onDestroy();
mIpresenterImpl.Deatch();
}
@Override
public void getdata(Object object) {
Bean bean= (Bean) object;
staggAdapter.setList(bean.getData());
}
}
StaggAdapter:
public class StaggAdapter extends RecyclerView.Adapter<StaggAdapter.ViewHolder> {
private List<Bean.DataBean> list;
private Context context;
public StaggAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public Bean.DataBean getitem(int position){
return list.get(position);
}
public void setList(List<Bean.DataBean> list) {
this.list = list;
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_pubu, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.getdata(getitem(i),context);
}
@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.pubu_text);
imageView=itemView.findViewById(R.id.pubu_image);
}
public void getdata(Bean.DataBean getitem, Context context) {
textView.setText(getitem.getName());
Glide.with(context).load(getitem.getIcon()).into(imageView);
}
}
}
自定义分割线:
自定义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());
}
}