162 lines
5.6 KiB
Java
162 lines
5.6 KiB
Java
package com.gh.gamecenter.info;
|
|
|
|
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 com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
|
import com.gh.base.fragment.BaseFragment;
|
|
import com.gh.common.util.DataCollectionUtils;
|
|
import com.gh.gamecenter.DataUtils;
|
|
import com.gh.common.util.StringUtils;
|
|
import com.gh.common.view.VerticalItemDecoration;
|
|
import com.gh.gamecenter.NewsDetailActivity;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.entity.NewsEntity;
|
|
import com.gh.gamecenter.eventbus.EBNetworkState;
|
|
import com.gh.gamecenter.eventbus.EBUISwitch;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
|
|
/**
|
|
* Created by LGT on 2016/6/29.
|
|
* 资讯-资讯界面
|
|
*/
|
|
public class InfoFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
|
|
|
|
@BindView(R.id.info_srl_refresh)
|
|
SwipeRefreshLayout refreshLayout;
|
|
@BindView(R.id.info_rv_list)
|
|
RecyclerView recyclerView;
|
|
@BindView(R.id.info_pb_loading)
|
|
ProgressBarCircularIndeterminate loadingLayout;
|
|
@BindView(R.id.reuse_no_connection)
|
|
LinearLayout noConnectionLayout;
|
|
|
|
private LinearLayoutManager layoutManager;
|
|
private InfoAdapter adapter;
|
|
|
|
Runnable runnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
adapter = new InfoAdapter(getContext(), InfoFragment.this, InfoFragment.this);
|
|
recyclerView.setAdapter(adapter);
|
|
adapter.addList(0);
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.fragment_info_info;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
refreshLayout.setColorSchemeResources(R.color.theme);
|
|
refreshLayout.setOnRefreshListener(this);
|
|
|
|
recyclerView.setHasFixedSize(true);
|
|
layoutManager = new LinearLayoutManager(getActivity());
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
recyclerView.addItemDecoration(new VerticalItemDecoration(getContext(), 8, true));
|
|
adapter = new InfoAdapter(getContext(), this, this);
|
|
recyclerView.setAdapter(adapter);
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
if (newState == RecyclerView.SCROLL_STATE_IDLE
|
|
&& layoutManager.findLastVisibleItemPosition() + 1 == adapter.getItemCount()) {
|
|
if (!adapter.isOver() && !adapter.isLoading() && !adapter.isNetworkError()) {
|
|
adapter.addList(adapter.getNewsListSize());
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void loadDone() { // 数据加载成功回调
|
|
refreshLayout.setRefreshing(false);
|
|
loadingLayout.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void loadError() { // 数据加载失败回调
|
|
refreshLayout.setRefreshing(false);
|
|
loadingLayout.setVisibility(View.GONE);
|
|
recyclerView.setVisibility(View.GONE);
|
|
noConnectionLayout.setVisibility(View.VISIBLE);
|
|
}
|
|
|
|
// 连接上网络事件
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBNetworkState busNetworkState) {
|
|
if (busNetworkState.isNetworkConnected()) {
|
|
if (noConnectionLayout.getVisibility() == View.VISIBLE) {
|
|
reconnection();
|
|
} else if (adapter.isNetworkError()) {
|
|
adapter.setNetworkError(false);
|
|
adapter.notifyItemChanged(adapter.getItemCount() - 1);
|
|
adapter.addList(adapter.getNewsListSize());
|
|
}
|
|
}
|
|
}
|
|
|
|
@OnClick(R.id.reuse_no_connection)
|
|
public void reconnection() { // 重新连接
|
|
refreshLayout.setRefreshing(true);
|
|
recyclerView.setVisibility(View.VISIBLE);
|
|
loadingLayout.setVisibility(View.VISIBLE);
|
|
noConnectionLayout.setVisibility(View.GONE);
|
|
postDelayedRunnable(runnable, 1000);
|
|
}
|
|
|
|
// 资讯Fragment界面切换事件
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBUISwitch busNine) {
|
|
if (InfoToolWrapperFragment.EB_NEWSFRAGMENT_TAG.equals(busNine.getFrom())) {
|
|
if (busNine.getPosition() == 0) {
|
|
if (loadingLayout.getVisibility() == View.VISIBLE) {
|
|
adapter.addList(0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onRefresh() { // 刷新
|
|
postDelayedRunnable(runnable, 1000);
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onListClick(View view, int position, Object 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);
|
|
|
|
DataCollectionUtils.uploadClick(getContext(), "列表", "资讯-资讯", newsEntity.getTitle());
|
|
|
|
//统计阅读量
|
|
adapter.statNewsViews(newsEntity, position);
|
|
NewsDetailActivity.startNewsDetailActivity(getContext(), newsEntity, StringUtils.buildString("(资讯:资讯[" + position + "])"));
|
|
}
|
|
}
|