APIHTTP界面
public interface ApiHttp {
@Multipart
@POST("circle/verify/v1/releaseCircle")
Observable<UpDataBean>updatabean(
@Header("userId") int userId,
@Header("sessionId") String sessionId,
@Query("commodityId") int commodityId,
@Query("content") String trim,
@Part List<MultipartBody.Part> image
);
@GET("circle/verify/v1/findMyCircleById")
Observable<DataBean>databean(@Header("userId") int userId,
@Header("sessionId") String sessionId,
@Query("page") int page,
@Query("count") int count);
}
HomeActivity
public class HomeActivity extends AppCompatActivity implements IMainView.CallBackData {
private RecyclerView recyclerview;
private DataPresenter presenter;
private int page = 1;
private MyAdapter adapter;
private SmartRefreshLayout srl_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
recyclerview = findViewById(R.id.recyclerview);
srl_layout = findViewById(R.id.srl_layout);
presenter = new DataPresenter();
presenter.ActachView(this);
srl_layout.setEnableLoadMore(true);
srl_layout.setEnableRefresh(true);
srl_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
page++;
presenter.DataPresenter(page);
srl_layout.finishLoadMore();
}
});
srl_layout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
page=1;
presenter.DataPresenter(page);
srl_layout.finishRefresh();
}
});
}
@Override
public void success(DataBean dataBean) {
List<DataBean.ResultBean> result = dataBean.getResult();
Log.e("AAA", "success: " + dataBean);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview.setLayoutManager(manager);
adapter = new MyAdapter(result, this);
recyclerview.setAdapter(adapter);
}}
Adapter
public class MyAdapter extends RecyclerView.Adapter {
List<DataBean.ResultBean> list;
private Context context;
public MyAdapter(List<DataBean.ResultBean> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == 0) {
View inflate = View.inflate(context, R.layout.item1, null);
MyHolder1 holder1 = new MyHolder1(inflate);
return holder1;
} else {
View inflate1 = View.inflate(context, R.layout.item2, null);
MyHolder2 holder2 = new MyHolder2(inflate1);
return holder2;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
int type = getItemViewType(i);
if (type == 0) {
MyHolder1 holder1 = (MyHolder1) viewHolder;
holder1.recy_text1.setText(list.get(i).getContent());
String[] split = list.get(i).getImage().split(",");
for (int j = 0; j < split.length; j++) {
String s = split[j];
ImageView imageView = new ImageView(context);
Glide.with(context).load(s).into(imageView);
holder1.recy_layout1.addView(imageView, 150, 150);
}
} else if (type == 1) {
MyHolder2 holder2 = (MyHolder2) viewHolder;
holder2.recy_text2.setText(list.get(i).getContent());
String[] split = list.get(i).getImage().split(",");
for (int j = 0; j < split.length; j++) {
String s = split[j];
ImageView imageView = new ImageView(context);
Glide.with(context).load(s).into(imageView);
holder2.recy_layout2.addView(imageView, 150, 150);
}
}
}
@Override
public int getItemCount() {
return list.size();
}
class MyHolder1 extends RecyclerView.ViewHolder {
private final TextView recy_text1;
private final LinearLayout recy_layout1;
public MyHolder1(@NonNull View itemView) {
super(itemView);
recy_text1 = itemView.findViewById(R.id.recy_text1);
recy_layout1 = itemView.findViewById(R.id.recy_layout1);
}
}
class MyHolder2 extends RecyclerView.ViewHolder {
private final TextView recy_text2;
private final LinearLayout recy_layout2;
public MyHolder2(@NonNull View itemView) {
super(itemView);
recy_text2 = itemView.findViewById(R.id.recy_text2);
recy_layout2 = itemView.findViewById(R.id.recy_layout2);
}
}
}
Mainactivity
public class MainActivity extends TakePhotoActivity implements IMainView.CallBackUp {
private EditText shu;
private Button xvan, shang;
private List<File> pics = new ArrayList<>();
List<MultipartBody.Part> image = new ArrayList<>();
private UpDataPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shu = (EditText) findViewById(R.id.shu);
xvan = (Button) findViewById(R.id.xvan);
shang = (Button) findViewById(R.id.shang);
presenter = new UpDataPresenter();
presenter.ActachView(this);
xvan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TakePhoto takePhoto = getTakePhoto();
takePhoto.onPickFromGallery();
}
});
shang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String trim = shu.getText().toString().trim();
presenter.UpDataPresenter(trim, image);
}
});
}
@Override
public void takeSuccess(TResult result) {
super.takeSuccess(result);
if (image.size() < 4) {
TImage image1 = result.getImage();
String originalPath = image1.getOriginalPath();
File file = new File(originalPath);
Log.e("aa",file.getName()+"");
pics.add(file);
MediaType parse = MediaType.parse("image/*");
RequestBody requestBody = RequestBody.create(parse, file);
MultipartBody.Part data = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
image.add(data);
} else {
Toast.makeText(this, "不能再多了", Toast.LENGTH_SHORT).show();
}
}
@Override
public void success(UpDataBean upDataBean) {
if (upDataBean.getStatus().equals("0000")) {
Toast.makeText(this, upDataBean.getMessage() + "", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent);
}else {
Toast.makeText(this, upDataBean.getMessage() + "", Toast.LENGTH_SHORT).show();
}
}
}