346 lines
14 KiB
Java
346 lines
14 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.Color;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.support.v7.widget.CardView;
|
|
import android.support.v7.widget.RecyclerView;
|
|
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.android.volley.Response;
|
|
import com.android.volley.VolleyError;
|
|
import com.gh.base.AppController;
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.MeasureHeightLayoutManager;
|
|
import com.gh.common.view.VerticalItemDecoration;
|
|
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
|
|
import com.gh.gamecenter.entity.NewsEntity;
|
|
import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Created by khy on 2016/8/22.
|
|
*/
|
|
public class NewsSearchActivity extends BaseActivity {
|
|
|
|
private RecyclerView mNewsRecyclerView;
|
|
private List<NewsEntity> newsEntities;
|
|
private NewsSearchAdapter searchAdapter;
|
|
private TextView tvSearch;
|
|
private LinearLayout reuse_none_data;
|
|
private EditText edSearch;
|
|
private LinearLayout llLoading;
|
|
private LinearLayout noConnection;
|
|
private CardView cardView;
|
|
|
|
private MeasureHeightLayoutManager layoutManager;
|
|
|
|
private boolean isLoadOver = true;
|
|
private boolean isRemove = false;
|
|
private boolean isNetworkError = false;
|
|
|
|
private String gameName;
|
|
private String searchKey;
|
|
private String gameId;
|
|
|
|
private Handler handler = new Handler();
|
|
|
|
private int page = 1;
|
|
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Intent intent = getIntent();
|
|
gameName = intent.getExtras().getString("gameName");
|
|
searchKey = intent.getExtras().getString("searchKey");
|
|
gameId = intent.getExtras().getString("gameId");
|
|
|
|
View view = View.inflate(this, R.layout.activity_gamedetail_news, null);
|
|
init(view, gameName);
|
|
|
|
newsEntities = new ArrayList<>();
|
|
searchAdapter = new NewsSearchAdapter();
|
|
|
|
findViewById(R.id.gamedetail_news_type_ll).setVisibility(View.GONE);
|
|
findViewById(R.id.ll_search).setVisibility(View.VISIBLE);
|
|
|
|
mNewsRecyclerView = (RecyclerView) findViewById(R.id.gamedetail_news_rv);
|
|
noConnection = (LinearLayout) findViewById(R.id.reuse_no_connection);
|
|
llLoading = (LinearLayout) findViewById(R.id.gamedetail_news_ll_loading);
|
|
cardView = (CardView) findViewById(R.id.gamedetail_news_cardView);
|
|
reuse_none_data = (LinearLayout) findViewById(R.id.reuse_none_data);
|
|
|
|
layoutManager = new MeasureHeightLayoutManager(this);
|
|
|
|
mNewsRecyclerView.setLayoutManager(layoutManager);
|
|
mNewsRecyclerView.setAdapter(searchAdapter);
|
|
mNewsRecyclerView.addItemDecoration(new VerticalItemDecoration(NewsSearchActivity.this, 1));
|
|
|
|
edSearch = (EditText) findViewById(R.id.et_search);
|
|
tvSearch = (TextView) findViewById(R.id.tv_search);
|
|
edSearch.setText(searchKey);
|
|
|
|
new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
loadNewsData(page);
|
|
}
|
|
}).start();
|
|
|
|
tvSearch.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
searchKey = edSearch.getText().toString().trim();
|
|
if (searchKey.length() >= 1){
|
|
newsEntities.clear();
|
|
searchAdapter.notifyDataSetChanged();
|
|
llLoading.setVisibility(View.VISIBLE);
|
|
reuse_none_data.setVisibility(View.GONE);
|
|
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(getApplicationContext(),"请输入关键字",Toast.LENGTH_SHORT).show();
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
mNewsRecyclerView.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);
|
|
}
|
|
}
|
|
});
|
|
|
|
noConnection.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
noConnection.setVisibility(View.GONE);
|
|
llLoading.setVisibility(View.VISIBLE);
|
|
cardView.setVisibility(View.GONE);
|
|
handler.postDelayed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
loadNewsData(1);
|
|
}
|
|
},1000);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
private void loadNewsData(final int page) {
|
|
String url = Config.HOST + "v1d45/search/news?game_id=" + gameId
|
|
+ "&keyword=" + Uri.encode(searchKey) + "&page="+ page + "&limit=20";
|
|
final JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(url,
|
|
new Response.Listener<JSONArray>() {
|
|
@Override
|
|
public void onResponse(JSONArray response) {
|
|
Gson gson = new Gson();
|
|
ArrayList<NewsEntity> news = gson.fromJson(
|
|
response.toString(),
|
|
new TypeToken<ArrayList<NewsEntity>>() {
|
|
}.getType());
|
|
newsEntities.addAll(news);
|
|
searchAdapter.notifyDataSetChanged();
|
|
isLoadOver = true;
|
|
llLoading.setVisibility(View.GONE);
|
|
cardView.setVisibility(View.VISIBLE);
|
|
if (newsEntities.size() == 0){
|
|
cardView.setVisibility(View.GONE);
|
|
reuse_none_data.setVisibility(View.VISIBLE);
|
|
}else {
|
|
cardView.setVisibility(View.VISIBLE);
|
|
reuse_none_data.setVisibility(View.GONE);
|
|
}
|
|
if (news.isEmpty() || (news.size() < 20)) {
|
|
isRemove = true;
|
|
searchAdapter.notifyItemChanged(searchAdapter.getItemCount() - 1);
|
|
}
|
|
}
|
|
}, new Response.ErrorListener() {
|
|
@Override
|
|
public void onErrorResponse(VolleyError error) {
|
|
if (page ==1){
|
|
noConnection.setVisibility(View.VISIBLE);
|
|
llLoading.setVisibility(View.GONE);
|
|
}
|
|
isLoadOver = true;
|
|
toast("加载失败,请检查网络状态");
|
|
isNetworkError = true;
|
|
searchAdapter.notifyItemChanged(searchAdapter.getItemCount() - 1);
|
|
}
|
|
});
|
|
AppController.addToRequestQueue(request, GameNewsActivity.class);
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
gameName = null;
|
|
searchKey = null;
|
|
gameId = null;
|
|
mNewsRecyclerView = null;
|
|
newsEntities = null;
|
|
searchAdapter = null;
|
|
tvSearch = null;
|
|
reuse_none_data = null;
|
|
edSearch = null;
|
|
llLoading = null;
|
|
noConnection = null;
|
|
cardView = null;
|
|
layoutManager = null;
|
|
handler = null;
|
|
}
|
|
|
|
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(getApplicationContext());
|
|
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
|
|
linearLayout.setBackgroundResource(R.drawable.cardview_item_style);
|
|
|
|
TextView tvType = new TextView(getApplicationContext());
|
|
tvType.setTextSize(12);
|
|
tvType.setPadding(DisplayUtils.dip2px(NewsSearchActivity.this,1), 0, DisplayUtils.dip2px(NewsSearchActivity.this,1), 0);
|
|
|
|
TextView textView = new TextView(getApplicationContext());
|
|
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();
|
|
|
|
linearLayout.addView(tvType);
|
|
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;
|
|
|
|
if ("活动".equals(newsEntities.get(position).getType())){
|
|
viewHolder.tvType.setTextColor(Color.WHITE);
|
|
viewHolder.tvType.setBackgroundResource(R.drawable.textview_orange_style);
|
|
} else if ("公告".equals(newsEntities.get(position).getType())){
|
|
viewHolder.tvType.setTextColor(Color.WHITE);
|
|
viewHolder.tvType.setBackgroundResource(R.drawable.textview_red_style);
|
|
} else {
|
|
viewHolder.tvType.setTextColor(Color.WHITE);
|
|
viewHolder.tvType.setBackgroundResource(R.drawable.textview_blue_style);
|
|
}
|
|
viewHolder.tvType.setText(newsEntities.get(position).getType());
|
|
|
|
viewHolder.tvTitle.setText(newsEntities.get(position).getTitle());
|
|
viewHolder.tvTitle.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
Intent intent = new Intent(getApplicationContext(),NewsDetailActivity.class);
|
|
intent.putExtra("newsId", newsEntities.get(holder.getPosition()).getId());
|
|
intent.putExtra("entrance", "游戏详情-全部资讯");
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
}else if (holder instanceof FooterViewHolder){
|
|
FooterViewHolder viewHolder = (FooterViewHolder) holder;
|
|
viewHolder.itemView.setPadding(0, 0, 0, 0);
|
|
if (isNetworkError) {
|
|
viewHolder.footerview_progressbar.setVisibility(View.GONE);
|
|
viewHolder.footerview_tv_loading.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.footerview_progressbar.setVisibility(View.GONE);
|
|
viewHolder.footerview_tv_loading.setText("加载完毕");
|
|
viewHolder.itemView.setClickable(false);
|
|
} else {
|
|
viewHolder.footerview_progressbar.setVisibility(View.VISIBLE);
|
|
viewHolder.footerview_tv_loading.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 tvType;
|
|
TextView tvTitle;
|
|
public ViewHolder(View itemView) {
|
|
super(itemView);
|
|
linearLayout = (LinearLayout) itemView;
|
|
tvType = (TextView) linearLayout.getChildAt(0);
|
|
tvTitle = (TextView) linearLayout.getChildAt(1);
|
|
}
|
|
}
|
|
}
|
|
}
|