Android实现点击切换视图并跳转传值
1,MainActivity的xml布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content">
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: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
publicclassMainActivityextendsAppCompatActivityimplementsIView{
privateIPersenterImpliPersenter;
Stringpath="searchProducts";
privateXRecyclerViewxRecyclerView;
HeadViewheadView;
privateBeanAdapteradapter;
privateintpage;
privatebooleanisLinear=true;
privateObjectmessage;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iPersenter=newIPersenterImpl(this);
page=1;
init();
//注册
EventBus.getDefault().register(this);
}
privatevoidinit(){
xRecyclerView=findViewById(R.id.xresycle);
headView=findViewById(R.id.headview);
//点击头部进行切换
headView.setOnClick(newHeadView.OnClick(){
@Override
publicvoidClick(){
List
changeLiGr();
adapter.setData(data);
}
});
//改变头部文字重新请求
headView.setGetEdText(newHeadView.getEdText(){
@Override
publicvoidgetName(Stringname){
page=1;
initData(name,page);
}
});
//刷新加载
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setPullRefreshEnabled(true);
changeLiGr();
xRecyclerView.setLoadingListener(newXRecyclerView.LoadingListener(){
@Override
publicvoidonRefresh(){
page=1;
initData(message+"",page);
}
@Override
publicvoidonLoadMore(){
initData(message+"",page);
}
});
}
//改变状态
privatevoidchangeLiGr(){
if(isLinear){
LinearLayoutManagerlayoutManager=newLinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
xRecyclerView.setLayoutManager(layoutManager);
}else{
GridLayoutManagermanager=newGridLayoutManager(this,2);
manager.setOrientation(LinearLayoutManager.VERTICAL);
xRecyclerView.setLayoutManager(manager);
}
adapter=newBeanAdapter(this,isLinear);
xRecyclerView.setAdapter(adapter);
adapter.setOnClick(newBeanAdapter.OnClick(){
@Override
publicvoidclick(intpid){
Intentintent=newIntent(MainActivity.this,LoginActivity.class);
intent.putExtra("pid",pid);
startActivity(intent);
}
});
//状态反选
isLinear=!isLinear;
}
//请求
privatevoidinitData(Stringname,intpage){
Map
pamars.put("keywords",name);
pamars.put("page",page+"");
iPersenter.showRequestData(path,pamars,GoodsBean.class);
}
//得到粘性事件
@Subscribe(threadMode=ThreadMode.MAIN,sticky=true)
publicvoidgetName(EventBusBeaneventBusBean){
if(eventBusBean.getId()==1){
message=eventBusBean.getMessage();
initData(eventBusBean.getMessage().toString(),page);
}
}
@Override
publicvoidstartRequestData(Objectdata){
if(datainstanceofGoodsBean){
GoodsBeanbean=(GoodsBean)data;
if(page==1){
adapter.setData(bean.getData());
}else{
adapter.addData(bean.getData());
}
page++;
xRecyclerView.refreshComplete();
xRecyclerView.loadMoreComplete();;
}
}
//解绑
@Override
protectedvoidonDestroy(){
super.onDestroy();
iPersenter.onDestory();
EventBus.getDefault().unregister(this);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。