admin
2021-07-06 abce02c7a61820f5d580f87364d542e817be429c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.tejia.lijin.app.ui.gmtemplate;
 
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
 
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.ui.BaseFragmentActivity;
import com.tejia.lijin.app.util.umengCustomEvent.GoodsCustomEvent;
import com.tejia.lijin.app.view.BaseFragment;
import com.viewpagerindicator.ImagePath;
import com.viewpagerindicator.TabPageIndicator_Gm;
import com.wpc.library.RetainViewFragment;
import com.wpc.library.okhttp.OkHttpUtils;
 
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
 
/**
 * 通用模版
 */
public class GmTemplateFragment extends RetainViewFragment implements View.OnClickListener {
    private TabPageIndicator_Gm gmtemplate_indicator;//指示器
    private ViewPager gmtemplate_Pager;
 
    private String key;
    private String from;
    private int type = 0;//上级传过来的type  默认为0 表示未传递
    private GmTemplateTabAdapter adapter;
    /**
     * 导航栏标题
     */
    private ArrayList<Map<String, String>> navtitle = new ArrayList<>();
 
    String tag = "commoncontent/getNavList";
 
    @Override
    public int getContentResource() {
        return R.layout.activity_gmtemplate;
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        getActivity().setTheme(R.style.AppTabTheme5);
        initView(contentView);
        key = getArguments().getString("key");
        from = getArguments().getString("from");
        String types = getArguments().getString("type");
        if (types != null && !types.equals("")) {//上级传过来的type
            double aDouble = Double.valueOf(types);
            type = (int) aDouble;//默认为0 表示未传递
        }
        GoodsCustomEvent.goodsListCommonTemplate(getContext(), types);
        FragmentManager fm = getChildFragmentManager();
        adapter = new GmTemplateTabAdapter(fm, navtitle, key, type + "", from);
        gmtemplate_Pager.setAdapter(adapter);
        gmtemplate_indicator.setImgList(imgList);
        gmtemplate_indicator.setViewPager(gmtemplate_Pager);
        getNavList(key);//获取 指示器数据
    }
 
 
    /**
     * 控件初始化
     */
    private void initView(View view) {
        view.findViewById(R.id.ll_top).setVisibility(View.GONE);
        gmtemplate_indicator = view.findViewById(R.id.gmtemplate_indicator);
        gmtemplate_Pager = view.findViewById(R.id.gmtemplate_Pager);
    }
 
    /**
     * 单击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }
 
    List<ImagePath> imgList = new ArrayList<>();
 
    /**
     * 获取通用模板的导航栏
     *
     * @param title 上级传过来的关键词
     */
    private void getNavList(String title) {
        ShoppingApi.getNavList(getContext(), title, type + "", new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optInt("code") == 0) {
                    if (navtitle != null && navtitle.size() > 0) {
                        navtitle.clear();
                    }
                    if (imgList != null && imgList.size() > 0) {
                        imgList.clear();
                    }
                    JSONArray json = jsonObject.optJSONArray("data");
                    //当data数组为0时表示没有导航栏
                    if (json == null || json.length() == 0) {
                        gmtemplate_indicator.setVisibility(View.GONE);
                        Map<String, String> map = new HashMap<>();
                        map.put("name", "");
                        map.put("cid", "");
                        navtitle.add(map);
                    } else {//有导航栏
                        gmtemplate_indicator.setVisibility(View.VISIBLE);
                        for (int i = 0; i < json.length(); i++) {
                            Map<String, String> map = new HashMap<>();
                            ImagePath info = new ImagePath();
                            map.put("name", ((JSONObject) json.get(i)).optString("name", ""));
                            map.put("cid", ((JSONObject) json.get(i)).optString("cid", ""));
                            navtitle.add(map);
                            info.setImgUrl(((JSONObject) json.get(i)).optString("picture", ""));
                            imgList.add(info);
                        }
                    }
                    adapter.notifyDataSetChanged();
                    gmtemplate_indicator.notifyDataSetChanged();
                } else {
//                    Toast.makeText(GmTemplate.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                }
            }
 
            //不论请求是否在服务器存在 都会回调
            @Override
            public void onFinish() {
                super.onFinish();
            }
 
        });
    }
 
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        gmtemplate_indicator = null;//指示器
        gmtemplate_Pager = null;
        key = null;
        type = 0;//上级传过来的type  默认为0 表示未传递
        adapter = null;
        OkHttpUtils.getInstance().cancelTag(tag);
    }
}