436 lines
14 KiB
Java
436 lines
14 KiB
Java
package com.gh.gamecenter.game;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v4.app.Fragment;
|
|
import android.support.v4.app.FragmentManager;
|
|
import android.support.v4.app.FragmentPagerAdapter;
|
|
import android.support.v4.app.FragmentTransaction;
|
|
import android.support.v4.view.ViewPager;
|
|
import android.support.v4.view.ViewPager.OnPageChangeListener;
|
|
import android.text.TextUtils;
|
|
import android.util.DisplayMetrics;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.View.OnClickListener;
|
|
import android.view.ViewGroup;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.LinearLayout.LayoutParams;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.util.DataUtils;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.download.DownloadManager;
|
|
import com.gh.gamecenter.ConcernActivity;
|
|
import com.gh.gamecenter.DownloadManagerActivity;
|
|
import com.gh.gamecenter.MainActivity;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.SearchActivity;
|
|
import com.gh.gamecenter.eventbus.EBRedDot;
|
|
import com.gh.gamecenter.eventbus.EBTopState;
|
|
import com.gh.gamecenter.eventbus.EBUISwitch;
|
|
import com.gh.gamecenter.manager.DataCollectionManager;
|
|
import com.gh.gamecenter.manager.PackageManager;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import de.greenrobot.event.EventBus;
|
|
|
|
/**
|
|
*
|
|
* @author 温冠超
|
|
* @email 294299195@qq.com
|
|
* @date 2015-8-8 主页fragment3页面
|
|
*/
|
|
public class GameFragment extends Fragment implements OnClickListener,
|
|
OnPageChangeListener {
|
|
|
|
private View view;
|
|
private View slideLine;
|
|
private ViewPager viewPager;
|
|
private TextView jingpinText;
|
|
private TextView zuixinText;
|
|
private TextView danjiText;
|
|
private TextView downloadHint;
|
|
private TextView searchHint;
|
|
|
|
private RelativeLayout.LayoutParams rparams;
|
|
|
|
private String hint;
|
|
|
|
private int width;
|
|
private int currentItem;
|
|
|
|
private Handler handler = new Handler();
|
|
|
|
@Override
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
super.onSaveInstanceState(outState);
|
|
outState.putInt("currentItem", currentItem);
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
view = View.inflate(getActivity(), R.layout.plugin_container_fragments, null);
|
|
|
|
SharedPreferences sp = getActivity().getSharedPreferences(
|
|
Config.PREFERENCE, Context.MODE_PRIVATE);
|
|
|
|
LinearLayout home_actionbar = (LinearLayout) view.findViewById(R.id.home_actionbar);
|
|
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
|
|
sp.getInt("actionbar_height", DisplayUtils.dip2px(getActivity(), 48)));
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
int top = DisplayUtils.getInternalDimensionSize(getResources(), "status_bar_height");
|
|
home_actionbar.setPadding(0, top, 0, 0);
|
|
params.height += top;
|
|
}
|
|
|
|
home_actionbar.setLayoutParams(params);
|
|
|
|
initActionBar();
|
|
|
|
jingpinText = (TextView) view.findViewById(R.id.jingpin_text);
|
|
zuixinText = (TextView) view.findViewById(R.id.zuixin_text);
|
|
danjiText = (TextView) view.findViewById(R.id.danji_text);
|
|
|
|
jingpinText.setOnClickListener(this);
|
|
zuixinText.setOnClickListener(this);
|
|
danjiText.setOnClickListener(this);
|
|
|
|
slideLine = view.findViewById(R.id.slide_line);
|
|
DisplayMetrics outMetrics = new DisplayMetrics();
|
|
getActivity().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
|
|
width = outMetrics.widthPixels / 3;
|
|
rparams = new RelativeLayout.LayoutParams(width / 2,
|
|
DisplayUtils.dip2px(getActivity(), 2));
|
|
rparams.addRule(RelativeLayout.BELOW, R.id.plugin_ll_top);
|
|
|
|
viewPager = (ViewPager) view.findViewById(R.id.pager);
|
|
viewPager.setAdapter(new FragmentAdapter(getChildFragmentManager()));
|
|
viewPager.addOnPageChangeListener(this);
|
|
|
|
currentItem = 0;
|
|
if (savedInstanceState != null) {
|
|
currentItem = savedInstanceState.getInt("currentItem");
|
|
}
|
|
viewPager.setCurrentItem(currentItem);
|
|
|
|
rparams.leftMargin = width * currentItem + width / 4;
|
|
slideLine.setLayoutParams(rparams);
|
|
if (currentItem == 0) {
|
|
jingpinText.setClickable(false);
|
|
zuixinText.setClickable(true);
|
|
danjiText.setClickable(true);
|
|
} else if (currentItem == 1) {
|
|
jingpinText.setClickable(true);
|
|
zuixinText.setClickable(false);
|
|
danjiText.setClickable(true);
|
|
} else {
|
|
jingpinText.setClickable(true);
|
|
zuixinText.setClickable(true);
|
|
danjiText.setClickable(false);
|
|
}
|
|
|
|
EventBus.getDefault().register(this);
|
|
|
|
handler.postDelayed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
EventBus.getDefault().post(new EBUISwitch("GameFragment", viewPager.getCurrentItem()));
|
|
}
|
|
}, 100);
|
|
}
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
if (container != null) {
|
|
container.removeView(view);
|
|
}
|
|
return view;
|
|
}
|
|
|
|
private void initActionBar() {
|
|
view.findViewById(R.id.actionbar_rl_download).setOnClickListener(this);
|
|
|
|
view.findViewById(R.id.actionbar_iv_search).setOnClickListener(this);
|
|
|
|
if (Config.isShow) {
|
|
view.findViewById(R.id.actionbar_rl_download).setVisibility(View.VISIBLE);
|
|
} else {
|
|
view.findViewById(R.id.actionbar_rl_download).setVisibility(View.GONE);
|
|
}
|
|
|
|
downloadHint = (TextView) view.findViewById(R.id.action_tip);
|
|
int updateSize = PackageManager.getUpdateListSize();
|
|
int downloadSize = DownloadManager.getInstance(getActivity()).getAll().size();
|
|
if (downloadSize != 0) {
|
|
downloadHint.setVisibility(View.VISIBLE);
|
|
downloadHint.setText(String.valueOf(downloadSize));
|
|
int width = DisplayUtils.dip2px(getActivity(), 12);
|
|
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(
|
|
width, width);
|
|
rparams.topMargin = DisplayUtils.dip2px(getActivity(), 8);
|
|
rparams.rightMargin = DisplayUtils.dip2px(getActivity(), 2);
|
|
rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
|
downloadHint.setLayoutParams(rparams);
|
|
} else if (updateSize != 0) {
|
|
downloadHint.setText("");
|
|
int width = DisplayUtils.dip2px(getActivity(), 10);
|
|
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(
|
|
width, width);
|
|
rparams.topMargin = DisplayUtils.dip2px(getActivity(), 8);
|
|
rparams.rightMargin = DisplayUtils.dip2px(getActivity(), 3);
|
|
rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
|
downloadHint.setLayoutParams(rparams);
|
|
} else {
|
|
downloadHint.setVisibility(View.GONE);
|
|
}
|
|
|
|
searchHint = (TextView) view.findViewById(R.id.actionbar_search_input);
|
|
searchHint.setOnClickListener(this);
|
|
if (!TextUtils.isEmpty(MainActivity.searchHint)) {
|
|
hint = MainActivity.searchHint;
|
|
searchHint.setHint(hint);
|
|
}
|
|
|
|
view.findViewById(R.id.actionbar_notification).setOnClickListener(this);
|
|
|
|
if (downloadSize != 0 || updateSize != 0) {
|
|
EventBus.getDefault().post(new EBTopState("显示", null));
|
|
}
|
|
}
|
|
|
|
private class FragmentAdapter extends FragmentPagerAdapter {
|
|
|
|
public FragmentAdapter(FragmentManager fm) {
|
|
super(fm);
|
|
}
|
|
|
|
@Override
|
|
public Fragment getItem(int position) {
|
|
if (position == 0) {
|
|
return new Game1Fragment();
|
|
} else if (position == 1) {
|
|
return new NewGame2Fragment();
|
|
} else {
|
|
return new Game3Fragment();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getCount() {
|
|
return 3;
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
final int id = v.getId();
|
|
if (id == R.id.actionbar_notification) {
|
|
DataUtils.onEvent(getActivity(), "主页", "关注图标");
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("location", "关注图标");
|
|
map.put("createOn", System.currentTimeMillis() / 1000);
|
|
map.put("page", "主页");
|
|
DataCollectionManager.onEvent(getActivity(), "click-item", map);
|
|
|
|
startActivity(new Intent(getActivity(), ConcernActivity.class));
|
|
} else if (id == R.id.actionbar_rl_download) {
|
|
DataUtils.onEvent(getActivity(), "主页", "下载图标");
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("location", "下载图标");
|
|
map.put("createOn", System.currentTimeMillis() / 1000);
|
|
map.put("page", "主页");
|
|
DataCollectionManager.onEvent(getActivity(), "click-item", map);
|
|
|
|
startActivity(new Intent(getActivity(), DownloadManagerActivity.class));
|
|
} else if (id == R.id.actionbar_search_input) {
|
|
DataUtils.onEvent(getActivity(), "主页", "搜索框");
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("location", "搜索框");
|
|
map.put("createOn", System.currentTimeMillis() / 1000);
|
|
map.put("page", "主页");
|
|
DataCollectionManager.onEvent(getActivity(), "click-item", map);
|
|
|
|
Intent goSearch = new Intent(getActivity(), SearchActivity.class);
|
|
goSearch.putExtra("clicked", false);
|
|
goSearch.putExtra("hint", hint);
|
|
startActivity(goSearch);
|
|
} else if (id == R.id.actionbar_iv_search) {
|
|
DataUtils.onEvent(getActivity(), "主页", "搜索图标");
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("location", "搜索图标");
|
|
map.put("createOn", System.currentTimeMillis() / 1000);
|
|
map.put("page", "主页");
|
|
DataCollectionManager.onEvent(getActivity(), "click-item", map);
|
|
|
|
Intent searchIntent = new Intent(getActivity(),
|
|
SearchActivity.class);
|
|
searchIntent.putExtra("clicked", true);
|
|
searchIntent.putExtra("hint", hint);
|
|
startActivity(searchIntent);
|
|
} else if (id == R.id.jingpin_text) {
|
|
viewPager.setCurrentItem(0);
|
|
} else if (id == R.id.zuixin_text) {
|
|
viewPager.setCurrentItem(1);
|
|
} else if (id == R.id.danji_text) {
|
|
viewPager.setCurrentItem(2);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPageSelected(int position) {
|
|
if (position == 0) {
|
|
jingpinText.setClickable(false);
|
|
zuixinText.setClickable(true);
|
|
danjiText.setClickable(true);
|
|
jingpinText.setTextColor(getResources().getColor(R.color.theme));
|
|
zuixinText.setTextColor(getResources().getColor(R.color.title));
|
|
danjiText.setTextColor(getResources().getColor(R.color.title));
|
|
} else if (position == 1) {
|
|
jingpinText.setClickable(true);
|
|
zuixinText.setClickable(false);
|
|
danjiText.setClickable(true);
|
|
jingpinText.setTextColor(getResources().getColor(R.color.title));
|
|
zuixinText.setTextColor(getResources().getColor(R.color.theme));
|
|
danjiText.setTextColor(getResources().getColor(R.color.title));
|
|
} else if (position == 2) {
|
|
jingpinText.setClickable(true);
|
|
zuixinText.setClickable(true);
|
|
danjiText.setClickable(false);
|
|
jingpinText.setTextColor(getResources().getColor(R.color.title));
|
|
zuixinText.setTextColor(getResources().getColor(R.color.title));
|
|
danjiText.setTextColor(getResources().getColor(R.color.theme));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPageScrolled(int position, float positionOffset,
|
|
int positionOffsetPixels) {
|
|
if (positionOffset != 0) {
|
|
rparams.leftMargin = (int) (width * (position + positionOffset) + width / 4);
|
|
slideLine.setLayoutParams(rparams);
|
|
} else {
|
|
if (currentItem != viewPager.getCurrentItem()) {
|
|
currentItem = viewPager.getCurrentItem();
|
|
EventBus.getDefault().post(new EBUISwitch("GameFragment", currentItem));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPageScrollStateChanged(int state) {
|
|
|
|
}
|
|
|
|
public void onEventMainThread(EBUISwitch busNine) {
|
|
if ("MainActivity".equals(busNine.getFrom())) {
|
|
if (busNine.getPosition() == 0) {
|
|
EventBus.getDefault().post(
|
|
new EBUISwitch("GameFragment", viewPager.getCurrentItem()));
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBTopState busTen) {
|
|
if ("搜索".equals(busTen.getType())) {
|
|
hint = busTen.getSearchHint();
|
|
searchHint.setHint(busTen.getSearchHint());
|
|
} else if ("隐藏".equals(busTen.getType())) {
|
|
if (downloadHint != null) {
|
|
downloadHint.setVisibility(View.GONE);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBRedDot busEight) {
|
|
updateRedDot(busEight.getType());
|
|
}
|
|
|
|
public void updateRedDot(int type) {
|
|
int downloadSize = DownloadManager.getInstance(getActivity()).getAll().size();
|
|
if (type == 1 && downloadSize == 0) {
|
|
downloadHint.setVisibility(View.VISIBLE);
|
|
downloadHint.setText(String.valueOf(downloadSize + type));
|
|
int width = DisplayUtils.dip2px(getActivity(), 12);
|
|
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(width, width);
|
|
rparams.topMargin = DisplayUtils.dip2px(getActivity(), 8);
|
|
rparams.rightMargin = DisplayUtils.dip2px(getActivity(), 2);
|
|
rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
|
downloadHint.setLayoutParams(rparams);
|
|
} else if (type == -1 && (downloadSize == 0 || downloadSize == 1)) {
|
|
int updateSize = PackageManager.getUpdateList().size();
|
|
if (updateSize != 0) {
|
|
downloadHint.setVisibility(View.VISIBLE);
|
|
downloadHint.setText("");
|
|
int width = DisplayUtils.dip2px(getActivity(), 10);
|
|
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(width, width);
|
|
rparams.topMargin = DisplayUtils.dip2px(getActivity(), 8);
|
|
rparams.rightMargin = DisplayUtils.dip2px(getActivity(), 3);
|
|
rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
|
downloadHint.setLayoutParams(rparams);
|
|
} else {
|
|
downloadHint.setVisibility(View.GONE);
|
|
}
|
|
} else if (downloadSize != 0) {
|
|
downloadHint.setVisibility(View.VISIBLE);
|
|
downloadHint.setText(String.valueOf(downloadSize + type));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onHiddenChanged(boolean hidden) {
|
|
super.onHiddenChanged(hidden);
|
|
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
|
List<Fragment> list = getChildFragmentManager().getFragments();
|
|
if (list != null) {
|
|
if (hidden) {
|
|
for (Fragment fragment : getChildFragmentManager().getFragments()) {
|
|
transaction.hide(fragment);
|
|
}
|
|
} else {
|
|
for (Fragment fragment : getChildFragmentManager().getFragments()) {
|
|
transaction.show(fragment);
|
|
}
|
|
}
|
|
}
|
|
transaction.commit();
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
EventBus.getDefault().unregister(this);
|
|
view = null;
|
|
slideLine = null;
|
|
viewPager = null;
|
|
jingpinText = null;
|
|
zuixinText = null;
|
|
danjiText = null;
|
|
downloadHint = null;
|
|
searchHint = null;
|
|
rparams = null;
|
|
hint = null;
|
|
handler = null;
|
|
}
|
|
|
|
}
|