RecyclerView切换布局(列表→网格)

public class SousuoActivity extends AppCompatActivity implements SousuoView {


    private CheckBox ck_box;
    private XRecyclerView xrecyclerview;
    private SousuoPresenter sousuoPresenter;
    private String keywords;
    private List<GoodsBean.DataBean> data;
    boolean flag = false;
    private int page=1;
    private SearchGvAdapter gvAdapter;
    private SearchLvAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sousuo);
        ck_box = findViewById(R.id.ck);
        xrecyclerview = findViewById(R.id.sou_lv);


        Intent intent = getIntent();
        keywords = intent.getStringExtra("name");
        sousuoPresenter = new SousuoPresenter(this);

        sousuoPresenter.getData(ApiUtil.SEARCH_URL,keywords,page);
    }

    @Override
    public void onSuccess(final GoodsBean goodsBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // 获取到集合
                data = goodsBean.getData();
                Toast.makeText(SousuoActivity.this, goodsBean.getMsg(),Toast.LENGTH_SHORT).show();
                setLvAdapter();
                ck_box.setChecked(flag);
                if (ck_box.isChecked()){
                    setGvAdapter();
                    gvAdapter.notifyDataSetChanged();
                }else {
                    setLvAdapter();
                    adapter.notifyDataSetChanged();
                }
                ck_box.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (flag){
                            setLvAdapter();
                            ck_box.setChecked(false);
                            flag = ck_box.isChecked();
                        }else {
                            setGvAdapter();
                            ck_box.setChecked(true);
                            flag = ck_box.isChecked();
                        }
                    }
                });

                xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
                    @Override
                    public void onRefresh() {
                       new Handler().postDelayed(new Runnable() {
                           @Override
                           public void run() {
                               data.clear();
                               sousuoPresenter.getData(ApiUtil.SEARCH_URL,keywords,1);

                               // 刷新完成
                               xrecyclerview.refreshComplete();
                           }
                       },2000);
                    }

                    @Override
                    public void onLoadMore() {
                       new Handler().postDelayed(new Runnable() {
                           @Override
                           public void run() {
                               page++;
                               sousuoPresenter.getData(ApiUtil.SEARCH_URL, keywords,page);
                               // 刷新完成
                               xrecyclerview.loadMoreComplete();
                           }
                       },2000);
                    }
                });
            }
        });
    }

    private void setGvAdapter() {
        gvAdapter = new SearchGvAdapter(SousuoActivity.this,data);
        xrecyclerview.setAdapter(gvAdapter);
        xrecyclerview.setLayoutManager(new StaggeredGridLayoutManager(2,      GridLayoutManager.VERTICAL));
    }

    private void setLvAdapter() {
        adapter = new SearchLvAdapter(SousuoActivity.this,data);
        xrecyclerview.setAdapter(adapter);
        xrecyclerview.setLayoutManager(new LinearLayoutManager(SousuoActivity.this,LinearLayoutManager.VERTICAL,false));
    }
}
发布了62 篇原创文章 · 获赞 5 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览