adapter
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
Context context;
List<Searchbean.ResultBean>list;
public SearchAdapter(Context context, List<Searchbean.ResultBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public SearchAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
View view = View.inflate(viewGroup.getContext(), R.layout.search_item, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SearchAdapter.ViewHolder viewHolder, int i) {
viewHolder.img.setImageURI(list.get(i).getMasterPic());
viewHolder.name.setText(list.get(i).getCommodityName());
viewHolder.price.setText(list.get(i).getPrice()+"");
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final SimpleDraweeView img;
private final TextView name;
private final TextView price;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.image1);
name = itemView.findViewById(R.id.name);
price = itemView.findViewById(R.id.price);
}
}
}
manactivity
public class SearchActivity extends AppCompatActivity implements IShowview<Searchbean> {
private ImageView leftimg;
private ImageView rightimg;
private EditText content1;
private RecyclerView search_recycle;
private Showpresenter showpresenter;
private int page = 1;
private int count = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
search_recycle = findViewById(R.id.search_recycle);
leftimg = findViewById(R.id.leftimg1);
rightimg = findViewById(R.id.rightimg1);
content1 = findViewById(R.id.content);
search_recycle.setLayoutManager(new GridLayoutManager(SearchActivity.this, 2));
showpresenter = new Showpresenter();
showpresenter.setView(this);
rightimg.setOnClickListener(new View.OnClickListener() {
private String keyword;
@Override
public void onClick(View v) {
keyword = content1.getText().toString();
showpresenter.getSearch(keyword, page, count);
}
});
}
@Override
public void Success(Searchbean searchbean, int s) {
if(s==6){
List<Searchbean.ResultBean> list = searchbean.getResult();
SearchAdapter searchsAdapter = new SearchAdapter(SearchActivity.this, list);
search_recycle.setAdapter(searchsAdapter);
}
}
@Override
public void error(String s) {
}
}
showpresnet
public void getSearch(String keyword,int page,int count) {
api.getSearch(keyword, page, count)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<Searchbean>() {
@Override
public void accept(Searchbean searchBean) throws Exception {
getView().Success(searchBean, 6);
}
});
//搜索
//
// @GET("small/commodity/v1/findCommodityByKeyword")
// Observable<Searchbean> getSearch(@Query("keyword") String keyword, @Query("page") int page, @Query("count") int count);