admin
2021-05-17 06a80d5c4b3a971cdc1ca3d91717ec3f6e03a443
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.tejia.lijin.app.ui.gmtemplate;
 
import android.os.Build;
import android.os.Bundle;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
 
import com.viewpagerindicator.ImagePath;
import com.viewpagerindicator.TabPageIndicator_Gm;
import com.wpc.library.okhttp.OkHttpUtils;
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 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;
 
/**
 * 通用模版
 */
public class GmTemplate extends BaseFragmentActivity implements View.OnClickListener {
    private TextView tv_top_bar_left;//返回
    private TextView tv_top_bar_middle;//title
    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
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gmtemplate);
        setTheme(R.style.AppTabTheme5);
        //设置状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //设置状态栏颜色为白色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.setStatusBarColor(getResources().getColor(R.color.white));
            }
            //设置状态栏文字颜色及图标为深色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        inintview();
        setOnClick();
        key = getIntent().getStringExtra("key");
        from = getIntent().getStringExtra("from");
        String types = getIntent().getStringExtra("type");
        if (types != null && !types.equals("")) {//上级传过来的type
            double aDouble = Double.valueOf(types);
            type = (int) aDouble;//默认为0 表示未传递
        }
        GoodsCustomEvent.goodsListCommonTemplate(this, types);
        FragmentManager fm = getSupportFragmentManager();
        adapter = new GmTemplateTabAdapter(fm, navtitle, key, type + "",from);
        gmtemplate_Pager.setAdapter(adapter);
        gmtemplate_indicator.setImgList(imgList);
        gmtemplate_indicator.setViewPager(gmtemplate_Pager);
 
        if (key == null || key.equals("")) {
            finish();
        } else {
            getNavList(key);//获取 指示器数据
        }
    }
 
 
    /**
     * 控件初始化
     */
    private void inintview() {
        tv_top_bar_left = findViewById(R.id.tv_top_bar_left);
        tv_top_bar_middle = findViewById(R.id.tv_top_bar_middle);
        gmtemplate_indicator = findViewById(R.id.gmtemplate_indicator);
        gmtemplate_Pager = findViewById(R.id.gmtemplate_Pager);
    }
 
    /**
     * 设置点击监听
     */
    private void setOnClick() {
        tv_top_bar_left.setOnClickListener(this);
    }
 
    /**
     * 单击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_top_bar_left:
                finish();
                break;
 
            default:
                break;
        }
    }
 
    List<ImagePath> imgList = new ArrayList<>();
 
    /**
     * 获取通用模板的导航栏
     *
     * @param title 上级传过来的关键词
     */
    private void getNavList(String title) {
        ShoppingApi.getNavList(this, title, type + "", new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optInt("code") == 0) {
                    tv_top_bar_middle.setText(getIntent().getStringExtra("title"));//设置标题
                    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
    protected void onDestroy() {
        super.onDestroy();
        gmtemplate_indicator = null;//指示器
        gmtemplate_Pager = null;
        key = null;
        type = 0;//上级传过来的type  默认为0 表示未传递
        adapter = null;
        OkHttpUtils.getInstance().cancelTag(tag);
    }
}