Files
assistant-android/app/src/main/java/com/gh/gamecenter/libao/Libao1Fragment.java
kehaoyuan 00879964e4 3.0Bug修复/UI优化/礼包优化
暂时未解决:礼包页面切换时,礼包搜索条自动顶上去/LibaoEntity getId和getLibaoId混乱
2017-09-05 11:57:19 +08:00

315 lines
11 KiB
Java

package com.gh.gamecenter.libao;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
import com.gh.base.fragment.BaseFragment;
import com.gh.common.util.EntranceUtils;
import com.gh.common.view.SwipeLayout;
import com.gh.common.view.VerticalItemDecoration;
import com.gh.gamecenter.LibaoActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.eventbus.EBReuse;
import com.gh.gamecenter.eventbus.EBUISwitch;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import butterknife.BindView;
import butterknife.OnClick;
/**
* Created by khy on 2016/12/12.
*/
public class Libao1Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener,
OnSearchCallBackListener {
@BindView(R.id.libao1_srl_refresh)
SwipeRefreshLayout mRefreshLayout;
@BindView(R.id.libao1_rv_list)
RecyclerView mRecyclerView;
@BindView(R.id.libao1_rv_history)
RecyclerView mHistoryRv;
@BindView(R.id.libao1_pb_loading)
ProgressBarCircularIndeterminate mLoadingLayout;
@BindView(R.id.reuse_no_connection)
LinearLayout mNoConnectionLayout;
@BindView(R.id.reuse_none_data)
LinearLayout mEmptyLayout;
@BindView(R.id.reuse_tv_none_data)
TextView mEmptyTv;
@BindView(R.id.godfather)
SwipeLayout swipeLayout;
private TextView mFooterHint;
private LinearLayoutManager mLayoutManager;
private Libao1FragmentAdapter adapter;
private Libao1FragmentAdapter mNormalAdapter;
private LibaoHistoryAdapter historyAdapter;
private boolean mIsSearch; // 记录页面状态 搜索页面/普通页面
private boolean isDone;
private boolean historyIsEmpty;
private String mSearckKey; // 记录搜索关键字
Runnable runnable = new Runnable() {
@Override
public void run() {
changeAdapter(true);
}
};
private LinearLayoutManager mHistoryLm;
@Override
protected int getLayoutId() {
return R.layout.fragment_libao1;
}
@Override
protected void initView(View view) {
super.initView(view);
mRefreshLayout.setColorSchemeResources(R.color.theme);
mRefreshLayout.setOnRefreshListener(this);
swipeLayout.setDragEdge(SwipeLayout.DragEdge.Bottom);
swipeLayout.setSwipeEnabled(false);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
adapter = new Libao1FragmentAdapter(this, this, this, mIsSearch, mSearckKey, mEntrance);
mNormalAdapter = adapter;
mRecyclerView.addItemDecoration(new VerticalItemDecoration(getContext(), 8, false));
mRecyclerView.setAdapter(adapter);
historyAdapter = new LibaoHistoryAdapter(this);
mHistoryLm = new LinearLayoutManager(getActivity());
mHistoryRv.setLayoutManager(mHistoryLm);
mHistoryRv.setAdapter(historyAdapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE
&& mLayoutManager.findLastVisibleItemPosition() + 1 == adapter.getItemCount()) {
if (historyIsEmpty && mFooterHint != null) {
mFooterHint.setText("加载完毕");
}
if (!adapter.isOver() && !adapter.isLoading() && !adapter.isNetworkError()) {
isDone = false;
adapter.addLibaoList(mIsSearch, adapter.getLibaoListSize());
}
}
if (mIsSearch) return;
if (mLayoutManager.findFirstVisibleItemPosition() == 0) {
swipeLayout.setDragEdge(SwipeLayout.DragEdge.Left);
} else {
if (swipeLayout.getDragEdge() != SwipeLayout.DragEdge.Bottom)
swipeLayout.setDragEdge(SwipeLayout.DragEdge.Bottom);
}
if (mLayoutManager.findLastCompletelyVisibleItemPosition() + 1 != adapter.getItemCount()) {
swipeLayout.setSwipeEnabled(false);
} else {
if (isDone && !historyIsEmpty) {
swipeLayout.setSwipeEnabled(true);
}
}
}
});
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onStartOpen(SwipeLayout layout) {
}
@Override
public void onOpen(SwipeLayout layout) {
// swipeLayout.setSwipeTrue(true);
swipeLayout.setSwipeEnabled(true);
EventBus.getDefault().post(new EBReuse("openPage"));
if (mFooterHint != null) {
mFooterHint.setText("下拉回到最新礼包");
}
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onClose(SwipeLayout layout) {
EventBus.getDefault().post(new EBReuse("closePage"));
// swipeLayout.setSwipeTrue(false);
swipeLayout.setSwipeEnabled(false);
adapter.notifyItemChanged(adapter.getItemCount() - 1);
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
}
});
mHistoryRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE
&& mHistoryLm.findLastVisibleItemPosition() + 1 == historyAdapter.getItemCount()) {
if (!historyAdapter.isOver() && !historyAdapter.isLoading() && !historyAdapter.isNetworkError()) {
isDone = false;
historyAdapter.loadList(historyAdapter.getLibaoListSize());
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
swipeLayout.setEnabled(mHistoryLm.findFirstCompletelyVisibleItemPosition() == 0);
}
});
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIsSearch = false;
isDone = false;
historyIsEmpty = false;
mSearckKey = null;
}
@Override
public void loadDone() { // 数据加载成功回调
mRefreshLayout.setRefreshing(false);
mLoadingLayout.setVisibility(View.GONE);
}
// 数据加载完毕
@Override
public void loadDone(Object obj) {
super.loadDone(obj);
if (obj.toString().equals("NULL")) {
historyIsEmpty = true;
return;
}
isDone = true;
int i = mRecyclerView.getChildCount() - 1;
if (!mIsSearch && i > 0) {
View view = mRecyclerView.getChildAt(i);
mFooterHint = (TextView) view.findViewById(R.id.footerview_hint);
}
}
@Override
public void loadError() { // 数据加载失败回调
mRefreshLayout.setRefreshing(false);
mLoadingLayout.setVisibility(View.GONE);
mRecyclerView.setVisibility(View.GONE);
mNoConnectionLayout.setVisibility(View.VISIBLE);
}
@Override
public void loadEmpty() {
mRefreshLayout.setRefreshing(false);
mLoadingLayout.setVisibility(View.GONE);
// mRecyclerView.setVisibility(View.GONE);
mEmptyLayout.setVisibility(View.VISIBLE);
// mRefreshLayout.setEnabled(false);
if (mIsSearch) {
mEmptyTv.setText("很抱歉,没找到相关礼包");
} else {
mEmptyTv.setText("这里还没有东西哦");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0x123 && adapter.getSkipPosition() != -1) {
// 更新对应位置
adapter.notifyItemChanged(adapter.getSkipPosition());
adapter.setSkipPosition(-1);
}
}
@Override
public void onRefresh() {
postDelayedRunnable(runnable, 1000);
}
@OnClick(R.id.reuse_no_connection)
public void reconnection() { // 重新连接
mRefreshLayout.setRefreshing(true);
mRecyclerView.setVisibility(View.VISIBLE);
mLoadingLayout.setVisibility(View.VISIBLE);
mNoConnectionLayout.setVisibility(View.GONE);
postDelayedRunnable(runnable, 1000);
}
@Override
public void search(boolean isSearch, String searchKey) { //搜索回调
if (mEmptyLayout.getVisibility() == View.VISIBLE) {
mEmptyLayout.setVisibility(View.GONE);
}
mIsSearch = isSearch;
mSearckKey = searchKey;
changeAdapter(false);
// if (isSearch) {
// swipeLayout.setSwipeEnabled(false);
// } else {
// swipeLayout.setSwipeEnabled(true);
// }
}
private void changeAdapter(boolean isRefresh) {
String entrance = getActivity().getIntent().getStringExtra(EntranceUtils.KEY_ENTRANCE);
if (mIsSearch) {
adapter = new Libao1FragmentAdapter(this, this, this, mIsSearch, mSearckKey, entrance);
} else {
if (mNormalAdapter != null && !isRefresh) {
adapter = mNormalAdapter;
// adapter.setSearchKey(mSearckKey);
} else {
adapter = new Libao1FragmentAdapter(this, this, this, mIsSearch, null, entrance);
mNormalAdapter = adapter;
adapter.addLibaoList(false, 0);
}
}
mRecyclerView.setAdapter(adapter);
}
// Fragment界面切换事件
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBUISwitch busNine) {
if (LibaoActivity.EB_LIBAOACTIVITY_TAG.equals(busNine.getFrom())) {
if (busNine.getPosition() == 0) {
if (mLoadingLayout.getVisibility() == View.VISIBLE) {
adapter.addLibaoList(false, 0);
}
}
}
}
}