package com.tejia.lijin.app.ui.category;
|
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.provider.Settings;
|
import androidx.recyclerview.widget.DefaultItemAnimator;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.view.View;
|
import android.widget.AdapterView;
|
import android.widget.LinearLayout;
|
import android.widget.ListView;
|
import android.widget.TextView;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.wpc.library.content.ConnectivityChangeHelper;
|
import com.wpc.library.okhttp.OkHttpUtils;
|
import com.wpc.library.util.NetUtils;
|
import com.wpc.library.util.common.StringUtils;
|
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.entity.FirstCategory;
|
import com.tejia.lijin.app.entity.SecondCategory;
|
import com.tejia.lijin.app.entity.SecondCategoryMax;
|
import com.tejia.lijin.app.ui.BaseActivity;
|
import com.tejia.lijin.app.ui.recommend.SearchActivity;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2017/6/19.
|
*/
|
|
public class CategoryTypeActivity extends BaseActivity implements View.OnClickListener {
|
|
private TextView tv_left;
|
|
private TextView tv_middle;
|
|
private ListView lv_category_max;
|
|
private RecyclerView recyclerView;
|
|
private List<FirstCategory> fList = new ArrayList<>();
|
private FirstCategoryAdapter1 adapter1;
|
private LinearLayout ll_no_net, ll_no_data, ll_request_failture, ll_content;
|
|
private ConnectivityChangeHelper mChangeHelper;
|
GridLayoutManager linearLayoutManager;
|
private String jumpcalss = "";//设置跳转分类
|
|
private String tag1 = "class/listSubMap";
|
private String tag2 = "class/listClass";
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_category_type);
|
TopStatusSettings.setStatusViewAndDeepColor(this);
|
tv_left = findViewById(R.id.tv_top_bar_left);
|
tv_middle = findViewById(R.id.tv_top_bar_middle);
|
lv_category_max = findViewById(R.id.lv_category_max);
|
recyclerView = findViewById(R.id.recyclerView);
|
ll_no_net = findViewById(R.id.ll_no_net);
|
ll_no_data = findViewById(R.id.ll_no_data);
|
ll_request_failture = findViewById(R.id.ll_request_failture);
|
ll_content = findViewById(R.id.ll_content);
|
findViewById(R.id.tv_net_setting).setOnClickListener(this);
|
findViewById(R.id.tv_refresh).setOnClickListener(this);
|
tv_middle.setText("分类");
|
|
lv_category_max.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
@Override
|
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
|
if (mList != null & mList.size() > 0) { //防止在网络差没有获取到数据的情况下下标越位报错
|
adapter1.getPosition(position);
|
mList.clear();
|
getSecondCategory(fList.get(position).getId());
|
}
|
}
|
});
|
tv_left.setOnClickListener(this);
|
findViewById(R.id.fl_search).setOnClickListener(this);
|
mChangeHelper = new ConnectivityChangeHelper(this,
|
new ConnectivityChangeHelper.OnConnectivityChangeListener() {
|
|
@Override
|
public void onNetworkUnAvailable() {
|
if (fList.size()==0)
|
requestState(3);
|
}
|
|
@Override
|
public void onNetworkAvailable() {
|
requestState(0);
|
if (fList.size() == 0) {
|
getFirstCategory();
|
}
|
}
|
});
|
|
recyclerView = findViewById(R.id.recyclerView);
|
linearLayoutManager = new GridLayoutManager(this, 3);
|
linearLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
@Override
|
public int getSpanSize(int position) {
|
return mList.get(position).isTitle() ? 3 : 1;
|
}
|
});
|
jumpcalss = getIntent().getStringExtra("jumpcalss");//接收 指定跳转分类
|
}
|
|
/**
|
* 使指定的项平滑到顶部
|
*
|
* @param position 待指定的项
|
*/
|
private void smoothMoveToPosition(final int position) {
|
int firstItemPosition = -1;
|
int lastItemPosition = -1;
|
|
//todo 获取第一个和最后一个可见位置方式1
|
// 第一个可见位置
|
firstItemPosition = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0));
|
// 最后一个可见位置
|
lastItemPosition = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(recyclerView.getChildCount() - 1));
|
|
//todo 获取第一个和最后一个可见位置方式2
|
// 判断是当前layoutManager是否为LinearLayoutManager
|
|
|
if (position < firstItemPosition) {
|
// 第一种可能:跳转位置在第一个可见位置之前
|
recyclerView.smoothScrollToPosition(position);
|
} else if (position <= lastItemPosition) {
|
// 第二种可能:跳转位置在第一个可见位置之后,在最后一个可见项之前
|
int movePosition = position - firstItemPosition;
|
if (movePosition >= 0 && movePosition < recyclerView.getChildCount()) {
|
int top = recyclerView.getChildAt(movePosition).getTop();
|
recyclerView.smoothScrollBy(0, top);//dx>0===>向左 dy>0====>向上
|
}
|
} else {
|
// 第三种可能:跳转位置在最后可见项之后
|
recyclerView.smoothScrollToPosition(position);
|
}
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
mChangeHelper.registerReceiver();
|
}
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
mChangeHelper.unregisterReceiver();
|
}
|
|
List<SecondCategory> mList = new ArrayList<>();
|
|
/**
|
* 二级分类
|
*/
|
private void getSecondCategory(String cid) {
|
ShoppingApi.getSecondCategory42(tv_left.getContext(), cid, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
List<SecondCategoryMax> list = gson.fromJson(
|
jsonObject.optJSONObject("data").optJSONArray("list").toString(),
|
new TypeToken<List<SecondCategoryMax>>() {
|
}.getType());
|
for (int i = 0; i < list.size(); i++) {
|
SecondCategory info = new SecondCategory();
|
info.setPos(i);
|
info.setTitle(true);
|
list.get(i).getListSub().add(0, info);
|
|
for (int j = 0; j < list.get(i).getListSub().size(); j++) {
|
list.get(i).getListSub().get(j).setTag(list.get(i).getName());
|
|
if (j == list.get(i).getListSub().size() - 1) {
|
list.get(i).getListSub().get(j).setTag("end");
|
}
|
// Log.e("mResult", list.get(i).getListSub().get(j).getTag());
|
}
|
mList.addAll(list.get(i).getListSub());
|
}
|
SecondCategory bottom = new SecondCategory();
|
bottom.setTitle(true);
|
bottom.setPos(-1);
|
mList.add(bottom);
|
// headerDecoration = new ItemHeaderDecoration(CategoryTypeActivity.this, mList);
|
// recyclerView.addItemDecoration(headerDecoration);
|
recyclerView.setLayoutManager(linearLayoutManager);
|
recyclerView.setItemAnimator(new DefaultItemAnimator());
|
CategoryMinAdapter adapter = new CategoryMinAdapter(CategoryTypeActivity.this, mList);
|
recyclerView.setAdapter(adapter);
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
}
|
});
|
}
|
|
private void getFirstCategory() {
|
ShoppingApi.getFirstCategory2(tv_left.getContext(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
if (fList.size() > 0)
|
fList.clear();
|
fList = gson.fromJson(
|
jsonObject.optJSONObject("data")
|
.optJSONArray("list").toString(),
|
new TypeToken<List<FirstCategory>>() {
|
}.getType());
|
adapter1 = new FirstCategoryAdapter1(tv_left.getContext(), fList);
|
lv_category_max.setAdapter(adapter1);
|
if (fList.size() > 0) {
|
if (!StringUtils.isEmpty(jumpcalss)) {
|
jumpclassification();//跳转分类
|
} else {
|
getSecondCategory(fList.get(0).getId());
|
}
|
}
|
requestState(fList.size() == 0 ? 1 : 0);
|
|
} else {
|
if (fList.size() == 0)
|
requestState(2);
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
if (fList.size() == 0)
|
if (NetUtils.getNetworkState(CategoryTypeActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
requestState(3);
|
} else {
|
requestState(2);
|
}
|
}
|
});
|
}
|
|
/**
|
* 跳转 分类项目
|
*/
|
private void jumpclassification() {
|
//跳转字段不为空
|
if (jumpcalss != null && !jumpcalss.equals("") && fList.size() > 0) {
|
lv_category_max.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
for (int i = 0; i < fList.size(); i++) {
|
if (fList.get(i).getName().equals(jumpcalss)) {
|
lv_category_max.smoothScrollToPositionFromTop(i, 0);
|
adapter1.getPosition(i);
|
mList.clear();
|
getSecondCategory(fList.get(i).getId());
|
break;
|
}
|
}
|
}
|
}, 300);
|
|
}
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
case R.id.fl_search:
|
Intent intent = new Intent(this, SearchActivity.class);
|
startActivity(intent);
|
break;
|
case R.id.tv_net_setting:
|
startActivity(new Intent(Settings.ACTION_SETTINGS));
|
break;
|
case R.id.tv_refresh:
|
// getFirstCategory();
|
// setHotData();
|
startActivity(new Intent(Settings.ACTION_SETTINGS));
|
break;
|
}
|
}
|
|
/**
|
* 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
|
*
|
* @param state
|
*/
|
private void requestState(int state) {
|
ll_content.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
|
ll_no_data.setVisibility(state == 1 ? View.VISIBLE : View.GONE);
|
ll_request_failture.setVisibility(state == 2 ? View.VISIBLE : View.GONE);
|
ll_no_net.setVisibility(state == 3 ? View.VISIBLE : View.GONE);
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
OkHttpUtils.getInstance().cancelTag(tag1);
|
OkHttpUtils.getInstance().cancelTag(tag2);
|
}
|
}
|