一个分层的登陆搜索页面

分层思想

public class ShoppListActivity extends AppCompatActivity implements ShoppListacFace {

    private RecyclerView shopp_rec;
    private SmartRefreshLayout smart_re;
    private ImageView goods_img;
    private EditText goods_edtext;
    private ShoppListPresenter shoppListPresenter;
    private List<ShoppListBean.DataBean> slist = new ArrayList<>();
    private int page = 1;
    Boolean flag = true;
    private MyShoppListAdapter myShoppListAdapter;
    //final修饰的变量就是一个常量,只能赋值一次
    private final int SOUSUO = 0, FRESH = 1, LOADMORE = 2;
    //TYPE是一个变量用于分层
    private int TYPE = 0;
    private String edguan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopp_list);

        initViews();

        shoppListPresenter = new ShoppListPresenter(this);

    }

    public void initViews() {
        //快捷键的方式为"ctrl+alt+M"。快速提取方法
        shopp_rec = findViewById(R.id.shopp_rec);
        smart_re = findViewById(R.id.smart_re);
        goods_img = findViewById(R.id.goods_img);
        goods_edtext = findViewById(R.id.goods_edtext);

        smart_re.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                TYPE = FRESH;
                page = 1;
                slist.clear();
                shoppListPresenter.getshopp(ApiUtil.Product_url, edguan, page);

            }
        });

        smart_re.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                TYPE = LOADMORE;
                page++;
                shoppListPresenter.getshopp(ApiUtil.Product_url, edguan, page);
            }
        });
    }

    public void imgqie(View view) {

        if (flag) {
            shopp_rec.setLayoutManager(new GridLayoutManager(ShoppListActivity.this, 2, GridLayoutManager.VERTICAL, false));
            myShoppListAdapter.notifyDataSetChanged();
            goods_img.setImageResource(R.drawable.lv_icon);
            flag = false;
        } else {
            shopp_rec.setLayoutManager(new LinearLayoutManager(ShoppListActivity.this, LinearLayoutManager.VERTICAL, false));
            myShoppListAdapter.notifyDataSetChanged();
            goods_img.setImageResource(R.drawable.grid_icon);
            flag = true;
        }

    }

    public void detailbut(View view) {

        TYPE = SOUSUO;
        edguan = goods_edtext.getText().toString();

        if (edguan == null || edguan.equals("")) {
            Toast.makeText(ShoppListActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
        } else {
            shoppListPresenter.getshopp(ApiUtil.Product_url, edguan, page);
        }

    }

    private static final String TAG = "ShoppListActivity----";

    @Override
    public void shoppac(ShoppListBean shoppListBean) {

        switch (TYPE) {
            case SOUSUO:
                slist.clear();
                break;
            case FRESH:
                slist.clear();
                break;
            case LOADMORE:

                break;
        }

        slist.addAll(shoppListBean.getData());

        if (myShoppListAdapter == null) {
            Log.d(TAG, "设置适配器---- ");
            myShoppListAdapter = new MyShoppListAdapter(ShoppListActivity.this, slist);
            shopp_rec.setAdapter(myShoppListAdapter);

        } else {
            Log.d(TAG, "更新数据----: ");
            myShoppListAdapter.notifyDataSetChanged();
        }

        smart_re.finishRefresh();
        smart_re.finishLoadMore();

        if (flag) {
            shopp_rec.setLayoutManager(new LinearLayoutManager(ShoppListActivity.this, LinearLayoutManager.VERTICAL, false));
        } else {
            shopp_rec.setLayoutManager(new GridLayoutManager(ShoppListActivity.this, 2, GridLayoutManager.VERTICAL, false));
        }

        myShoppListAdapter = new MyShoppListAdapter(ShoppListActivity.this, slist);
        shopp_rec.setAdapter(myShoppListAdapter);

        //点击事件
        myShoppListAdapter.setonClickListener(new OnitemClick() {
            @Override
            public void OnClick(int position) {
                Intent intent = new Intent(ShoppListActivity.this, DetailActivity.class);
                intent.putExtra("pid", slist.get(position).getPid());
                startActivity(intent);
            }
        });
    }

}

猜你喜欢

转载自blog.csdn.net/wrpbk/article/details/80042894