Android实现点击切换视图并跳转传值
1,mainactivity的xml布局
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android=""
xmlns:app=""
xmlns: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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。