public class ProductListActivity extends AppCompatActivity implements View.OnClickListener,ProductListActivityInter {
private String keywords;
private RecyclerView product_list_recycler;
private RecyclerView product_grid_recycler;
private ProductListPresenter productListPresenter;
private int page = 1;
private ProDuctListAdapter proDuctListAdapter;
private ProDuctGridAdapter proDuctGridAdapter;
private ImageView product_image_back;
private LinearLayout linear_search;
private ImageView image_change;
private boolean isList = true;//是否是列表展示
private RefreshLayout refreshLayout;
private List<ProductListBean.DataBean> listAll = new ArrayList<>();//装当前页面所有的数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
product_list_recycler = findViewById(R.id.product_list_recycler);
product_grid_recycler = findViewById(R.id.product_grid_recycler);
product_image_back = findViewById(R.id.product_image_back);
linear_search = findViewById(R.id.linear_search);
image_change = findViewById(R.id.image_change);
refreshLayout = findViewById(R.id.refreshLayout);
productListPresenter = new ProductListPresenter(this);
//接收传递的关键词
keywords = getIntent().getStringExtra("keywords");
if (keywords != null) {
//根据关键词和page去请求列表数据
productListPresenter.getProductData(ApiUtil.SEARTCH_URL,keywords,page);
}
//设置列表布局
product_list_recycler.setLayoutManager(new LinearLayoutManager(ProductListActivity.this));
product_grid_recycler.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
//设置点击事件
product_image_back.setOnClickListener(this);
linear_search.setOnClickListener(this);
image_change.setOnClickListener(this);
//下拉刷新的监听
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
page = 1;
//集合清空
listAll.clear();
//重新获取数据
productListPresenter.getProductData(ApiUtil.SEARTCH_URL,keywords,page);
}
});
//上拉加载的监听
refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
page ++;
//重新获取数据
productListPresenter.getProductData(ApiUtil.SEARTCH_URL,keywords,page);
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.linear_search:
Toast.makeText(this,"即将跳转搜索...",Toast.LENGTH_SHORT).show();
break;
case R.id.product_image_back:
finish();
break;
case R.id.image_change:
if (isList){
image_change.setImageResource(R.drawable.kind_liner);
product_grid_recycler.setVisibility(View.VISIBLE);
product_list_recycler.setVisibility(View.GONE);
isList = false;
}else {
image_change.setImageResource(R.drawable.kind_grid);
product_list_recycler.setVisibility(View.VISIBLE);
product_grid_recycler.setVisibility(View.GONE);
isList = true;
}
break;
}
}
@Override
public void getProductDataSuccess(ProductListBean productListBean) {
listAll.addAll(productListBean.getData());
setAdapter();
proDuctListAdapter.setOnItemListner(new OnItemListner() {
@Override
public void onItemClick(int position) {
//跳转详情
Intent intent = new Intent(ProductListActivity.this,XiangQingActivity.class);
intent.putExtra("pid",listAll.get(position).getPid());
startActivity(intent);
}
@Override
public void onItemLongClick(int position) {
}
});
proDuctGridAdapter.setOnItemListner(new OnItemListner() {
@Override
public void onItemClick(int position) {
//跳转详情
Intent intent = new Intent(ProductListActivity.this,XiangQingActivity.class);
intent.putExtra("pid",listAll.get(position).getPid());
startActivity(intent);
}
@Override
public void onItemLongClick(int position) {
}
});
}
private void setAdapter() {
if (proDuctListAdapter == null){
proDuctListAdapter = new ProDuctListAdapter(ProductListActivity.this, listAll);
product_list_recycler.setAdapter(proDuctListAdapter);
}else {
proDuctListAdapter.notifyDataSetChanged();
}
if (proDuctGridAdapter == null){
proDuctGridAdapter = new ProDuctGridAdapter(ProductListActivity.this, listAll);
product_grid_recycler.setAdapter(proDuctGridAdapter);
}else {
proDuctGridAdapter.notifyDataSetChanged();
}
//停止刷新和加载更多
refreshLayout.finishRefresh();
refreshLayout.finishLoadmore();
}
}