欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2020-11-08 12:05:16|栏目:Android代码|点击:

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);
}
}

上一篇:教你如何使用platform密钥对apk进行签名

栏    目:Android代码

下一篇:Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例

本文标题:Android实现点击切换视图并跳转传值

本文地址:http://www.codeinn.net/misctech/20230.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有