330 lines
14 KiB
Java
330 lines
14 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.Color;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.util.DisplayMetrics;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.widget.EditText;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.util.DataCollectionUtils;
|
|
import com.gh.common.util.DataUtils;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.MeasureHeightLayoutManager;
|
|
import com.gh.common.util.NewsUtils;
|
|
import com.gh.common.view.CardLinearLayout;
|
|
import com.gh.common.view.VerticalItemDecoration;
|
|
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
|
|
import com.gh.gamecenter.entity.NewsEntity;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import butterknife.BindView;
|
|
import retrofit2.adapter.rxjava.HttpException;
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
import rx.functions.Func1;
|
|
import rx.schedulers.Schedulers;
|
|
|
|
/**
|
|
* Created by khy on 2016/8/22.
|
|
* 新闻搜索界面
|
|
*/
|
|
public class NewsSearchActivity extends BaseActivity {
|
|
|
|
@BindView(R.id.gamedetail_news_rv) RecyclerView gamedetail_news_rv;
|
|
@BindView(R.id.tv_search) TextView tv_search;
|
|
@BindView(R.id.reuse_none_data) LinearLayout reuse_none_data;
|
|
@BindView(R.id.et_search) EditText et_search;
|
|
@BindView(R.id.gamedetail_news_ll_loading) LinearLayout gamedetail_news_ll_loading;
|
|
@BindView(R.id.reuse_no_connection) LinearLayout reuse_no_connection;
|
|
@BindView(R.id.gamedetail_news_cardView) CardLinearLayout gamedetail_news_cardView;
|
|
|
|
private MeasureHeightLayoutManager layoutManager;
|
|
private List<NewsEntity> newsEntities;
|
|
private NewsSearchAdapter searchAdapter;
|
|
|
|
private boolean isLoadOver = true;
|
|
private boolean isRemove = false;
|
|
private boolean isNetworkError = false;
|
|
|
|
private String searchKey;
|
|
private String gameId;
|
|
private String entrance;
|
|
|
|
private Handler handler = new Handler();
|
|
|
|
private int page = 1;
|
|
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Intent intent = getIntent();
|
|
String gameName = intent.getExtras().getString("gameName");
|
|
searchKey = intent.getExtras().getString("searchKey");
|
|
gameId = intent.getExtras().getString("gameId");
|
|
entrance = intent.getExtras().getString("entrance");
|
|
|
|
View view = View.inflate(this, R.layout.activity_gamedetail_news, null);
|
|
init(view, gameName);
|
|
|
|
DisplayMetrics outMetrics = new DisplayMetrics();
|
|
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
|
|
|
|
ViewGroup.LayoutParams params = gamedetail_news_cardView.getLayoutParams();
|
|
params.height = outMetrics.heightPixels
|
|
- DisplayUtils.dip2px(this, 38 + 16 + 8 + 8)
|
|
- getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE).getInt("actionbar_height",
|
|
DisplayUtils.dip2px(getApplicationContext(), 48))
|
|
- DisplayUtils.getStatusBarHeight(getResources());
|
|
gamedetail_news_cardView.setLayoutParams(params);
|
|
|
|
newsEntities = new ArrayList<>();
|
|
searchAdapter = new NewsSearchAdapter();
|
|
|
|
layoutManager = new MeasureHeightLayoutManager(this);
|
|
|
|
gamedetail_news_rv.setLayoutManager(layoutManager);
|
|
gamedetail_news_rv.setAdapter(searchAdapter);
|
|
gamedetail_news_rv.addItemDecoration(new VerticalItemDecoration(NewsSearchActivity.this, 1));
|
|
|
|
et_search.setText(searchKey);
|
|
|
|
new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
loadNewsData(page);
|
|
}
|
|
}).start();
|
|
|
|
tv_search.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
searchKey = et_search.getText().toString().trim();
|
|
if (searchKey.length() >= 1) {
|
|
newsEntities.clear();
|
|
searchAdapter.notifyDataSetChanged();
|
|
gamedetail_news_ll_loading.setVisibility(View.VISIBLE);
|
|
reuse_none_data.setVisibility(View.GONE);
|
|
gamedetail_news_cardView.setVisibility(View.GONE);
|
|
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
|
|
handler.postDelayed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
page = 1;
|
|
loadNewsData(page);
|
|
}
|
|
},1000);
|
|
|
|
}else {
|
|
Toast.makeText(NewsSearchActivity.this,"请输入关键字",Toast.LENGTH_SHORT).show();
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
gamedetail_news_rv.setOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
if (newState == RecyclerView.SCROLL_STATE_IDLE &&layoutManager.findLastVisibleItemPosition() + 1 == searchAdapter.getItemCount()
|
|
&&isLoadOver&&!isRemove&& !isNetworkError){
|
|
isLoadOver = false;
|
|
page++;
|
|
loadNewsData(page);
|
|
}
|
|
}
|
|
});
|
|
|
|
reuse_no_connection.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
gamedetail_news_ll_loading.setVisibility(View.VISIBLE);
|
|
gamedetail_news_cardView.setVisibility(View.GONE);
|
|
handler.postDelayed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
loadNewsData(1);
|
|
}
|
|
},1000);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
private void loadNewsData(final int page) {
|
|
DataUtils.onEvent(this, "游戏新闻搜索", searchKey);
|
|
DataCollectionUtils.uploadSearch(this, searchKey, "游戏新闻搜索");
|
|
|
|
RetrofitManager.getApi().getSearchNews(gameId, searchKey, page, 20)
|
|
.map(new Func1<List<NewsEntity>, List<NewsEntity>>() {
|
|
@Override
|
|
public List<NewsEntity> call(List<NewsEntity> list) {
|
|
// 去掉重复数据
|
|
return NewsUtils.removeDuplicateData(newsEntities, list);
|
|
}
|
|
})
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<List<NewsEntity>>() {
|
|
@Override
|
|
public void onResponse(List<NewsEntity> response) {
|
|
isLoadOver = true;
|
|
gamedetail_news_ll_loading.setVisibility(View.GONE);
|
|
gamedetail_news_cardView.setVisibility(View.VISIBLE);
|
|
reuse_none_data.setVisibility(View.GONE);
|
|
|
|
if (response.size() != 0) {
|
|
newsEntities.addAll(response);
|
|
searchAdapter.notifyDataSetChanged();
|
|
} else {
|
|
if (page == 1) {
|
|
gamedetail_news_cardView.setVisibility(View.GONE);
|
|
reuse_none_data.setVisibility(View.VISIBLE);
|
|
}
|
|
|
|
isRemove = true;
|
|
searchAdapter.notifyItemChanged(searchAdapter.getItemCount() - 1);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(HttpException e) {
|
|
if (page == 1) {
|
|
reuse_no_connection.setVisibility(View.VISIBLE);
|
|
gamedetail_news_ll_loading.setVisibility(View.GONE);
|
|
}
|
|
isLoadOver = true;
|
|
toast("加载失败,请检查网络状态");
|
|
isNetworkError = true;
|
|
searchAdapter.notifyItemChanged(searchAdapter.getItemCount() - 1);
|
|
}
|
|
});
|
|
}
|
|
|
|
public class NewsSearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
|
|
@Override
|
|
public int getItemViewType(int position) {
|
|
if (position == newsEntities.size()) {
|
|
return 0;
|
|
} else {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
if (viewType == 0){
|
|
View view = LayoutInflater.from(parent.getContext()).inflate(
|
|
R.layout.refresh_footerview, parent, false);
|
|
return new FooterViewHolder(view);
|
|
} else {
|
|
LinearLayout linearLayout = new LinearLayout(parent.getContext());
|
|
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
|
|
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
|
linearLayout.setBackgroundResource(R.drawable.cardview_item_style);
|
|
|
|
TextView textView = new TextView(parent.getContext());
|
|
textView.setPadding(DisplayUtils.dip2px(NewsSearchActivity.this,10),DisplayUtils.dip2px(NewsSearchActivity.this,10)
|
|
,0, DisplayUtils.dip2px(NewsSearchActivity.this,10));
|
|
textView.setTextColor(Color.parseColor("#3a3a3a"));
|
|
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
|
textView.setSingleLine();
|
|
textView.setTextSize(15);
|
|
|
|
linearLayout.addView(textView);
|
|
|
|
return new ViewHolder(linearLayout);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
|
|
if (holder instanceof ViewHolder){
|
|
ViewHolder viewHolder = (ViewHolder) holder;
|
|
viewHolder.tvTitle.setText(newsEntities.get(position).getTitle());
|
|
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
NewsEntity newsEntity = newsEntities.get(holder.getPosition());
|
|
|
|
Map<String, Object> kv = new HashMap<>();
|
|
kv.put("名字", newsEntity.getTitle());
|
|
kv.put("位置", String.valueOf(holder.getPosition() + 1));
|
|
DataUtils.onEvent(NewsSearchActivity.this, "点击", "游戏新闻搜索", kv);
|
|
|
|
DataCollectionUtils.uploadClick(NewsSearchActivity.this,
|
|
"列表", "游戏新闻搜索", newsEntity.getTitle());
|
|
|
|
// 统计阅读量
|
|
NewsUtils.statNewsViews(newsEntity.getId());
|
|
NewsUtils.startNewsDetailActivity(NewsSearchActivity.this, newsEntity, entrance + "+(游戏新闻搜索)");
|
|
}
|
|
});
|
|
}else if (holder instanceof FooterViewHolder){
|
|
FooterViewHolder viewHolder = (FooterViewHolder) holder;
|
|
viewHolder.itemView.setPadding(0, 0, 0, 0);
|
|
if (isNetworkError) {
|
|
viewHolder.loading.setVisibility(View.GONE);
|
|
viewHolder.hint.setText("加载失败,点击重试");
|
|
viewHolder.itemView.setClickable(true);
|
|
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
isNetworkError = false;
|
|
notifyItemChanged(getItemCount() - 1);
|
|
loadNewsData(page);
|
|
}
|
|
});
|
|
} else if (isRemove) {
|
|
viewHolder.loading.setVisibility(View.GONE);
|
|
viewHolder.hint.setText("加载完毕");
|
|
viewHolder.itemView.setClickable(false);
|
|
} else {
|
|
viewHolder.loading.setVisibility(View.VISIBLE);
|
|
viewHolder.hint.setText("加载中...");
|
|
viewHolder.itemView.setClickable(false);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
if (newsEntities.isEmpty()){
|
|
return 0;
|
|
}
|
|
return newsEntities.size() + 1;
|
|
}
|
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder{
|
|
LinearLayout linearLayout;
|
|
TextView tvTitle;
|
|
public ViewHolder(View itemView) {
|
|
super(itemView);
|
|
linearLayout = (LinearLayout) itemView;
|
|
tvTitle = (TextView) linearLayout.getChildAt(0);
|
|
}
|
|
}
|
|
}
|
|
}
|