291 lines
10 KiB
Java
291 lines
10 KiB
Java
package com.gh.gamecenter.news;
|
|
|
|
import android.app.Dialog;
|
|
import android.content.Intent;
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.text.TextUtils;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.Window;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
|
import com.gh.base.fragment.BaseFragment;
|
|
import com.gh.gamecenter.DataUtils;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.LoginUtils;
|
|
import com.gh.common.util.NewsUtils;
|
|
import com.gh.common.util.StringUtils;
|
|
import com.gh.gamecenter.ConcernActivity;
|
|
import com.gh.gamecenter.GameNewsActivity;
|
|
import com.gh.gamecenter.NewsDetailActivity;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.entity.GameEntity;
|
|
import com.gh.gamecenter.entity.NewsEntity;
|
|
import com.gh.gamecenter.eventbus.EBConcernChanged;
|
|
import com.gh.gamecenter.eventbus.EBUISwitch;
|
|
import com.gh.gamecenter.manager.DataCollectionManager;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
import com.jakewharton.rxbinding.view.RxView;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
import rx.functions.Action1;
|
|
import rx.schedulers.Schedulers;
|
|
|
|
/**
|
|
* Created by khy on 2017/4/7.
|
|
* 资讯-攻略界面
|
|
*/
|
|
public class News3Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener,
|
|
OnStrategyDialogCallBackListener {
|
|
|
|
@BindView(R.id.strategy_game_name)
|
|
TextView mGameName;
|
|
@BindView(R.id.strategy_select_game_rl)
|
|
RelativeLayout mSelectGameRl;
|
|
@BindView(R.id.strategy_rv)
|
|
RecyclerView mStrategyRv;
|
|
@BindView(R.id.popup_bg)
|
|
View popupBg;
|
|
@BindView(R.id.reuse_none_data)
|
|
LinearLayout mNoData;
|
|
@BindView(R.id.reuse_no_connection)
|
|
LinearLayout mNoConnection;
|
|
@BindView(R.id.strategy_refresh)
|
|
SwipeRefreshLayout mSwipeRefreshLayout;
|
|
@BindView(R.id.strategy_loading)
|
|
ProgressBarCircularIndeterminate mLoading;
|
|
|
|
private Dialog dialog;
|
|
private LinearLayoutManager mLayoutManager;
|
|
private News3FragmentAdapter mStrategyAdapter;
|
|
|
|
private List<GameEntity> mConcernGame;
|
|
|
|
Runnable runnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
mStrategyAdapter = new News3FragmentAdapter(getContext(), News3Fragment.this, News3Fragment.this);
|
|
mStrategyRv.setAdapter(mStrategyAdapter);
|
|
mStrategyAdapter.addList(0);
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.fragment_news3;
|
|
}
|
|
|
|
@Override
|
|
protected void initView(View view) {
|
|
super.initView(view);
|
|
|
|
mConcernGame = new ArrayList<>();
|
|
|
|
mSwipeRefreshLayout.setColorSchemeResources(R.color.theme);
|
|
mSwipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
mStrategyAdapter = new News3FragmentAdapter(getContext(), this, this);
|
|
mLayoutManager = new LinearLayoutManager(getContext());
|
|
mStrategyRv.setLayoutManager(mLayoutManager);
|
|
mStrategyRv.setAdapter(mStrategyAdapter);
|
|
|
|
mStrategyRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
if (mLayoutManager.findLastVisibleItemPosition() + 1 == mStrategyAdapter.getItemCount()
|
|
&& newState == RecyclerView.SCROLL_STATE_IDLE && mStrategyAdapter.isCanLoading()) {
|
|
mStrategyAdapter.addList(mStrategyAdapter.getItemCount() - 1);
|
|
}
|
|
}
|
|
});
|
|
|
|
// 防抖处理
|
|
RxView.clicks(mSelectGameRl)
|
|
.throttleFirst(1, TimeUnit.SECONDS)
|
|
.subscribe(new Action1<Void>() {
|
|
@Override
|
|
public void call(Void aVoid) {
|
|
if (mConcernGame.size() > 1 || (mConcernGame.size() == 1
|
|
&& !getString(R.string.ghzs_id).equals(mConcernGame.get(0).getId()))) {
|
|
OnSelectGameClickListener();
|
|
} else {
|
|
Intent intent = ConcernActivity.getIntent(getContext(), "资讯(攻略-我关注的游戏)");
|
|
startActivity(intent);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void getConcernGame() {
|
|
RetrofitManager.getInstance(getContext()).getApi()
|
|
.getConcern()
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<List<GameEntity>>() {
|
|
@Override
|
|
public void onResponse(List<GameEntity> response) {
|
|
super.onResponse(response);
|
|
mConcernGame.clear();
|
|
mConcernGame.addAll(response);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void OnSelectGameClickListener() {
|
|
isShowPopupBg(true);
|
|
|
|
View contentView = View.inflate(getContext(), R.layout.dialog_strategy_select_game, null);
|
|
RecyclerView selectGameRv = (RecyclerView) contentView.findViewById(R.id.dialog_strategy_select_game_rv);
|
|
RelativeLayout allGameRl = (RelativeLayout) contentView.findViewById(R.id.dialog_strategy_select_game_rl);
|
|
|
|
if (mConcernGame.size() >= 8) {
|
|
ViewGroup.LayoutParams params = selectGameRv.getLayoutParams();
|
|
params.height = DisplayUtils.dip2px(getContext(), 43 * 8);
|
|
selectGameRv.setLayoutParams(params);
|
|
}
|
|
|
|
int widthPixels = getResources().getDisplayMetrics().widthPixels;
|
|
allGameRl.setLayoutParams(new LinearLayout.LayoutParams((widthPixels * 9) / 10,
|
|
DisplayUtils.dip2px(getContext(), 50)));
|
|
|
|
allGameRl.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
isShowPopupBg(false);
|
|
Intent intent = ConcernActivity.getIntent(getContext(), "资讯(攻略-我关注的游戏)");
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
|
|
selectGameRv.setLayoutManager(new LinearLayoutManager(getContext()));
|
|
selectGameRv.setAdapter(new News3FragmentDialogAdapter(News3Fragment.this, mConcernGame));
|
|
|
|
|
|
dialog = new Dialog(getContext());
|
|
|
|
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
dialog.setContentView(contentView);
|
|
dialog.show();
|
|
|
|
|
|
contentView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
isShowPopupBg(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void isShowPopupBg(boolean isShow) {
|
|
if (!isShow) {
|
|
if (dialog != null) {
|
|
dialog.cancel();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadDone() {
|
|
super.loadDone();
|
|
mSwipeRefreshLayout.setRefreshing(false);
|
|
mLoading.setVisibility(View.GONE);
|
|
mStrategyRv.setVisibility(View.VISIBLE);
|
|
mNoData.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void loadError() {
|
|
super.loadError();
|
|
mSwipeRefreshLayout.setRefreshing(false);
|
|
mLoading.setVisibility(View.GONE);
|
|
mStrategyRv.setVisibility(View.GONE);
|
|
mNoConnection.setVisibility(View.VISIBLE);
|
|
}
|
|
|
|
@Override
|
|
public void loadEmpty() {
|
|
super.loadEmpty();
|
|
mNoData.setVisibility(View.VISIBLE);
|
|
mStrategyRv.setVisibility(View.GONE);
|
|
mLoading.setVisibility(View.GONE);
|
|
}
|
|
|
|
@OnClick(R.id.reuse_no_connection)
|
|
public void reconnection() { // 重新连接
|
|
mStrategyRv.setVisibility(View.VISIBLE);
|
|
mLoading.setVisibility(View.VISIBLE);
|
|
mNoConnection.setVisibility(View.GONE);
|
|
postDelayedRunnable(runnable, 1000);
|
|
}
|
|
|
|
@Override
|
|
public void selectPosition(int position, GameEntity gameEntity) {
|
|
isShowPopupBg(false);
|
|
Intent intent = GameNewsActivity.getIntent(getContext(), gameEntity.getName(), gameEntity.getId(), "资讯-攻略(我关注的游戏)");
|
|
getContext().startActivity(intent);
|
|
}
|
|
|
|
// 资讯Fragment界面切换事件
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBUISwitch busNine) {
|
|
if (NewsWrapperFragment.EB_NEWSFRAGMENT_TAG.equals(busNine.getFrom())) {
|
|
if (busNine.getPosition() == 2 && mLoading.getVisibility() == View.VISIBLE) {
|
|
mStrategyAdapter.addList(0);
|
|
if (!TextUtils.isEmpty(LoginUtils.getToken(getContext()))) {
|
|
getConcernGame();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBConcernChanged changed) {
|
|
getConcernGame();
|
|
}
|
|
|
|
@Override
|
|
public void onRefresh() {
|
|
postDelayedRunnable(runnable, 1000);
|
|
}
|
|
|
|
@Override
|
|
public void onListClick(View view, int position, Object data) {
|
|
super.onListClick(view, position, data);
|
|
List<NewsEntity> newsList = (List<NewsEntity>) data;
|
|
NewsEntity newsEntity = newsList.get(position);
|
|
Map<String, Object> kv = new HashMap<>();
|
|
kv.put("名字", newsEntity.getTitle());
|
|
kv.put("位置", String.valueOf(position + 1));
|
|
DataUtils.onEvent(getContext(), "点击", "资讯-攻略", kv);
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
map.put("page", "资讯-攻略");
|
|
map.put("news", newsEntity.getTitle());
|
|
map.put("news_id", newsEntity.getId());
|
|
DataCollectionManager.onEvent(getContext(), "click-item", map);
|
|
|
|
// 统计阅读量
|
|
NewsUtils.statNewsViews(getContext(), newsEntity.getId());
|
|
NewsDetailActivity.startNewsDetailActivity(getContext(), newsEntity,
|
|
StringUtils.buildString("(资讯:攻略[" + position + "])"));
|
|
}
|
|
}
|