209 lines
6.9 KiB
Java
209 lines
6.9 KiB
Java
package com.gh.gamecenter.libao;
|
|
|
|
import android.content.Context;
|
|
import android.os.Bundle;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.design.widget.AppBarLayout;
|
|
import android.support.v4.app.Fragment;
|
|
import android.support.v4.app.FragmentTransaction;
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
import android.text.TextUtils;
|
|
import android.view.KeyEvent;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.inputmethod.EditorInfo;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.widget.EditText;
|
|
import android.widget.TextView;
|
|
|
|
import com.gh.base.fragment.BaseFragment;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.eventbus.EBReuse;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import java.util.List;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
|
|
import static com.gh.gamecenter.LibaoActivity.LIBAO_CLOSEPAGE;
|
|
import static com.gh.gamecenter.LibaoActivity.LIBAO_OPENPAGE;
|
|
|
|
/**
|
|
* Created by khy on 5/09/17.
|
|
*/
|
|
|
|
public class Libao1Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
|
|
|
|
@BindView(R.id.libao_appbar)
|
|
AppBarLayout mAppBar;
|
|
@BindView(R.id.libao_refresh)
|
|
SwipeRefreshLayout mRefreshLayout;
|
|
@BindView(R.id.libao_et_search)
|
|
EditText mLibaoEtSearch;
|
|
@BindView(R.id.libao_tv_back)
|
|
TextView mLibaoTvBack;
|
|
@BindView(R.id.libao_tv_search)
|
|
TextView mLibaoTvSearch;
|
|
|
|
public final static String OPEN_LIBAO_APPBAR = "open_libao_appbar";
|
|
|
|
private LibaoSearchFragment mSearchFragment;
|
|
private LibaoNewFragment mNewFragment;
|
|
|
|
private boolean mIsSearch;
|
|
private boolean mIsTouchScreen;
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.fragment_libao1;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
mRefreshLayout.setColorSchemeResources(R.color.theme);
|
|
mRefreshLayout.setOnRefreshListener(this);
|
|
mRefreshLayout.setEnabled(false);
|
|
|
|
changeFragment();
|
|
|
|
mAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
|
|
@Override
|
|
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
|
|
if (verticalOffset == 0) {
|
|
mRefreshLayout.setEnabled(true);
|
|
} else {
|
|
mRefreshLayout.setEnabled(false);
|
|
}
|
|
int totalScrollRange = appBarLayout.getTotalScrollRange();
|
|
if (totalScrollRange == -verticalOffset) {
|
|
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
|
|
}
|
|
}
|
|
});
|
|
|
|
mLibaoEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
@Override
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
mLibaoTvSearch.performClick();
|
|
}
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
|
|
public void onTouchEvent(MotionEvent ev) {
|
|
switch (ev.getAction()) {
|
|
case MotionEvent.ACTION_DOWN:
|
|
mIsTouchScreen = true;
|
|
break;
|
|
case MotionEvent.ACTION_UP:
|
|
mIsTouchScreen = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void changeFragment() {
|
|
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
|
hideFragments(transaction);
|
|
if (mIsSearch) {
|
|
mSearchFragment = alterFragment(transaction, LibaoSearchFragment.class);
|
|
} else {
|
|
mNewFragment = alterFragment(transaction, LibaoNewFragment.class);
|
|
}
|
|
|
|
transaction.commit();
|
|
}
|
|
|
|
// 将所有的Fragment都置为隐藏状态。
|
|
private void hideFragments(FragmentTransaction transaction) {
|
|
List<Fragment> list = getChildFragmentManager().getFragments();
|
|
if (list != null) {
|
|
for (Fragment fragment : list) {
|
|
transaction.hide(fragment);
|
|
}
|
|
}
|
|
}
|
|
|
|
private <T extends Fragment> T alterFragment(FragmentTransaction transaction, Class<T> cls) {
|
|
T fragmentByTag = (T) getChildFragmentManager().findFragmentByTag(cls.getSimpleName());
|
|
try {
|
|
if (fragmentByTag != null) {
|
|
transaction.show(fragmentByTag);
|
|
if (fragmentByTag instanceof LibaoSearchFragment) {
|
|
((LibaoSearchFragment) fragmentByTag).search();
|
|
}
|
|
} else {
|
|
fragmentByTag = cls.newInstance();
|
|
transaction.add(R.id.layout_fragment_content, fragmentByTag, cls.getSimpleName());
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return fragmentByTag;
|
|
}
|
|
|
|
@OnClick({R.id.libao_tv_back, R.id.libao_tv_search})
|
|
public void onViewClicked(View view) {
|
|
switch (view.getId()) {
|
|
case R.id.libao_tv_back:
|
|
mIsSearch = false;
|
|
break;
|
|
case R.id.libao_tv_search:
|
|
String key = mLibaoEtSearch.getText().toString();
|
|
if (TextUtils.isEmpty(key)) {
|
|
toast(getString(R.string.search_hint));
|
|
return;
|
|
} else if (!mIsSearch) {
|
|
mIsSearch = true;
|
|
}
|
|
break;
|
|
}
|
|
if (mIsSearch) {
|
|
mLibaoTvBack.setVisibility(View.VISIBLE);
|
|
} else {
|
|
mLibaoTvBack.setVisibility(View.GONE);
|
|
}
|
|
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
|
|
changeFragment();
|
|
}
|
|
|
|
public String getSearchKey() {
|
|
return mLibaoEtSearch.getText().toString();
|
|
}
|
|
|
|
@Override
|
|
public void onRefresh() {
|
|
if (mIsSearch) {
|
|
mSearchFragment.onRefresh();
|
|
} else {
|
|
mNewFragment.onRefresh();
|
|
}
|
|
}
|
|
|
|
public void colseRefresh() {
|
|
mRefreshLayout.setRefreshing(false);
|
|
}
|
|
|
|
// 页面切换事件(查看历史礼包的页面)
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBReuse reuse) {
|
|
if (LIBAO_OPENPAGE.equals(reuse.getType())) {
|
|
mAppBar.setVisibility(View.GONE);
|
|
mRefreshLayout.setEnabled(false);
|
|
} else if (LIBAO_CLOSEPAGE.equals(reuse.getType())) {
|
|
mAppBar.setVisibility(View.VISIBLE);
|
|
mRefreshLayout.setEnabled(true);
|
|
} else if (OPEN_LIBAO_APPBAR.equals(reuse.getType()) && !mIsTouchScreen) {
|
|
mAppBar.setExpanded(true, true);
|
|
}
|
|
}
|
|
|
|
}
|