android点击切换,Android实现点击切换视图并跳转传值

该博客介绍了如何在Android应用中使用XRecyclerView库来实现下拉刷新和上拉加载更多功能。内容包括XML布局配置、MainActivity的代码实现,以及通过EventBus进行事件传递和数据更新。此外,还展示了如何根据用户点击切换列表的显示方式,即线性布局和网格布局之间的切换。
摘要由CSDN通过智能技术生成

1,MainActivity的xml布局

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:id="@+id/headview"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#fff"

android:text="销量"

/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#fff"

android:text="销量"

/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#fff"

android:text="销量"

/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#fff"

android:text="销量"

/>

android:id="@+id/xresycle"

android:layout_width="match_parent"

android:layout_height="match_parent">

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

2.MainActivity

public class MainActivity extends AppCompatActivity implements IView {

private IPersenterImpl iPersenter;

String path ="searchProducts";

private XRecyclerView xRecyclerView;

HeadView headView;

private BeanAdapter adapter;

private int page;

private boolean isLinear = true;

private Object message;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

iPersenter = new IPersenterImpl(this);

page = 1;

init();

//注册

EventBus.getDefault().register(this);

}

private void init() {

xRecyclerView = findViewById(R.id.xresycle);

headView = findViewById(R.id.headview);

//点击头部进行切换

headView.setOnClick(new HeadView.OnClick() {

@Override

public void Click() {

List data = adapter.getData();

changeLiGr();

adapter.setData(data);

}

});

//改变头部文字重新请求

headView.setGetEdText(new HeadView.getEdText() {

@Override

public void getName(String name) {

page = 1;

initData(name,page);

}

});

//刷新加载

xRecyclerView.setLoadingMoreEnabled(true);

xRecyclerView.setPullRefreshEnabled(true);

changeLiGr();

xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {

@Override

public void onRefresh() {

page = 1;

initData(message+"",page);

}

@Override

public void onLoadMore() {

initData(message+"",page);

}

});

}

//改变状态

private void changeLiGr(){

if (isLinear){

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

xRecyclerView.setLayoutManager(layoutManager);

}else{

GridLayoutManager manager = new GridLayoutManager(this,2);

manager.setOrientation(LinearLayoutManager.VERTICAL);

xRecyclerView.setLayoutManager(manager);

}

adapter = new BeanAdapter(this,isLinear);

xRecyclerView.setAdapter(adapter);

adapter.setOnClick(new BeanAdapter.OnClick() {

@Override

public void click(int pid) {

Intent intent = new Intent(MainActivity.this,LoginActivity.class);

intent.putExtra("pid",pid);

startActivity(intent);

}

});

//状态反选

isLinear = !isLinear;

}

//请求

private void initData(String name,int page) {

Map pamars = new HashMap<>();

pamars.put("keywords",name);

pamars.put("page",page+"");

iPersenter.showRequestData(path,pamars,GoodsBean.class);

}

//得到粘性事件

@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)

public void getName(EventBusBean eventBusBean){

if (eventBusBean.getId() == 1) {

message = eventBusBean.getMessage();

initData(eventBusBean.getMessage().toString(), page);

}

}

@Override

public void startRequestData(Object data) {

if (data instanceof GoodsBean){

GoodsBean bean = (GoodsBean) data;

if (page == 1){

adapter.setData(bean.getData());

}else{

adapter.addData(bean.getData());

}

page++;

xRecyclerView.refreshComplete();

xRecyclerView.loadMoreComplete();;

}

}

//解绑

@Override

protected void onDestroy() {

super.onDestroy();

iPersenter.onDestory();

EventBus.getDefault().unregister(this);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值