导依赖:
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" />
</shape>
activity_linear.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.LinearActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/linear_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_linear.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/linear_image"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/linear_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/linear_image"
android:textSize="17sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
</android.support.constraint.ConstraintLayout>
LinearActivity:
public class LinearActivity extends AppCompatActivity implements IView {
private IpresenterImpl mIpresenterImpl;
private RecyclerView linear_view;
private LinearAdapter linearAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear);
//获取资源ID
linear_view = findViewById(R.id.linear_view);
//互绑
mIpresenterImpl=new IpresenterImpl(this);
mIpresenterImpl.requestter(Apis.TYPE_URL,new HashMap<String, String>(),Bean.class);
//写一个布局管理器,线性管理器
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
//设置方向
layoutManager.setOrientation(OrientationHelper.VERTICAL);
//设置布局管理器
linear_view.setLayoutManager(layoutManager);
linearAdapter = new LinearAdapter(this);
linear_view.setAdapter(linearAdapter);
//设置分割线
DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.item_decoration));
linear_view.addItemDecoration(dividerItemDecoration);
}
//解绑
@Override
protected void onDestroy() {
super.onDestroy();
mIpresenterImpl.Deatch();
}
@Override
public void getdata(Object object) {
Bean bean= (Bean) object;
linearAdapter.setList(bean.getData());
}
}
LinearAdapter:
public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.ViewHolder> {
private List<Bean.DataBean> list;
private Context context;
public LinearAdapter(Context context) {
this.context=context;
list=new ArrayList<>();
}
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_linear, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Bean.DataBean dataBean = list.get(i);
viewHolder.textView.setText(dataBean.getName());
Glide.with(context).load(dataBean.getIcon()).into(viewHolder.imageView);
}
@Override
public int getItemCount() {
return list.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.linear_text);
imageView=itemView.findViewById(R.id.linear_image);
}
}
}