518 lines
19 KiB
Java
518 lines
19 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.animation.Animator;
|
|
import android.os.Bundle;
|
|
import android.support.v4.content.ContextCompat;
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
import android.support.v7.widget.DefaultItemAnimator;
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.text.TextUtils;
|
|
import android.view.KeyEvent;
|
|
import android.view.View;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.PopupWindow;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.util.DownloadItemUtils;
|
|
import com.gh.common.view.VerticalItemDecoration;
|
|
import com.gh.download.DataWatcher;
|
|
import com.gh.download.DownloadEntity;
|
|
import com.gh.download.DownloadManager;
|
|
import com.gh.gamecenter.adapter.KaiFuAdapter;
|
|
import com.gh.gamecenter.adapter.KaiFuDialogAdapter;
|
|
import com.gh.gamecenter.entity.ApkEntity;
|
|
import com.gh.gamecenter.entity.GameEntity;
|
|
import com.gh.gamecenter.eventbus.EBDownloadStatus;
|
|
import com.gh.gamecenter.eventbus.EBNetworkState;
|
|
import com.gh.gamecenter.eventbus.EBPackage;
|
|
import com.jakewharton.rxbinding.view.RxView;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import butterknife.BindView;
|
|
import rx.functions.Action1;
|
|
|
|
/**
|
|
* Created by khy on 2017/3/16.
|
|
* 首页-开服表
|
|
*/
|
|
public class KaiFuActivity extends BaseActivity implements KaiFuDialogAdapter.OnKaiFuDialogCallBackListener
|
|
, KaiFuAdapter.OnScrollToKaiFuIndexListener, SwipeRefreshLayout.OnRefreshListener {
|
|
|
|
@BindView(R.id.kaifu_rv)
|
|
RecyclerView mRecyclerView;
|
|
@BindView(R.id.kaifu_pb)
|
|
ProgressBarCircularIndeterminate mPbLoading;
|
|
@BindView(R.id.kaifu_refresh)
|
|
SwipeRefreshLayout mRefreshLayout;
|
|
@BindView(R.id.kaifu_game_name)
|
|
TextView mGameName;
|
|
@BindView(R.id.popup_bg)
|
|
View mPopupBg;
|
|
@BindView(R.id.kaifu_select_game_rl)
|
|
RelativeLayout mSelectGameRl;
|
|
@BindView(R.id.reuse_no_connection)
|
|
LinearLayout mNoConnection;
|
|
@BindView(R.id.reuse_none_data)
|
|
LinearLayout mNoData;
|
|
@BindView(R.id.kaifu_select_change_rl)
|
|
RelativeLayout mChangeRl;
|
|
@BindView(R.id.kaifu_change_name)
|
|
TextView mChangeName;
|
|
|
|
private PopupWindow mPopupWindow;
|
|
|
|
private KaiFuAdapter mAdapter;
|
|
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
private boolean isEverpause = false;
|
|
private boolean move = false;
|
|
private boolean smoothMove = false;
|
|
|
|
private String kaifuType;
|
|
Runnable runnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
mAdapter = new KaiFuAdapter(KaiFuActivity.this, KaiFuActivity.this, KaiFuActivity.this, kaifuType);
|
|
mRecyclerView.setAdapter(mAdapter);
|
|
}
|
|
};
|
|
private int mDialogGamePosition; // 顶部弹窗过滤位置
|
|
private int kaifuOffset = 0;
|
|
private int mInitMovePosition; // 首次进入移动的位置
|
|
private int mMovePosition; // 移动位置
|
|
private DataWatcher dataWatcher = new DataWatcher() {
|
|
@Override
|
|
public void onDataChanged(DownloadEntity downloadEntity) {
|
|
ArrayList<Integer> locationList = mAdapter.getLocationMap().get(downloadEntity.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
gameEntity = mAdapter.getSubjectList().get(location - getGameEntityIndex());
|
|
if (gameEntity != null) {
|
|
DownloadItemUtils.processDate(KaiFuActivity.this, gameEntity, downloadEntity,
|
|
mAdapter, location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.activity_kaifu;
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
init(getString(R.string.title_kaifu));
|
|
|
|
kaifuType = "全部";
|
|
|
|
mRefreshLayout.setColorSchemeResources(R.color.theme);
|
|
mRefreshLayout.setOnRefreshListener(this);
|
|
mRefreshLayout.setEnabled(false);
|
|
|
|
mAdapter = new KaiFuAdapter(this, this, this, kaifuType);
|
|
layoutManager = new LinearLayoutManager(this);
|
|
((DefaultItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
|
|
mRecyclerView.addItemDecoration(new VerticalItemDecoration(this, 8, false));
|
|
mRecyclerView.setLayoutManager(layoutManager);
|
|
mRecyclerView.setAdapter(mAdapter);
|
|
|
|
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
if (smoothMove && newState == RecyclerView.SCROLL_STATE_IDLE) {
|
|
smoothMove = false;
|
|
int n = mMovePosition - layoutManager.findFirstVisibleItemPosition();
|
|
if (0 <= n && n < mRecyclerView.getChildCount()) {
|
|
View view = layoutManager.findViewByPosition(n);
|
|
if (view!= null) {
|
|
int top = view.getTop();
|
|
mRecyclerView.smoothScrollBy(0, top);
|
|
}
|
|
}
|
|
}
|
|
|
|
//上拉加载
|
|
if (mAdapter.isLoadedBottom() && newState == RecyclerView.SCROLL_STATE_IDLE
|
|
&& mAdapter.getItemCount() == layoutManager.findLastVisibleItemPosition() + 1) {
|
|
mAdapter.initListBottom(kaifuOffset + mAdapter.loadCountBottom());
|
|
}
|
|
//下拉加载
|
|
if (kaifuOffset > 0 && mAdapter.isLoadedTop() && newState == RecyclerView.SCROLL_STATE_IDLE
|
|
&& layoutManager.findFirstVisibleItemPosition() == 0) {
|
|
int countTop = mAdapter.loadCountTop();
|
|
if (countTop % 20 == 0) { // 不是20的倍数 证明加载完毕
|
|
int offset = kaifuOffset - countTop;
|
|
int limit = 20;
|
|
if (offset < 0) {
|
|
limit = limit + offset;
|
|
offset = 0;
|
|
}
|
|
|
|
if (limit == 0) return;
|
|
|
|
mAdapter.initListTop(offset, limit);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
super.onScrolled(recyclerView, dx, dy);
|
|
if (move) {
|
|
move = false;
|
|
int n = mMovePosition - layoutManager.findFirstVisibleItemPosition();
|
|
if (0 <= n && n < mRecyclerView.getChildCount()) {
|
|
int top = mRecyclerView.getChildAt(n).getTop();
|
|
mRecyclerView.scrollBy(0, top);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
// 防抖处理
|
|
RxView.clicks(mSelectGameRl)
|
|
.throttleFirst(1, TimeUnit.SECONDS)
|
|
.subscribe(new Action1<Void>() {
|
|
@Override
|
|
public void call(Void aVoid) {
|
|
initSelectGameDialog();
|
|
}
|
|
});
|
|
|
|
mNoConnection.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
mAdapter.initKaiFuOffset();
|
|
mNoConnection.setVisibility(View.GONE);
|
|
mPbLoading.setVisibility(View.VISIBLE);
|
|
}
|
|
});
|
|
|
|
mChangeRl.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
mRecyclerView.stopScroll();
|
|
|
|
if ("即将开服↑".equals(mChangeName.getText().toString())) {
|
|
mChangeName.setText("当前开服↓");
|
|
mChangeName.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.theme));
|
|
mChangeRl.setBackgroundResource(R.drawable.comment_border_bg);
|
|
if (kaifuOffset == 0 && mInitMovePosition < 4) {
|
|
mMovePosition = 0;
|
|
} else {
|
|
mMovePosition = mInitMovePosition + mAdapter.getLoadCountTop() - 25;
|
|
}
|
|
} else {
|
|
mChangeName.setText("即将开服↑");
|
|
mChangeName.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.content));
|
|
mChangeRl.setBackgroundResource(R.drawable.border_suggest_bg);
|
|
mMovePosition = mInitMovePosition + mAdapter.getLoadCountTop() - 20;
|
|
if (mMovePosition < 0) {
|
|
mMovePosition = mInitMovePosition;
|
|
}
|
|
}
|
|
|
|
if (mInitMovePosition != 1) {
|
|
mRecyclerView.stopScroll();
|
|
moveToPosition(mMovePosition, true);
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
private void initSelectGameDialog() {
|
|
isShowPopupBg(true);
|
|
|
|
View contentView = View.inflate(this, R.layout.dialog_kaifu_select_game, null);
|
|
RecyclerView selectGameRv = (RecyclerView) contentView.findViewById(R.id.dialog_kaifu_select_game_rv);
|
|
final TextView selectGameTitle = (TextView) contentView.findViewById(R.id.dialog_kaifu_title);
|
|
|
|
final LinearLayoutManager layout = new LinearLayoutManager(this);
|
|
selectGameRv.setLayoutManager(layout);
|
|
selectGameRv.setAdapter(new KaiFuDialogAdapter(this, this, mDialogGamePosition));
|
|
|
|
|
|
mPopupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT
|
|
, LinearLayout.LayoutParams.MATCH_PARENT, true);
|
|
mPopupWindow.setAnimationStyle(R.style.scale_popwindow_anim_style);
|
|
mPopupWindow.showAtLocation(mSelectGameRl, 0, 0, 0);
|
|
|
|
selectGameRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
super.onScrolled(recyclerView, dx, dy);
|
|
|
|
if (layout.findFirstVisibleItemPosition() >= 5) {
|
|
selectGameTitle.setVisibility(View.VISIBLE);
|
|
} else {
|
|
selectGameTitle.setVisibility(View.GONE);
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
selectGameRv.setOnKeyListener(new View.OnKeyListener() {
|
|
@Override
|
|
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
|
if (keyCode == KeyEvent.KEYCODE_BACK
|
|
&& event.getRepeatCount() == 0
|
|
&& mPopupWindow != null
|
|
&& mPopupWindow.isShowing()) {
|
|
isShowPopupBg(false);
|
|
}
|
|
return false;
|
|
}
|
|
});
|
|
|
|
contentView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
isShowPopupBg(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void moveToPosition(int n, boolean isSmooth) {
|
|
int firstItem = layoutManager.findFirstVisibleItemPosition();
|
|
int lastItem = layoutManager.findLastVisibleItemPosition();
|
|
if (n <= firstItem) {
|
|
if (isSmooth) {
|
|
mRecyclerView.smoothScrollToPosition(n);
|
|
} else {
|
|
mRecyclerView.scrollToPosition(n);
|
|
}
|
|
} else if (n <= lastItem) {
|
|
int top = layoutManager.findViewByPosition(n).getTop();
|
|
if (isSmooth) {
|
|
mRecyclerView.smoothScrollBy(0, top);
|
|
} else {
|
|
mRecyclerView.scrollBy(0, top);
|
|
}
|
|
} else {
|
|
if (isSmooth) {
|
|
mRecyclerView.smoothScrollToPosition(n);
|
|
smoothMove = true;
|
|
} else {
|
|
mRecyclerView.scrollToPosition(n);
|
|
move = true;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void isShowPopupBg(boolean isShow) {
|
|
if (isShow) {
|
|
mPopupBg.setVisibility(View.VISIBLE);
|
|
mPopupBg.animate()
|
|
.alpha(1f)
|
|
.setDuration(300)
|
|
.setListener(null);
|
|
} else {
|
|
mPopupWindow.dismiss();
|
|
mPopupBg.animate()
|
|
.alpha(0f)
|
|
.setDuration(300)
|
|
.setListener(new Animator.AnimatorListener() {
|
|
@Override
|
|
public void onAnimationStart(Animator animation) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onAnimationEnd(Animator animation) {
|
|
mPopupBg.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void onAnimationCancel(Animator animation) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onAnimationRepeat(Animator animation) {
|
|
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
isEverpause = true;
|
|
DownloadManager.getInstance(getApplicationContext()).removeObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (isEverpause) {
|
|
for (GameEntity entity : mAdapter.getSubjectList()) {
|
|
entity.setEntryMap(DownloadManager.getInstance(this).getEntryMap(entity.getName()));
|
|
}
|
|
}
|
|
isEverpause = false;
|
|
DownloadManager.getInstance(getApplicationContext()).addObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
public void loadDone() {
|
|
mNoData.setVisibility(View.GONE);
|
|
if (mPbLoading != null && mPbLoading.getVisibility() == View.VISIBLE) {
|
|
mPbLoading.setVisibility(View.GONE);
|
|
}
|
|
|
|
if (mRefreshLayout.isRefreshing()) {
|
|
mRefreshLayout.setRefreshing(false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadDone(Object obj) {
|
|
super.loadDone(obj);
|
|
|
|
kaifuOffset = (int) obj;
|
|
|
|
// if (kaifuOffset == 0) {
|
|
// mRefreshLayout.setEnabled(true);
|
|
// } else {
|
|
// mRefreshLayout.setEnabled(false);
|
|
// }
|
|
}
|
|
|
|
@Override
|
|
public void loadError() {
|
|
if (mPbLoading != null && mPbLoading.getVisibility() == View.VISIBLE) {
|
|
mPbLoading.setVisibility(View.GONE);
|
|
}
|
|
toast("加载失败,请检查网络状态");
|
|
mPbLoading.setVisibility(View.GONE);
|
|
mNoData.setVisibility(View.GONE);
|
|
mNoConnection.setVisibility(View.VISIBLE);
|
|
}
|
|
|
|
@Override
|
|
public void loadEmpty() {
|
|
super.loadEmpty();
|
|
mNoData.setVisibility(View.VISIBLE);
|
|
mNoConnection.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void selectPosition(int position, String tag, String gameName) {
|
|
mDialogGamePosition = position;
|
|
if (!TextUtils.isEmpty(gameName)) {
|
|
mGameName.setText(gameName);
|
|
} else {
|
|
mGameName.setText(tag);
|
|
}
|
|
isShowPopupBg(false);
|
|
|
|
kaifuOffset = 0;
|
|
kaifuType = tag;
|
|
|
|
mRecyclerView.setVisibility(View.VISIBLE);
|
|
mPbLoading.setVisibility(View.VISIBLE);
|
|
mNoConnection.setVisibility(View.GONE);
|
|
mNoData.setVisibility(View.GONE);
|
|
|
|
mAdapter = new KaiFuAdapter(this, this, this, kaifuType);
|
|
mRecyclerView.setAdapter(mAdapter);
|
|
|
|
mChangeName.setText("即将开服↑");
|
|
mChangeName.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.content));
|
|
mChangeRl.setBackgroundResource(R.drawable.border_suggest_bg);
|
|
}
|
|
|
|
// 下载被删除事件
|
|
public void onEventMainThread(EBDownloadStatus status) {
|
|
if ("delete".equals(status.getStatus())) {
|
|
DownloadManager.getInstance(this).removePlatform(status.getName(), status.getPlatform());
|
|
|
|
ArrayList<Integer> locationList = mAdapter.getLocationMap().get(status.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
|
|
gameEntity = mAdapter.getSubjectList().get(location - getGameEntityIndex());
|
|
if (gameEntity != null && gameEntity.getEntryMap() != null) {
|
|
gameEntity.getEntryMap().remove(status.getPlatform());
|
|
}
|
|
mAdapter.notifyItemChanged(location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private int getGameEntityIndex() {
|
|
if (kaifuOffset != 0) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public void onEventMainThread(EBPackage busFour) {
|
|
ArrayList<Integer> locationList = mAdapter.getLocationMap().get(busFour.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
if ("安装".equals(busFour.getType())) {
|
|
gameEntity = mAdapter.getSubjectList().get(location - getGameEntityIndex());
|
|
for (ApkEntity apkEntity : gameEntity.getApk()) {
|
|
if (apkEntity.getPackageName().equals(busFour.getPackageName())) {
|
|
if (gameEntity.getEntryMap() != null) {
|
|
gameEntity.getEntryMap().remove(apkEntity.getPlatform());
|
|
}
|
|
mAdapter.notifyItemChanged(location + getGameEntityIndex());
|
|
break;
|
|
}
|
|
}
|
|
} else if ("卸载".equals(busFour.getType())) {
|
|
mAdapter.notifyItemChanged(location + getGameEntityIndex());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//连接上网络事件
|
|
public void onEventMainThread(EBNetworkState busNetworkState) {
|
|
if (busNetworkState.isNetworkConnected()) {
|
|
if (mNoConnection.getVisibility() == View.VISIBLE) {
|
|
mRecyclerView.setVisibility(View.VISIBLE);
|
|
mPbLoading.setVisibility(View.VISIBLE);
|
|
mNoConnection.setVisibility(View.GONE);
|
|
mAdapter = new KaiFuAdapter(this, this, this, kaifuType);
|
|
mRecyclerView.setAdapter(mAdapter);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onScroll(int scrollPosition) {
|
|
mInitMovePosition = scrollPosition + getGameEntityIndex() + 1;
|
|
moveToPosition(mInitMovePosition, false);
|
|
}
|
|
|
|
@Override
|
|
public void onRefresh() {
|
|
mRecyclerView.postDelayed(runnable, 1000);
|
|
}
|
|
}
|