如何在Android中實現(xiàn)一個點擊切換視圖并跳轉(zhuǎn)傳值功能

本文章向大家介紹如何在Android中實現(xiàn)一個點擊切換視圖并跳轉(zhuǎn)傳值功能的基本知識點總結(jié)和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。

成都創(chuàng)新互聯(lián)總部坐落于成都市區(qū),致力網(wǎng)站建設服務有網(wǎng)站建設、網(wǎng)站設計、網(wǎng)絡營銷策劃、網(wǎng)頁設計、網(wǎng)站維護、公眾號搭建、微信小程序、軟件開發(fā)等為企業(yè)提供一整套的信息化建設解決方案。創(chuàng)造真正意義上的網(wǎng)站建設,為互聯(lián)網(wǎng)品牌在互動行銷領域創(chuàng)造價值而不懈努力!

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

}
//改變狀態(tài)
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);
        }
    });
    //狀態(tài)反選
    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);
}
}

以上就是小編為大家?guī)淼娜绾卧贏ndroid中實現(xiàn)一個點擊切換視圖并跳轉(zhuǎn)傳值功能的全部內(nèi)容了,希望大家多多支持創(chuàng)新互聯(lián)!

新聞名稱:如何在Android中實現(xiàn)一個點擊切換視圖并跳轉(zhuǎn)傳值功能
文章起源:http://bm7419.com/article2/goejic.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、ChatGPT、品牌網(wǎng)站建設網(wǎng)站營銷、網(wǎng)頁設計公司域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化