所需要的依赖
1
2
3
4
5
6
7
8
|
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'org.greenrobot:eventbus:3.1.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
|
net下HttpUtils GET请求
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
|
public class HttpUtils {
private static volatile HttpUtils httpUtils;
private final OkHttpClient client;
private HttpUtils(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build();
}
public static HttpUtils getHttpUtils(){
if(httpUtils==null){
synchronized (HttpUtils.class){
if (httpUtils==null){
httpUtils = new HttpUtils();
}
}
}
return httpUtils;
}
/**
* GET请求
*
* @param url
* @param callback
*/
public void doGet(String url, Callback callback){
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(callback);
}
}
|
OnNetListener接口回调
1
2
3
4
|
public interface OnNetListener<
T
> {
public void onSuccess(T t);
public void onFailure(Exception e);
}
|
Api
1
2
3
|
public interface Api {
public static final String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation";
}
|
model层 MainModel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class MainModel implements IMainModel{
private Handler handler = new Handler(Looper.getMainLooper());
public void getGoods(final OnNetListener<
GoosBean
> onNetListener){
HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final GoosBean goosBean = new Gson().fromJson(string, GoosBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onSuccess(goosBean);
}
});
}
});
}
}
|
IMainModel
1
2
3
|
public interface IMainModel {
public void getGoods(OnNetListener<
GoosBean
> onNetListener);
}
|
presenter层 MainPresenter
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
|
public class MainPresenter {
private final IMainModel iMainModel;
private final IMainActivity iMainActivity;
public MainPresenter(IMainActivity iMainActivity) {
this.iMainActivity = iMainActivity;
iMainModel = new MainModel() ;
}
public void getGoods(){
iMainModel.getGoods(new OnNetListener<
GoosBean
>() {
@Override
public void onSuccess(GoosBean goosBean) {
//List<
GoosBean.DataBean
> groupList, List<
List
<GoosBean.DataBean.DatasBean>> childList
List<
GoosBean.DataBean
> dataBean = goosBean.getData();
List<
List
<GoosBean.DataBean.DatasBean>> childList = new ArrayList<
List
<GoosBean.DataBean.DatasBean>>();
for (int i = 0; i <
dataBean.size
(); i++) {
List<GoosBean.DataBean.DatasBean> datas = dataBean.get(i).getDatas();
childList.add(datas);
}
iMainActivity.showList(dataBean, childList);
}
@Override
public void onFailure(Exception e) {
}
});
}
}
|
view层 MainActivity
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
59
60
61
62
63
64
65
|
public class MainActivity extends AppCompatActivity implements IMainActivity{
private ExpandableListView mElv;
private CheckBox mCheckbox2;
/**
* 0
*/
private TextView mTvPrice;
/**
* 结算(0)
*/
private TextView mTvNum;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
EventBus.getDefault().register(this);
new MainPresenter(this).getGoods();
mCheckbox2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.changeAllListCbState(mCheckbox2.isChecked());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
private void initView() {
mElv = (ExpandableListView) findViewById(R.id.elv);
mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);
mTvPrice = (TextView) findViewById(R.id.tv_price);
mTvNum = (TextView) findViewById(R.id.tv_num);
}
@Override
public void showList(List<
GoosBean.DataBean
> groupList, List<
List
<GoosBean.DataBean.DatasBean>> childList) {
adapter = new MyAdapter(this, groupList, childList);
mElv.setAdapter(adapter);
mElv.setGroupIndicator(null);
//默认让其全部展开
for (int i = 0; i < groupList.size(); i++) {
mElv.expandGroup(i);
}
}
@Subscribe
public void onMessageEvent(MessageEvent event){
mCheckbox2.setChecked(event.isChecked());
}
@Subscribe
public void onMessageEvent(PriceAndCountEvent event){
mTvNum.setText("结算(" + event.getCount() + ")");
mTvPrice.setText(event.getPrice()+"");
}
}
|
IMainActivity
1
2
3
|
public interface IMainActivity {
public void showList(List<
GoosBean.DataBean
> groupList, List<
List
<GoosBean.DataBean.DatasBean>> childList);
}
|
adapter下 MyAdapter