986 lines
30 KiB
Java
986 lines
30 KiB
Java
package com.gh.gamecenter.gamedetails;
|
|
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v4.app.Fragment;
|
|
import android.support.v4.util.ArrayMap;
|
|
import android.support.v7.widget.GridLayoutManager;
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.text.Editable;
|
|
import android.text.Html;
|
|
import android.text.Spanned;
|
|
import android.text.TextUtils;
|
|
import android.text.TextWatcher;
|
|
import android.view.KeyEvent;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.View.OnClickListener;
|
|
import android.view.ViewGroup;
|
|
import android.view.ViewGroup.LayoutParams;
|
|
import android.view.inputmethod.EditorInfo;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.widget.AbsListView.OnScrollListener;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageView;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.ProgressBar;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
import android.widget.TextView.OnEditorActionListener;
|
|
import android.widget.Toast;
|
|
|
|
import com.android.volley.NoConnectionError;
|
|
import com.android.volley.Response;
|
|
import com.android.volley.TimeoutError;
|
|
import com.android.volley.VolleyError;
|
|
import com.gh.base.AppController;
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.util.DataUtils;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.NewsUtils;
|
|
import com.gh.common.util.Utils;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.entity.NewsEntity;
|
|
import com.gh.gamecenter.eventbus.EBMoveTop;
|
|
import com.gh.gamecenter.eventbus.EBNetworkState;
|
|
import com.gh.gamecenter.eventbus.EBNewsType;
|
|
import com.gh.gamecenter.eventbus.EBSlide;
|
|
import com.gh.gamecenter.manager.DataCollectionManager;
|
|
import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
|
|
import java.lang.reflect.Type;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import de.greenrobot.event.EventBus;
|
|
|
|
/**
|
|
* 游戏攻略fragment
|
|
*
|
|
* @author 黄壮华
|
|
*
|
|
*/
|
|
public class GameStrategyFragment extends Fragment {
|
|
|
|
private View view;
|
|
private RecyclerView fm_gamestrategy_rv_show;
|
|
private LinearLayoutManager linearLayoutManager;
|
|
private GameStrategyAdapter adapter;
|
|
private LinearLayout fm_gamestrategy_ll_loading, reuse_no_connection;
|
|
private RelativeLayout.LayoutParams rparams;
|
|
|
|
private ArrayMap<String, GameStrategyAdapter> adapterMap;
|
|
private List<String> typeList;
|
|
|
|
private String id;
|
|
private String type = "全部";
|
|
private String keyword;
|
|
private String gameName;
|
|
private String entrance;
|
|
|
|
private int dis;
|
|
private int distance;
|
|
private int currentPosition = 0;
|
|
private int column;
|
|
|
|
private boolean isNull = false;
|
|
private boolean isLoading = false;
|
|
private boolean isNetworkError = false;
|
|
private boolean isDestroy = false;
|
|
|
|
public void setParams(String id, String gameName, String entrance) {
|
|
this.id = id;
|
|
this.gameName = gameName;
|
|
this.entrance = entrance;
|
|
}
|
|
|
|
@Override
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
Utils.log(this.getClass().getSimpleName() + " onSaveInstanceState");
|
|
outState.putString("id", id);
|
|
outState.putString("gameName", gameName);
|
|
outState.putString("entrance", entrance);
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
if (savedInstanceState != null) {
|
|
id = savedInstanceState.getString("id");
|
|
gameName = savedInstanceState.getString("gameName");
|
|
entrance = savedInstanceState.getString("entrance");
|
|
}
|
|
|
|
view = View.inflate(getActivity(), R.layout.gamestrategy, null);
|
|
|
|
distance = DisplayUtils.dip2px(getActivity(), 66);
|
|
typeList = new ArrayList<String>();
|
|
adapterMap = new ArrayMap<String, GameStrategyAdapter>();
|
|
|
|
SharedPreferences sp = getActivity().getSharedPreferences(
|
|
Config.PREFERENCE, Context.MODE_PRIVATE);
|
|
column = sp.getInt("game_detail_news_type_tab_column", 4);
|
|
|
|
fm_gamestrategy_ll_loading = (LinearLayout) view
|
|
.findViewById(R.id.fm_gamestrategy_ll_loading);
|
|
|
|
reuse_no_connection = (LinearLayout) view
|
|
.findViewById(R.id.reuse_no_connection);
|
|
reuse_no_connection.setOnClickListener(new OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
fm_gamestrategy_rv_show.setVisibility(View.GONE);
|
|
fm_gamestrategy_ll_loading.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
getGameInformation(0, true);
|
|
}
|
|
});
|
|
|
|
rparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
|
|
LayoutParams.WRAP_CONTENT);
|
|
|
|
fm_gamestrategy_rv_show = (RecyclerView) view
|
|
.findViewById(R.id.fm_gamestrategy_rv_show);
|
|
fm_gamestrategy_rv_show.setHasFixedSize(true);
|
|
linearLayoutManager = new LinearLayoutManager(getActivity());
|
|
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
|
fm_gamestrategy_rv_show.setLayoutManager(linearLayoutManager);
|
|
fm_gamestrategy_rv_show
|
|
.setOnScrollListener(new GameStrategyOnScrollListener());
|
|
|
|
EventBus.getDefault().register(this);
|
|
|
|
getGameInformation(0, true);
|
|
}
|
|
|
|
@Override
|
|
@Nullable
|
|
public View onCreateView(LayoutInflater inflater,
|
|
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
if (container != null) {
|
|
container.removeView(view);
|
|
}
|
|
return view;
|
|
}
|
|
|
|
private class GameStrategyOnScrollListener extends
|
|
RecyclerView.OnScrollListener {
|
|
|
|
@Override
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
|
|
super.onScrolled(recyclerView, dx, dy);
|
|
|
|
int visibility;
|
|
if (linearLayoutManager.findFirstVisibleItemPosition() >= 1) {
|
|
visibility = View.VISIBLE;
|
|
} else {
|
|
visibility = View.GONE;
|
|
}
|
|
|
|
dis += dy;
|
|
|
|
rparams.topMargin -= dy;
|
|
|
|
EventBus.getDefault().post(
|
|
new EBMoveTop("top", visibility, rparams.topMargin));
|
|
}
|
|
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
if (newState == OnScrollListener.SCROLL_STATE_IDLE) {
|
|
int position = adapter.getNewsList().size() + 4;
|
|
if (!Config.isShow) {
|
|
position--;
|
|
}
|
|
if (linearLayoutManager.findLastVisibleItemPosition() == position
|
|
&& !adapter.isRemove() && !isLoading) {
|
|
if (currentPosition == -1) {
|
|
searchFormServer(adapter.getNewsList().size());
|
|
} else {
|
|
getGameInformation(adapter.getNewsList().size());
|
|
}
|
|
}
|
|
|
|
if (dis > distance) {
|
|
EventBus.getDefault().post(new EBSlide(distance, 0, false));
|
|
EventBus.getDefault().post(new EBSlide(distance, 1, false));
|
|
} else if (dis < -distance) {
|
|
EventBus.getDefault().post(new EBSlide(0, 0, false));
|
|
EventBus.getDefault().post(new EBSlide(0, 1, false));
|
|
} else {
|
|
EventBus.getDefault().post(new EBSlide(dis, 0, false));
|
|
EventBus.getDefault().post(new EBSlide(dis, 1, false));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBSlide busOne) {
|
|
if (busOne.getPosition() == 2) {
|
|
if (busOne.isReset()) {
|
|
fm_gamestrategy_rv_show.scrollBy(0, distance - dis);
|
|
} else {
|
|
if (busOne.getDistance() == 0) {
|
|
fm_gamestrategy_rv_show.scrollToPosition(0);
|
|
rparams.topMargin = 0;
|
|
dis = 0;
|
|
} else {
|
|
fm_gamestrategy_rv_show.scrollBy(0, busOne.getDistance()
|
|
- dis);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBNetworkState busNetworkState) {
|
|
if (busNetworkState.isNetworkConnected()) {
|
|
if (reuse_no_connection.getVisibility() == View.VISIBLE) {
|
|
fm_gamestrategy_rv_show.setVisibility(View.GONE);
|
|
fm_gamestrategy_ll_loading.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
getGameInformation(0, true);
|
|
} else if (isNetworkError) {
|
|
isNetworkError = false;
|
|
adapter.notifyItemChanged(adapter.getItemCount() - 2);
|
|
if (currentPosition == -1) {
|
|
searchFormServer(adapter.getNewsList().size());
|
|
} else {
|
|
getGameInformation(adapter.getNewsList().size());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBNewsType type) {
|
|
if ("攻略".equals(type.getType())) {
|
|
typeList = type.getList();
|
|
adapter.notifyItemChanged(2);
|
|
}
|
|
}
|
|
|
|
private void getGameInformation(final int offset) {
|
|
getGameInformation(offset, false);
|
|
}
|
|
|
|
private void getGameInformation(final int offset, final boolean isFirst) {
|
|
if (isLoading) {
|
|
return;
|
|
}
|
|
isLoading = true;
|
|
if (offset == 0) {
|
|
adapter = adapterMap.get(type);
|
|
if (adapter == null) {
|
|
adapter = new GameStrategyAdapter(new ArrayList<NewsEntity>());
|
|
adapterMap.put(type, adapter);
|
|
}
|
|
fm_gamestrategy_rv_show.setAdapter(adapter);
|
|
}
|
|
|
|
final String url;
|
|
|
|
if ("全部".equals(type)) {
|
|
url = Config.HOST + "v1d45/game/" + id + "/news?limit=20&offset="
|
|
+ offset + "&type_group=" + Uri.encode("攻略");
|
|
} else {
|
|
url = Config.HOST + "v1d45/game/" + id + "/news?limit=20&offset="
|
|
+ offset + "&type=" + Uri.encode(type);
|
|
}
|
|
|
|
JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(url,
|
|
new Response.Listener<JSONArray>() {
|
|
|
|
@Override
|
|
public void onResponse(JSONArray response) {
|
|
isLoading = false;
|
|
if (!isDestroy) {
|
|
try {
|
|
processingData(response, offset, isFirst);
|
|
// adapter.setLoaded(true);
|
|
// adapter.notifyItemChanged(adapter.getItemCount() - 2);
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}, new Response.ErrorListener() {
|
|
|
|
@Override
|
|
public void onErrorResponse(VolleyError error) {
|
|
isLoading = false;
|
|
// 无网络连接和访问超时
|
|
if (error.getClass().equals(NoConnectionError.class)
|
|
|| error.getClass().equals(TimeoutError.class)) {
|
|
if (!isDestroy) {
|
|
if (offset == 0 && isFirst) {
|
|
fm_gamestrategy_rv_show
|
|
.setVisibility(View.GONE);
|
|
fm_gamestrategy_ll_loading
|
|
.setVisibility(View.GONE);
|
|
reuse_no_connection
|
|
.setVisibility(View.VISIBLE);
|
|
} else {
|
|
Toast.makeText(getActivity(), "网络错误",
|
|
Toast.LENGTH_SHORT).show();
|
|
isNetworkError = true;
|
|
adapter.notifyItemChanged(adapter
|
|
.getItemCount() - 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
if ((adapter.getNewsList().isEmpty() && !adapter.isRemove() && offset == 0)
|
|
|| (offset != 0)) {
|
|
AppController
|
|
.addToRequestQueue(request, GameStrategyFragment.class);
|
|
}
|
|
}
|
|
|
|
private void processingData(JSONArray response, int offset, boolean isFirst)
|
|
throws JSONException {
|
|
if (offset == 0) {
|
|
fm_gamestrategy_ll_loading.setVisibility(View.GONE);
|
|
fm_gamestrategy_rv_show.setVisibility(View.VISIBLE);
|
|
}
|
|
Type listType = new TypeToken<ArrayList<NewsEntity>>() {}.getType();
|
|
Gson gson = new Gson();
|
|
List<NewsEntity> newsList = gson.fromJson(response.toString(), listType);
|
|
int size = response.length();
|
|
adapter.getNewsList().addAll(newsList);
|
|
adapter.notifyItemRangeInserted(adapter.getNewsList().size() - size + 3, size);
|
|
if (size < 20) {
|
|
adapter.setRemove(true);
|
|
adapter.notifyItemRemoved(adapter.getNewsList().size() + 3);
|
|
adapter.notifyItemChanged(adapter.getNewsList().size() + 3);
|
|
if (isFirst && size == 0 && !isNull) {
|
|
isNull = true;
|
|
adapter.notifyItemRemoved(2);
|
|
}
|
|
}
|
|
if (size == 0 && dis != 0) {
|
|
int dy = DisplayUtils.dip2px(getActivity(), 35);
|
|
dis -= dy;
|
|
rparams.topMargin += dy;
|
|
EventBus.getDefault().post(
|
|
new EBMoveTop("top", View.VISIBLE, rparams.topMargin));
|
|
}
|
|
}
|
|
|
|
private class GameStrategyViewHolder extends RecyclerView.ViewHolder
|
|
implements OnClickListener, TextWatcher, OnEditorActionListener {
|
|
|
|
private EditText item_et_search;
|
|
private ImageView item_iv_delete;
|
|
private TextView item_tv_search;
|
|
private RecyclerView item_rv_select;
|
|
private LinearLayout item_ll_search_result;
|
|
private TextView item_tv_search_result;
|
|
private ImageView item_iv_search_result;
|
|
|
|
private TextView information_type, information_title;
|
|
private View itemView;
|
|
|
|
private ProgressBar item_pb_loading;
|
|
private TextView item_tv_loading;
|
|
|
|
public GameStrategyViewHolder(View convertView) {
|
|
super(convertView);
|
|
|
|
itemView = convertView;
|
|
int position = (Integer) convertView.getTag();
|
|
if (!isNull && position == 2) {
|
|
item_et_search = (EditText) convertView
|
|
.findViewById(R.id.item_et_search);
|
|
item_et_search.addTextChangedListener(this);
|
|
item_et_search.setOnEditorActionListener(this);
|
|
item_iv_delete = (ImageView) convertView
|
|
.findViewById(R.id.item_iv_delete);
|
|
item_iv_delete.setOnClickListener(this);
|
|
item_tv_search = (TextView) convertView
|
|
.findViewById(R.id.item_tv_search);
|
|
item_tv_search.setOnClickListener(this);
|
|
item_rv_select = (RecyclerView) convertView
|
|
.findViewById(R.id.item_rv_select);
|
|
item_rv_select.setLayoutManager(new GridLayoutManager(
|
|
getActivity(), column));
|
|
item_rv_select.setAdapter(new SelectViewAdapter());
|
|
item_ll_search_result = (LinearLayout) convertView
|
|
.findViewById(R.id.item_ll_search_result);
|
|
item_tv_search_result = (TextView) convertView
|
|
.findViewById(R.id.item_tv_search_result);
|
|
item_iv_search_result = (ImageView) convertView
|
|
.findViewById(R.id.item_iv_search_result);
|
|
item_iv_search_result.setOnClickListener(this);
|
|
} else if (position >= 3
|
|
&& position <= adapter.getNewsList().size() + 2) {
|
|
information_type = (TextView) convertView
|
|
.findViewById(R.id.information_type);
|
|
information_title = (TextView) convertView
|
|
.findViewById(R.id.information_title);
|
|
convertView.setOnClickListener(this);
|
|
} else if (!adapter.isRemove()
|
|
&& position == adapter.getNewsList().size() + 3) {
|
|
item_pb_loading = (ProgressBar) convertView
|
|
.findViewById(R.id.item_pb_loading);
|
|
item_tv_loading = (TextView) convertView
|
|
.findViewById(R.id.item_tv_loading);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (v == itemView) {
|
|
NewsEntity newsEntity = adapter.getNewsList().get(
|
|
getPosition() - 3);
|
|
|
|
Map<String, Object> kv = new HashMap<String, Object>();
|
|
kv.put("文章类型", newsEntity.getType());
|
|
kv.put("入口", "游戏详情-攻略");
|
|
DataUtils.onEvent(getActivity(), "文章阅读-攻略",
|
|
newsEntity.getTitle(), kv);
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("location", "攻略");
|
|
map.put("createOn", System.currentTimeMillis() / 1000);
|
|
map.put("game", gameName);
|
|
map.put("news", newsEntity.getTitle());
|
|
map.put("page", "游戏详情");
|
|
DataCollectionManager.onEvent(getActivity(), "click-item", map);
|
|
|
|
//统计阅读量
|
|
NewsUtils.statNewsViews(newsEntity.getId());
|
|
NewsUtils.startNewsActivity(getActivity(), newsEntity, entrance
|
|
+ "-游戏详情-攻略");
|
|
}
|
|
if (v == item_tv_search) {
|
|
InputMethodManager imm = (InputMethodManager) getActivity()
|
|
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
|
if (!TextUtils.isEmpty(item_et_search.getText().toString())) {
|
|
keyword = item_et_search.getText().toString();
|
|
|
|
Map<String, Object> kv = new HashMap<String, Object>();
|
|
kv.put("关键字", keyword);
|
|
DataUtils.onEvent(getActivity(), "搜索-游戏详情", gameName, kv);
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("key", keyword);
|
|
map.put("from", "游戏详情-攻略");
|
|
map.put("createdOn", System.currentTimeMillis() / 1000);
|
|
DataCollectionManager.onEvent(getActivity(), "search", map);
|
|
|
|
searchFormServer(0);
|
|
} else {
|
|
Toast.makeText(getActivity(), "请输入搜索关键字",
|
|
Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
if (v == item_iv_delete) {
|
|
item_et_search.setText("");
|
|
}
|
|
if (v == item_iv_search_result) {
|
|
InputMethodManager imm = (InputMethodManager) getActivity()
|
|
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
|
EventBus.getDefault().post(new EBSlide(0, 0, false));
|
|
EventBus.getDefault().post(new EBSlide(0, 1, false));
|
|
fm_gamestrategy_rv_show.scrollBy(0, rparams.topMargin);
|
|
currentPosition = 0;
|
|
adapter = adapterMap.get("全部");
|
|
fm_gamestrategy_rv_show.setAdapter(adapter);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void beforeTextChanged(CharSequence s, int start, int count,
|
|
int after) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence s, int start, int before,
|
|
int count) {
|
|
if (s.length() > 0) {
|
|
item_iv_delete.setVisibility(View.VISIBLE);
|
|
} else {
|
|
item_iv_delete.setVisibility(View.GONE);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
InputMethodManager imm = (InputMethodManager) getActivity()
|
|
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
|
if (!TextUtils.isEmpty(item_et_search.getText().toString())) {
|
|
keyword = item_et_search.getText().toString();
|
|
|
|
Map<String, Object> kv = new HashMap<String, Object>();
|
|
kv.put("关键字", keyword);
|
|
DataUtils.onEvent(getActivity(), "搜索-游戏详情", gameName, kv);
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("key", keyword);
|
|
map.put("from", "游戏详情-攻略");
|
|
map.put("createdOn", System.currentTimeMillis() / 1000);
|
|
DataCollectionManager.onEvent(getActivity(), "search", map);
|
|
|
|
searchFormServer(0);
|
|
} else {
|
|
Toast.makeText(getActivity(), "请输入搜索关键字",
|
|
Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
private void searchFormServer(int offset) {
|
|
if (isLoading) {
|
|
return;
|
|
}
|
|
isLoading = true;
|
|
if (offset == 0) {
|
|
EventBus.getDefault().post(new EBSlide(0, 0, false));
|
|
EventBus.getDefault().post(new EBSlide(0, 1, false));
|
|
fm_gamestrategy_rv_show.scrollBy(0, rparams.topMargin);
|
|
|
|
currentPosition = -1;
|
|
|
|
adapter = new GameStrategyAdapter(new ArrayList<NewsEntity>());
|
|
fm_gamestrategy_rv_show.setAdapter(adapter);
|
|
}
|
|
|
|
final String url = Config.HOST + "v1d45/search/news?type_group="
|
|
+ Uri.encode("攻略") + "&game_id=" + id + "&keyword="
|
|
+ Uri.encode(keyword) + "&limit=20&offset=" + offset;
|
|
JsonArrayExtendedRequest searchRequest = new JsonArrayExtendedRequest(
|
|
url, new Response.Listener<JSONArray>() {
|
|
|
|
@Override
|
|
public void onResponse(JSONArray response) {
|
|
isLoading = false;
|
|
if (!isDestroy) {
|
|
try {
|
|
processingSearchData(response);
|
|
// adapter.setLoaded(true);
|
|
// adapter.notifyItemChanged(adapter.getItemCount() - 2);
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}, new Response.ErrorListener() {
|
|
|
|
@Override
|
|
public void onErrorResponse(VolleyError error) {
|
|
isLoading = false;
|
|
// 无网络连接和访问超时
|
|
if (error.getClass().equals(NoConnectionError.class)
|
|
|| error.getClass().equals(TimeoutError.class)) {
|
|
if (!isDestroy) {
|
|
Toast.makeText(getActivity(), "网络错误",
|
|
Toast.LENGTH_SHORT).show();
|
|
isNetworkError = true;
|
|
adapter.notifyItemChanged(adapter
|
|
.getItemCount() - 2);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
AppController.addToRequestQueue(searchRequest,
|
|
GameStrategyFragment.class);
|
|
}
|
|
|
|
private void processingSearchData(JSONArray response) throws JSONException {
|
|
Type listType = new TypeToken<ArrayList<NewsEntity>>() {}.getType();
|
|
Gson gson = new Gson();
|
|
List<NewsEntity> newsList = gson.fromJson(response.toString(), listType);
|
|
int size = response.length();
|
|
adapter.getNewsList().addAll(newsList);
|
|
adapter.notifyItemRangeInserted(adapter.getNewsList().size() - size + 3, size);
|
|
if (size < 20) {
|
|
adapter.setRemove(true);
|
|
adapter.notifyItemRemoved(adapter.getNewsList().size() + 3);
|
|
adapter.notifyItemChanged(adapter.getNewsList().size() + 3);
|
|
}
|
|
adapter.notifyItemChanged(2);
|
|
}
|
|
|
|
private class GameStrategyAdapter extends
|
|
RecyclerView.Adapter<GameStrategyViewHolder> {
|
|
|
|
private ArrayList<NewsEntity> newsList;
|
|
private boolean isRemove;
|
|
// private boolean isLoaded;
|
|
|
|
public GameStrategyAdapter(ArrayList<NewsEntity> newsList) {
|
|
this.newsList = newsList;
|
|
isRemove = false;
|
|
// isLoaded = false;
|
|
}
|
|
|
|
public ArrayList<NewsEntity> getNewsList() {
|
|
return newsList;
|
|
}
|
|
|
|
// public void setLoaded(boolean isLoaded) {
|
|
// this.isLoaded = isLoaded;
|
|
// }
|
|
|
|
public boolean isRemove() {
|
|
return isRemove;
|
|
}
|
|
|
|
public void setRemove(boolean isRemove) {
|
|
this.isRemove = isRemove;
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
if (isNull) {
|
|
return 3;
|
|
}
|
|
if (isRemove) {
|
|
return newsList.size() + 4;
|
|
}
|
|
return newsList.size() + 5;
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(GameStrategyViewHolder viewHolder,
|
|
int position) {
|
|
|
|
if (!isNull && position == 2) {
|
|
if (currentPosition == -1) {
|
|
viewHolder.item_et_search.setText(keyword);
|
|
viewHolder.item_et_search.setSelection(keyword.length());
|
|
viewHolder.item_et_search.requestFocus();
|
|
Spanned content = Html.fromHtml("与“<font color='#1BA4FC'>"
|
|
+ keyword + "</font>”相关的资讯共"
|
|
+ adapter.getNewsList().size() + "条");
|
|
viewHolder.item_tv_search_result.setText(content);
|
|
viewHolder.item_rv_select.setVisibility(View.GONE);
|
|
viewHolder.item_ll_search_result
|
|
.setVisibility(View.VISIBLE);
|
|
} else if (typeList.isEmpty()) {
|
|
viewHolder.item_rv_select.setVisibility(View.GONE);
|
|
viewHolder.item_ll_search_result.setVisibility(View.GONE);
|
|
} else {
|
|
viewHolder.item_rv_select.setVisibility(View.VISIBLE);
|
|
viewHolder.item_ll_search_result.setVisibility(View.GONE);
|
|
int height = DisplayUtils.dip2px(
|
|
getActivity(),
|
|
50 * ((int) Math.ceil((typeList.size() + 1)
|
|
/ (double) (column))));
|
|
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
|
|
LayoutParams.MATCH_PARENT, height);
|
|
viewHolder.item_rv_select.setLayoutParams(lparams);
|
|
}
|
|
} else if (position >= 3
|
|
&& position <= adapter.getNewsList().size() + 2) {
|
|
viewHolder.information_type.setText((position - 2) + "");
|
|
if (currentPosition == -1) {
|
|
String title = adapter
|
|
.getNewsList()
|
|
.get(position - 3)
|
|
.getTitle()
|
|
.replace(
|
|
keyword,
|
|
"<font color='#1BA4FC'>" + keyword
|
|
+ "</font>");
|
|
Spanned content = Html.fromHtml(title);
|
|
viewHolder.information_title.setText(content);
|
|
} else {
|
|
viewHolder.information_title.setText(adapter.getNewsList()
|
|
.get(position - 3).getTitle());
|
|
}
|
|
} else if (isRemove && position == newsList.size() + 3) {
|
|
LayoutParams params = new LayoutParams(
|
|
LayoutParams.MATCH_PARENT, 0);
|
|
|
|
int itemHeight = DisplayUtils.dip2px(getActivity(), 38);
|
|
|
|
int totalHeight = 0;
|
|
|
|
if (currentPosition == -1) {
|
|
totalHeight = fm_gamestrategy_rv_show.getHeight()
|
|
- DisplayUtils.dip2px(getActivity(), 29 + 51 + 35);
|
|
} else {
|
|
totalHeight = fm_gamestrategy_rv_show.getHeight()
|
|
- DisplayUtils.dip2px(
|
|
getActivity(),
|
|
29 + 51 + 50 * ((int) Math.ceil((typeList
|
|
.size() + 1) / (double) (column))));
|
|
}
|
|
int bottomHeight = totalHeight - newsList.size() * itemHeight;
|
|
|
|
int normalHeight = DisplayUtils.dip2px(getActivity(), 44);
|
|
|
|
if (!Config.isShow) {
|
|
normalHeight = 0;
|
|
}
|
|
|
|
if (bottomHeight > normalHeight) {
|
|
params.height = bottomHeight;
|
|
} else {
|
|
params.height = normalHeight;
|
|
}
|
|
|
|
viewHolder.itemView.setLayoutParams(params);
|
|
} else if (!isRemove && position == newsList.size() + 3) {
|
|
if (isNetworkError) {
|
|
viewHolder.item_pb_loading.setVisibility(View.GONE);
|
|
viewHolder.item_tv_loading.setText("加载失败,点击重试");
|
|
viewHolder.itemView.setClickable(true);
|
|
viewHolder.itemView
|
|
.setOnClickListener(new OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
isNetworkError = false;
|
|
notifyItemChanged(getItemCount() - 2);
|
|
if (currentPosition == -1) {
|
|
searchFormServer(newsList.size());
|
|
} else {
|
|
getGameInformation(newsList.size());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
// else if (isLoaded) {
|
|
// viewHolder.item_pb_loading.setVisibility(View.GONE);
|
|
// viewHolder.item_tv_loading.setText("点击加载更多");
|
|
// viewHolder.itemView.setClickable(true);
|
|
// viewHolder.itemView
|
|
// .setOnClickListener(new OnClickListener() {
|
|
// @Override
|
|
// public void onClick(View v) {
|
|
// isLoaded = false;
|
|
// notifyItemChanged(getItemCount() - 2);
|
|
// if (currentPosition == -1) {
|
|
// searchFormServer(newsList.size());
|
|
// } else {
|
|
// getGameInformation(newsList.size());
|
|
// }
|
|
// }
|
|
// });
|
|
// }
|
|
else {
|
|
viewHolder.item_pb_loading.setVisibility(View.VISIBLE);
|
|
viewHolder.item_tv_loading.setText("加载中...");
|
|
viewHolder.itemView.setClickable(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public GameStrategyViewHolder onCreateViewHolder(ViewGroup viewGroup,
|
|
int i) {
|
|
|
|
View view;
|
|
if (i == 0) {
|
|
RelativeLayout relativeLayout = new RelativeLayout(
|
|
getActivity());
|
|
LayoutParams params = new LayoutParams(
|
|
LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(
|
|
getActivity(), 66));
|
|
relativeLayout.setLayoutParams(params);
|
|
view = relativeLayout;
|
|
} else if (i == 1) {
|
|
RelativeLayout relativeLayout = new RelativeLayout(
|
|
getActivity());
|
|
LayoutParams params = new LayoutParams(
|
|
LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(
|
|
getActivity(), 29));
|
|
relativeLayout.setLayoutParams(params);
|
|
view = relativeLayout;
|
|
} else if (isNull && i == 2) {
|
|
view = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
R.layout.reuse_none_date, viewGroup, false);
|
|
TextView hint = (TextView) view
|
|
.findViewById(R.id.reuse_tv_none);
|
|
hint.setText("暂无数据");
|
|
int height = fm_gamestrategy_rv_show.getHeight()
|
|
- DisplayUtils.dip2px(getActivity(), 29);
|
|
LayoutParams params = new LayoutParams(
|
|
LayoutParams.MATCH_PARENT, height);
|
|
view.setLayoutParams(params);
|
|
} else if (i == 2) {
|
|
view = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
R.layout.gameinformaiton_item_search, viewGroup, false);
|
|
((TextView) view.findViewById(R.id.item_tv_search))
|
|
.setText("搜攻略");
|
|
} else if (isRemove && (newsList == null || newsList.isEmpty())
|
|
&& i == 3) {
|
|
view = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
R.layout.reuse_none_date, viewGroup, false);
|
|
TextView hint = (TextView) view
|
|
.findViewById(R.id.reuse_tv_none);
|
|
int height = 0;
|
|
if (currentPosition == -1) {
|
|
hint.setText("搜索为空");
|
|
height = fm_gamestrategy_rv_show.getHeight()
|
|
- DisplayUtils.dip2px(getActivity(), 29 + 51 + 35);
|
|
} else {
|
|
hint.setText("暂无数据");
|
|
height = fm_gamestrategy_rv_show.getHeight()
|
|
- DisplayUtils.dip2px(
|
|
getActivity(),
|
|
29 + 51 + 50 * ((int) Math.ceil((typeList
|
|
.size() + 1) / (double) (column))));
|
|
}
|
|
LayoutParams params = new LayoutParams(
|
|
LayoutParams.MATCH_PARENT, height);
|
|
view.setLayoutParams(params);
|
|
} else if (!isRemove && i == adapter.getNewsList().size() + 3) {
|
|
view = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
R.layout.gamedetails_item_loading, viewGroup, false);
|
|
} else if ((!isRemove && i == newsList.size() + 4)
|
|
|| (isRemove && i == newsList.size() + 3)) {
|
|
RelativeLayout relativeLayout = new RelativeLayout(
|
|
getActivity());
|
|
LayoutParams params = new LayoutParams(
|
|
LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(
|
|
getActivity(), 44));
|
|
if (!Config.isShow) {
|
|
params.height = 0;
|
|
}
|
|
relativeLayout.setLayoutParams(params);
|
|
view = relativeLayout;
|
|
} else {
|
|
view = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
R.layout.gameinformation_item, viewGroup, false);
|
|
}
|
|
view.setTag(i);
|
|
return new GameStrategyViewHolder(view);
|
|
}
|
|
|
|
@Override
|
|
public int getItemViewType(int position) {
|
|
|
|
return position;
|
|
}
|
|
}
|
|
|
|
private class SelectViewHolder extends RecyclerView.ViewHolder implements
|
|
OnClickListener {
|
|
|
|
private TextView select_tv_type;
|
|
|
|
public SelectViewHolder(View covertView) {
|
|
super(covertView);
|
|
|
|
select_tv_type = (TextView) covertView
|
|
.findViewById(R.id.select_tv_type);
|
|
select_tv_type.setOnClickListener(this);
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
EventBus.getDefault().post(new EBSlide(0, 0, false));
|
|
EventBus.getDefault().post(new EBSlide(0, 1, false));
|
|
fm_gamestrategy_rv_show.scrollBy(0, rparams.topMargin);
|
|
type = select_tv_type.getText().toString();
|
|
currentPosition = getPosition();
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("location", "攻略-" + type);
|
|
map.put("createOn", System.currentTimeMillis() / 1000);
|
|
map.put("game", gameName);
|
|
map.put("page", "游戏详情");
|
|
DataCollectionManager.onEvent(getActivity(), "click-item", map);
|
|
|
|
getGameInformation(0);
|
|
}
|
|
|
|
}
|
|
|
|
private class SelectViewAdapter extends
|
|
RecyclerView.Adapter<SelectViewHolder> {
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
|
|
return 1 + typeList.size();
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(SelectViewHolder viewHolder, int position) {
|
|
|
|
if (position == 0) {
|
|
viewHolder.select_tv_type.setText("全部");
|
|
} else {
|
|
viewHolder.select_tv_type.setText(typeList.get(position - 1));
|
|
}
|
|
if (currentPosition == position) {
|
|
viewHolder.select_tv_type
|
|
.setBackgroundResource(R.drawable.textview_search_dn);
|
|
viewHolder.select_tv_type.setClickable(false);
|
|
} else {
|
|
viewHolder.select_tv_type
|
|
.setBackgroundResource(R.drawable.textview_search_up);
|
|
viewHolder.select_tv_type.setClickable(true);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public SelectViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
|
|
|
|
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
R.layout.gameinformation_select_item, viewGroup, false);
|
|
return new SelectViewHolder(view);
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
isDestroy = true;
|
|
EventBus.getDefault().unregister(this);
|
|
AppController.canclePendingRequests(GameStrategyFragment.class);
|
|
view = null;
|
|
fm_gamestrategy_rv_show = null;
|
|
linearLayoutManager = null;
|
|
adapter = null;
|
|
fm_gamestrategy_ll_loading = null;
|
|
reuse_no_connection = null;
|
|
rparams = null;
|
|
adapterMap = null;
|
|
typeList = null;
|
|
id = null;
|
|
type = null;
|
|
keyword = null;
|
|
gameName = null;
|
|
entrance = null;
|
|
}
|
|
}
|