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

作者:木子呀 时间:2022-07-14 07:23:28 

1,MainActivity的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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">

   <com.example.tiamo.weeklianxi.view.HeadView
       android:id="@+id/headview"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

   </com.example.tiamo.weeklianxi.view.HeadView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#fff"
            android:text="销量"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#fff"
            android:text="销量"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#fff"
            android:text="销量"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#fff"
            android:text="销量"
            />
    </LinearLayout>

<com.jcodecraeer.xrecyclerview.XRecyclerView
    android:id="@+id/xresycle"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</com.jcodecraeer.xrecyclerview.XRecyclerView>

</LinearLayout>
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<GoodsBean.DataBean> 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<String,String> 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);
}
}

来源:https://blog.csdn.net/weixin_43587850/article/details/85558815

标签:Android,切换视图
0
投稿

猜你喜欢

  • C#操作XML方法详解

    2022-11-10 21:21:14
  • c语言中static修饰函数的方法及代码

    2023-08-26 15:46:21
  • Android 跨进程通Messenger(简单易懂)

    2022-03-24 00:48:25
  • SpringBoot整合支付宝APP支付

    2021-10-12 02:57:24
  • Android屏蔽软键盘并且显示光标的实例详解

    2023-01-16 16:54:20
  • 解析Android框架之Volley源码

    2021-09-30 19:44:28
  • Java高版本Api在Android中的使用方法详解

    2023-01-19 20:03:08
  • Android ExpandableListView双层嵌套实现三级树形菜单

    2023-11-05 23:04:55
  • java实现录音播放功能

    2023-03-25 03:38:23
  • C# DataGridView添加新行的2个方法

    2023-06-23 05:42:19
  • spring boot 打包jar jar没有主目录清单问题的完美解决方法

    2021-08-28 16:07:39
  • C#自定义控件实现TextBox禁止粘贴的方法

    2023-08-03 06:00:22
  • 深入分析c# 继承

    2023-02-18 21:36:31
  • 详解基于spring多数据源动态调用及其事务处理

    2023-06-23 14:37:25
  • Java如何实现压缩文件与解压缩zip文件

    2022-01-28 09:14:00
  • Android仿微信朋友圈图片查看器

    2023-01-27 18:07:20
  • Kotlin启动协程的三种方式示例详解

    2023-06-07 02:58:53
  • 探讨如何用委托处理排序

    2023-12-17 15:06:36
  • Unity键盘WASD实现物体移动

    2021-06-14 13:46:21
  • SpringBoot如何用java生成静态html

    2023-08-08 08:46:59
  • asp之家 软件编程 m.aspxhome.com