370 lines
14 KiB
Java
370 lines
14 KiB
Java
package com.gh.gamecenter.subject;
|
|
|
|
import android.graphics.Color;
|
|
import android.graphics.drawable.ColorDrawable;
|
|
import android.os.Bundle;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.design.widget.AppBarLayout;
|
|
import android.support.design.widget.CoordinatorLayout;
|
|
import android.support.v7.widget.DefaultItemAnimator;
|
|
import android.support.v7.widget.GridLayoutManager;
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
|
import com.gh.base.BaseFragment;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.DownloadItemUtils;
|
|
import com.gh.common.util.StringUtils;
|
|
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.R;
|
|
import com.gh.gamecenter.adapter.SubjectTypeAdapter;
|
|
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 java.util.ArrayList;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
|
|
/**
|
|
* Created by khy on 27/05/17.
|
|
*/
|
|
|
|
public class SubjectTileFragment extends BaseFragment implements SubjectTypeAdapter.OnSelectTypeListener {
|
|
|
|
@BindView(R.id.subject_list)
|
|
RecyclerView subject_list;
|
|
@BindView(R.id.subject_type_list)
|
|
RecyclerView subject_type_list;
|
|
@BindView(R.id.subject_pb_loading)
|
|
ProgressBarCircularIndeterminate subject_pb_loading;
|
|
@BindView(R.id.reuse_no_connection)
|
|
LinearLayout reuse_no_connection;
|
|
@BindView(R.id.reuse_none_data)
|
|
LinearLayout mNoData;
|
|
@BindView(R.id.subject_appbar)
|
|
AppBarLayout mAppbar;
|
|
@BindView(R.id.subject_coordinator)
|
|
CoordinatorLayout mCoordinatorLayout;
|
|
@BindView(R.id.subject_tabbar_hot_tv)
|
|
TextView mTabbarHotTv;
|
|
@BindView(R.id.subject_tabbar_new_tv)
|
|
TextView mTabbarNewTv;
|
|
|
|
TextView mActionBarTitle;
|
|
|
|
private SubjectAdapter adapter;
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
private String mId;
|
|
private String mName;
|
|
private String mEntrance;
|
|
private String mType;
|
|
private String mOrder;
|
|
private String mTagType;
|
|
|
|
private boolean mScorllTop = false;
|
|
private boolean mIsOrder;
|
|
private boolean isEverpause = false;
|
|
private boolean mIsTouchScreen = false;
|
|
|
|
private int page = 1;
|
|
|
|
// 黄壮华 添加观察者 修改2015/8/15
|
|
private DataWatcher dataWatcher = new DataWatcher() {
|
|
@Override
|
|
public void onDataChanged(DownloadEntity downloadEntity) {
|
|
ArrayList<Integer> locationList = adapter.getLocationMap().get(downloadEntity.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
gameEntity = adapter.getSubjectList().get(location);
|
|
if (gameEntity != null) {
|
|
DownloadItemUtils.processDate(getContext(), gameEntity, downloadEntity,
|
|
adapter, location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
public void loadData() {
|
|
subject_pb_loading.setVisibility(View.VISIBLE);
|
|
subject_list.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
adapter = new SubjectAdapter(getContext(), SubjectTileFragment.this, mType, mId, mName, mEntrance, mOrder, mTagType, mIsOrder);
|
|
subject_list.setAdapter(adapter);
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
init(R.layout.fragment_subject_tiled);
|
|
Bundle arguments = getArguments();
|
|
|
|
mActionBarTitle = (TextView) getActivity().findViewById(R.id.actionbar_tv_title);
|
|
|
|
mId = arguments.getString("id");
|
|
mName = arguments.getString("name");
|
|
mIsOrder = arguments.getBoolean("order", false);
|
|
mEntrance = arguments.getString("entrance");
|
|
mTagType = arguments.getString("tagType");
|
|
|
|
ArrayList<String> contentTitle = arguments.getStringArrayList("contentTitle");
|
|
|
|
mType = "全部";
|
|
|
|
reuse_no_connection.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
subject_pb_loading.setVisibility(View.VISIBLE);
|
|
subject_list.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
adapter = new SubjectAdapter(getContext(), SubjectTileFragment.this, mType, mId, mName, mEntrance, mOrder, mTagType, mIsOrder);
|
|
subject_list.setAdapter(adapter);
|
|
}
|
|
});
|
|
|
|
// 设置标题
|
|
GridLayoutManager layout = new GridLayoutManager(getContext(), 4) {
|
|
@Override
|
|
public boolean canScrollVertically() {
|
|
return false;
|
|
}
|
|
};
|
|
subject_type_list.setLayoutManager(layout);
|
|
if (contentTitle != null && contentTitle.size() > 1) {
|
|
ViewGroup.LayoutParams params = subject_type_list.getLayoutParams();
|
|
params.height = (int) Math.ceil(contentTitle.size() / 4f) * DisplayUtils.dip2px(getContext(), 25);
|
|
subject_type_list.setLayoutParams(params);
|
|
((DefaultItemAnimator) subject_type_list.getItemAnimator()).setSupportsChangeAnimations(false);
|
|
SubjectTypeAdapter adapter = new SubjectTypeAdapter(getContext(), SubjectTileFragment.this, contentTitle);
|
|
subject_type_list.setAdapter(adapter);
|
|
}
|
|
|
|
|
|
layoutManager = new LinearLayoutManager(getContext());
|
|
|
|
((DefaultItemAnimator) subject_list.getItemAnimator()).setSupportsChangeAnimations(false);
|
|
subject_list.addItemDecoration(new VerticalItemDecoration(getContext(), 8, true));
|
|
subject_list.setHasFixedSize(true);
|
|
subject_list.setLayoutManager(layoutManager);
|
|
adapter = new SubjectAdapter(getContext(), SubjectTileFragment.this, mType, mId, mName, mEntrance, mOrder, mTagType, mIsOrder);
|
|
subject_list.setAdapter(adapter);
|
|
|
|
subject_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
int position = layoutManager.findFirstCompletelyVisibleItemPosition();
|
|
if (mScorllTop && position == 0 && newState == RecyclerView.SCROLL_STATE_IDLE) {
|
|
mScorllTop = false;
|
|
mAppbar.setExpanded(true, true);
|
|
}
|
|
|
|
|
|
if (!adapter.isRemove() && adapter.isLoaded() && newState == RecyclerView.SCROLL_STATE_IDLE
|
|
&& adapter.getItemCount() == layoutManager.findLastVisibleItemPosition() + 1) {
|
|
adapter.initList(page);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
super.onScrolled(recyclerView, dx, dy);
|
|
int position = layoutManager.findFirstCompletelyVisibleItemPosition();
|
|
if (!mIsTouchScreen && position == 0 && Math.abs(dy) > 10) {
|
|
mAppbar.setExpanded(true, true);
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
mAppbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
|
|
@Override
|
|
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
|
|
int totalScrollRange = appBarLayout.getTotalScrollRange();
|
|
if (Math.abs(verticalOffset) < totalScrollRange / 2) {
|
|
mActionBarTitle.setText(mName);
|
|
} else if (Math.abs(verticalOffset) == totalScrollRange && totalScrollRange != 0) {
|
|
mActionBarTitle.setText(StringUtils.buildString(mName, "-", mType, "↑"));
|
|
}
|
|
}
|
|
});
|
|
|
|
mActionBarTitle.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0 || adapter.getItemCount() == 0) {
|
|
mAppbar.setExpanded(true, true);
|
|
} else {
|
|
layoutManager.smoothScrollToPosition(subject_list, null, 0);
|
|
mScorllTop = true;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public void onTouchEvent(MotionEvent ev) {
|
|
switch (ev.getAction()) {
|
|
case MotionEvent.ACTION_DOWN:
|
|
mIsTouchScreen = true;
|
|
break;
|
|
case MotionEvent.ACTION_UP:
|
|
mIsTouchScreen = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadError() {
|
|
if (subject_pb_loading != null && subject_pb_loading.getVisibility() == View.VISIBLE) {
|
|
subject_pb_loading.setVisibility(View.GONE);
|
|
}
|
|
toast("加载失败,请检查网络状态");
|
|
subject_list.setVisibility(View.GONE);
|
|
reuse_no_connection.setVisibility(View.VISIBLE);
|
|
mNoData.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void loadDone() {
|
|
mNoData.setVisibility(View.GONE);
|
|
if (subject_pb_loading != null && subject_pb_loading.getVisibility() == View.VISIBLE) {
|
|
subject_pb_loading.setVisibility(View.GONE);
|
|
}
|
|
page++;
|
|
}
|
|
|
|
@Override
|
|
public void loadEmpty() {
|
|
super.loadEmpty();
|
|
mNoData.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
}
|
|
|
|
|
|
// 下载被删除事件
|
|
public void onEventMainThread(EBDownloadStatus status) {
|
|
if ("delete".equals(status.getStatus())) {
|
|
DownloadManager.getInstance(getContext()).removePlatform(status.getName(), status.getPlatform());
|
|
|
|
ArrayList<Integer> locationList = adapter.getLocationMap().get(status.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
gameEntity = adapter.getSubjectList().get(location);
|
|
if (gameEntity != null && gameEntity.getEntryMap() != null) {
|
|
gameEntity.getEntryMap().remove(status.getPlatform());
|
|
}
|
|
adapter.notifyItemChanged(location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBPackage busFour) {
|
|
ArrayList<Integer> locationList = adapter.getLocationMap().get(busFour.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
if ("安装".equals(busFour.getType())) {
|
|
gameEntity = adapter.getSubjectList().get(location);
|
|
for (ApkEntity apkEntity : gameEntity.getApk()) {
|
|
if (apkEntity.getPackageName().equals(busFour.getPackageName())) {
|
|
if (gameEntity.getEntryMap() != null) {
|
|
gameEntity.getEntryMap().remove(apkEntity.getPlatform());
|
|
}
|
|
adapter.notifyItemChanged(location);
|
|
break;
|
|
}
|
|
}
|
|
} else if ("卸载".equals(busFour.getType())) {
|
|
adapter.notifyItemChanged(location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//连接上网络事件
|
|
public void onEventMainThread(EBNetworkState busNetworkState) {
|
|
if (busNetworkState.isNetworkConnected()) {
|
|
if (reuse_no_connection.getVisibility() == View.VISIBLE) {
|
|
subject_list.setVisibility(View.VISIBLE);
|
|
subject_pb_loading.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
adapter = new SubjectAdapter(getContext(), SubjectTileFragment.this, mType, mId, mName, mEntrance, mOrder, mTagType, mIsOrder);
|
|
subject_list.setAdapter(adapter);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (isEverpause) {
|
|
for (GameEntity entity : adapter.getSubjectList()) {
|
|
entity.setEntryMap(DownloadManager.getInstance(getContext()).getEntryMap(entity.getName()));
|
|
}
|
|
}
|
|
isEverpause = false;
|
|
DownloadManager.getInstance(getContext()).addObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
isEverpause = true;
|
|
DownloadManager.getInstance(getContext()).removeObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
public void onSelectType(String type) {
|
|
page = 1;
|
|
this.mType = type;
|
|
subject_list.setVisibility(View.VISIBLE);
|
|
subject_pb_loading.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
adapter = new SubjectAdapter(getContext(), this, type, mId, mName, mEntrance, mOrder, mTagType, mIsOrder);
|
|
subject_list.setAdapter(adapter);
|
|
}
|
|
|
|
@OnClick({R.id.subject_tabbar_hot_tv, R.id.subject_tabbar_new_tv})
|
|
public void onClick(View view) {
|
|
switch (view.getId()) {
|
|
case R.id.subject_tabbar_hot_tv:
|
|
mTabbarHotTv.setTextColor(Color.WHITE);
|
|
mTabbarHotTv.setBackgroundResource(R.drawable.gamedetail_tag_select_bg);
|
|
mTabbarNewTv.setTextColor(Color.BLACK);
|
|
mTabbarNewTv.setBackgroundDrawable(new ColorDrawable(0));
|
|
mOrder = "";
|
|
loadData();
|
|
break;
|
|
case R.id.subject_tabbar_new_tv:
|
|
mTabbarHotTv.setTextColor(Color.BLACK);
|
|
mTabbarHotTv.setBackgroundDrawable(new ColorDrawable(0));
|
|
mTabbarNewTv.setTextColor(Color.WHITE);
|
|
mTabbarNewTv.setBackgroundResource(R.drawable.gamedetail_tag_unselect_bg);
|
|
mOrder = "latest";
|
|
loadData();
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|