Files
assistant-android/app/src/main/java/com/gh/gamecenter/GameNewsActivity.java
2017-04-29 17:25:37 +08:00

202 lines
7.6 KiB
Java

package com.gh.gamecenter;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.gh.base.BaseActivity;
import com.gh.common.util.DisplayUtils;
import com.gh.gamecenter.adapter.GameNewsAdapter;
import com.gh.gamecenter.adapter.GameNewsTypeListAdapter;
import com.gh.gamecenter.eventbus.EBTypeChange;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import retrofit2.adapter.rxjava.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* Created by LGT on 2016/8/29.
* 游戏新闻界面
*/
public class GameNewsActivity extends BaseActivity implements View.OnClickListener {
@BindView(R.id.game_news_list)
RecyclerView game_news_list;
@BindView(R.id.game_news_top_type_list)
RecyclerView game_news_top_type_list;
@BindView(R.id.reuse_none_data)
LinearLayout mNoDataLl;
@BindView(R.id.reuse_tv_none_data)
TextView mNoDataTv;
private GameNewsAdapter adapter;
private GameNewsTypeListAdapter typeListAdapter;
private LinearLayoutManager layoutManager;
private ImageView ivSearch;
private ArrayMap<String, GameNewsAdapter> adapterMap;
private List<String> typeList;
private String gameId;
private String entrance;
private float startY = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
entrance = getIntent().getStringExtra("entrance");
View contentView = View.inflate(this, R.layout.activity_game_news, null);
ivSearch = new ImageView(this);
ivSearch.setImageResource(R.drawable.ic_search_white);
RelativeLayout.LayoutParams ivSearchParams = new RelativeLayout.LayoutParams(
DisplayUtils.dip2px(this, 20), DisplayUtils.dip2px(this, 20));
ivSearchParams.addRule(RelativeLayout.CENTER_VERTICAL);
ivSearchParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ivSearchParams.setMargins(0, 0, DisplayUtils.dip2px(this, 10), 0);
RelativeLayout reuse_actionbar = (RelativeLayout) contentView.findViewById(R.id.reuse_actionbar);
reuse_actionbar.addView(ivSearch, ivSearchParams);
ivSearch.setOnClickListener(this);
ivSearch.setVisibility(View.GONE);
String gameName = getIntent().getStringExtra("gameName");
init(contentView, gameName);
mNoDataTv.setText("暂无内容");
adapterMap = new ArrayMap<>();
typeList = new ArrayList<>();
gameId = getIntent().getStringExtra("gameId");
game_news_list.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
game_news_list.setLayoutManager(layoutManager);
game_news_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (layoutManager.findFirstVisibleItemPosition() >= 1) {
game_news_top_type_list.setVisibility(View.VISIBLE);
ivSearch.setVisibility(View.VISIBLE);
} else {
game_news_top_type_list.setVisibility(View.GONE);
ivSearch.setVisibility(View.GONE);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (adapter != null && newState == RecyclerView.SCROLL_STATE_IDLE
&& layoutManager.findLastVisibleItemPosition() == adapter.getItemCount() - 1) {
if (!adapter.isRemove() && !adapter.isLoading() && !adapter.isNetworkError()) {
adapter.addList(adapter.getNewsList().size());
}
}
}
});
game_news_top_type_list.setHasFixedSize(true);
game_news_top_type_list.setLayoutManager(new GridLayoutManager(this, 5));
//禁止由于滑动出现的阴影
game_news_top_type_list.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startY = event.getY();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (Math.abs(startY - event.getY()) > 0) {
return true;
}
}
return false;
}
});
if (!TextUtils.isEmpty(gameId)) {
getGameArticleType();
}
}
private void getGameArticleType() {
RetrofitManager.getApi()
.getGameArticleType(gameId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<String>>() {
@Override
public void onResponse(List<String> response) {
super.onResponse(response);
typeList = response;
typeList.add(0, "全部");
adapter = new GameNewsAdapter(GameNewsActivity.this, typeList, game_news_list, gameId, "全部", entrance, mNoDataLl); adapterMap.put("全部", adapter);
game_news_list.setAdapter(adapter);
typeListAdapter = new GameNewsTypeListAdapter(GameNewsActivity.this, typeList, "全部");
game_news_top_type_list.setAdapter(typeListAdapter);
ViewGroup.LayoutParams params = game_news_top_type_list.getLayoutParams();
params.height = (int) Math.ceil(typeList.size() / 5f) * DisplayUtils.dip2px(GameNewsActivity.this, 35)
+ DisplayUtils.dip2px(GameNewsActivity.this, 12);
game_news_top_type_list.setLayoutParams(params);
}
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
}
});
}
public void onEventMainThread(EBTypeChange change) {
adapter = adapterMap.get(change.getType());
if (adapter == null) {
adapter = new GameNewsAdapter(GameNewsActivity.this, typeList, game_news_list, gameId, change.getType(), entrance, mNoDataLl);
adapterMap.put(change.getType(), adapter);
}
game_news_list.setAdapter(adapter);
game_news_top_type_list.setVisibility(View.GONE);
ivSearch.setVisibility(View.GONE);
typeListAdapter.setNewsType(change.getType(), change.getPosition());
}
@Override
public void onClick(View v) {
if (v == ivSearch) {
game_news_list.scrollToPosition(0);
game_news_top_type_list.setVisibility(View.GONE);
ivSearch.setVisibility(View.GONE);
adapter.openKeyBoard();
}
}
}