Activity:
public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {
private static final int SPAN_COUNT = 2;
private IPresenterImpl mIPresenterImpl;
private EditText mEdit;
private XRecyclerView mXRecyclerView;
private RecyclerAdapter mAdapter;
/**
* 用成员变量记录页数
*/
private int mPage = 1;
/**
* 用成员变量记录所选条目
*/
private int mSort = 0;
/**
* 用来控制管理器是线性还是网格
*/
private boolean mIsLinear = true;
private TextView mTvComplex, mTvSalesVolume, mTvPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPresenter();
initView();
initRecyclerView();
}
/**
* 实现P层
*/
private void initPresenter() {
mIPresenterImpl = new IPresenterImpl(this);
}
/**
* 初始化所有控件
*/
private void initView() {
mTvComplex = findViewById(R.id.tv_complex);
mTvSalesVolume = findViewById(R.id.tv_sales_volume);
mTvPrice = findViewById(R.id.tv_price);
mTvComplex.setSelected(true);
findViewById(R.id.iv_search);
findViewById(R.id.iv_change);
mEdit = findViewById(R.id.edit_text);
mXRecyclerView = findViewById(R.id.XRecyclerView);
findViewById(R.id.iv_search).setOnClickListener(this);
findViewById(R.id.iv_change).setOnClickListener(this);
mTvComplex.setOnClickListener(this);
mTvSalesVolume.setOnClickListener(this);
mTvPrice.setOnClickListener(this);
}
/**
* 初始化RecyclerView
*/
private void initRecyclerView() {
mXRecyclerView.setPullRefreshEnabled(true);
mXRecyclerView.setLoadingMoreEnabled(true);
mXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
mPage = 1;
getProductList();
}
@Override
public void onLoadMore() {
getProductList();
}
});
changeRecyclerView();
}
/**
* 切换管理器
*/
private void changeRecyclerView() {
//根据状态判断展示线性还是网格
if (mIsLinear) {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mXRecyclerView.setLayoutManager(layoutManager);
} else {
GridLayoutManager layoutManager = new GridLayoutManager(this, SPAN_COUNT);
layoutManager.setOrientation(GridLayoutManager.VERTICAL);
mXRecyclerView.setLayoutManager(layoutManager);
}
//重新创建adapter,因为new 之后数据和点击事件都没有了,所以需要重新添加点击事件
mAdapter = new RecyclerAdapter(this, mIsLinear);
mAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
List<ProductsListBean.Data> data = mAdapter.getData();
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra(Constants.INTENT_KEY_PRODUCES_PID, data.get(position).getPid());
startActivity(intent);
}
});
mXRecyclerView.setAdapter(mAdapter);
//将状态反选
mIsLinear = !mIsLinear;
}
/**
* 拉取数据
*/
private void getProductList() {
HashMap<String, String> map = new HashMap<>();
map.put(Constants.MAP_KEY_SEARCH_PRODUCTS_KEYWORDS, mEdit.getText().toString());
map.put(Constants.MAP_KEY_SEARCH_PRODUCES_PAGE, String.valueOf(mPage));
map.put(Constants.MAP_KEY_SEARCH_PRODUCES_SORT, String.valueOf(mSort));
mIPresenterImpl.startRequest(Apis.URL_SEARCH_PRODUCTS, map, ProductsListBean.class);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_search:
//开始获取网络数据
getProductList();
break;
case R.id.iv_change:
//切换布局
//因为切换布局时需要new 一个adapter,为了保证数据的一致性,所以先取出老adapter中的老数据
List<ProductsListBean.Data> data = mAdapter.getData();
changeRecyclerView();
//将老数据赋值给新的adapter
mAdapter.setData(data);
break;
case R.id.tv_complex:
//如果当前栏目已经被选中,则再次点击无效,不然就会再次请求
if(!mTvComplex.isSelected()) {
//切换时一定要从第一页获取数据,不然会造成新老数据在一起,导致排序出现问题
mPage = 1;
mSort = 0;
getProductList();
mTvComplex.setSelected(true);
mTvSalesVolume.setSelected(false);
mTvPrice.setSelected(false);
}
break;
case R.id.tv_sales_volume:
if(!mTvSalesVolume.isSelected()) {
mPage = 1;
mSort = 1;
getProductList();
mTvComplex.setSelected(false);
mTvSalesVolume.setSelected(true);
mTvPrice.setSelected(false);
}
break;
case R.id.tv_price:
if(!mTvPrice.isSelected()) {
mPage = 1;
mSort = 2;
getProductList();
mTvComplex.setSelected(false);
mTvSalesVolume.setSelected(false);
mTvPrice.setSelected(true);
}
break;
default:
break;
}
}
@Override
public void showResponseData(Object data) {
if (data instanceof ProductsListBean) {
ProductsListBean productsListBean = (ProductsListBean) data;
List<ProductsListBean.Data> list = productsListBean.getData();
if (mPage == 1) {
mAdapter.setData(list);
} else {
mAdapter.addData(list);
}
mPage++;
mXRecyclerView.refreshComplete();
mXRecyclerView.loadMoreComplete();
}else{
Toast.makeText(this, "获取数据失败", Toast.LENGTH_SHORT).show();
}
}
@Override
public void showResponseFail(Object data) {
if (data instanceof Exception){
Exception e = (Exception)data;
e.printStackTrace();
}
Toast.makeText(this, "网络请求失败", Toast.LENGTH_SHORT).show();
}
}
适配器:
RecyclerAdapter:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private List<ProductsListBean.Data> mList = new ArrayList<>();
private boolean mIsLinear;
public RecyclerAdapter(Context context, boolean isLinear) {
mContext = context;
mIsLinear = isLinear;
}
public List<ProductsListBean.Data> getData(){
//TODO:完成剩余布局,完成javaBean剩余参数,完成数据展示,我不写了
return mList;
}
public void setData(List<ProductsListBean.Data> list) {
if (list != null) {
mList = list;
}
notifyDataSetChanged();
}
public void addData(List<ProductsListBean.Data> list) {
if (list != null) {
mList.addAll(list);
}
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder = null;
if (mIsLinear) {
View v = View.inflate(mContext, R.layout.item_recycle_linear, null);
holder = new ViewHolderLinear(v);
}else{
View v = View.inflate(mContext, R.layout.item_recycle_grid, null);
holder = new ViewHolderGrid(v);
}
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
String img = mList.get(position).getImages();
String[] imgs = img.split("\\|");
if(mIsLinear) {
ViewHolderLinear holderLinear = (ViewHolderLinear) holder;
Glide.with(mContext).load(imgs[0]).into(holderLinear.img);
holderLinear.textView.setText(mList.get(position).getTitle());
holderLinear.ll_item_recycle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mClickListener != null){
mClickListener.onItemClick(position);
}
}
});
}else{
ViewHolderGrid holderGrid = (ViewHolderGrid) holder;
Glide.with(mContext).load(imgs[0]).into(holderGrid.img);
holderGrid.textView.setText(mList.get(position).getTitle());
}
}
@Override
public int getItemCount() {
return mList.size();
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
class ViewHolderLinear extends RecyclerView.ViewHolder {
public ImageView img;
public TextView textView;
public LinearLayout ll_item_recycle;
public ViewHolderLinear(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_item_recycle);
img = itemView.findViewById(R.id.iv_item_recycle);
ll_item_recycle = itemView.findViewById(R.id.ll_item_recycle);
}
}
class ViewHolderGrid extends RecyclerView.ViewHolder {
public ImageView img;
public TextView textView;
public ViewHolderGrid(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_item_grid);
img = itemView.findViewById(R.id.iv_item_grid);
}
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public interface OnLongItemClickListener {
void onItemLongClick(int position);
}
private OnItemClickListener mClickListener;
private OnLongItemClickListener mLongItemClickListener;
public void setOnItemClickListener(OnItemClickListener clickListener) {
this.mClickListener = clickListener;
}
public void setOnLongItemClickListener(OnLongItemClickListener longItemClickListener) {
this.mLongItemClickListener = longItemClickListener;
}
}