BaseFragment
public abstract class BaseFragment extends Fragment {
private boolean isCreate = false;
private Activity activity;
private View mView;
public Context getContext() {
if (activity == null)
return App.getInstance();
return activity;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isCreate = true;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
activity = getActivity();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(setLayout(), container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mView = view;
setPresenter();
setBinding(view);
if (savedInstanceState == null) {
initEventAndData();
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser && isCreate) {
setBinding(mView);
initEventAndData();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
protected abstract int setLayout();
protected abstract void setPresenter();
protected abstract void setBinding(View view);
protected abstract void initEventAndData();
protected void hint(String msg) {
SnackBarUtil.showShort(((Activity) getContext()).getWindow().getDecorView(), msg);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@color/colorLT_bg">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/title"
android:textColor="@color/colorLT_title"
android:textSize="20sp"/>
<ImageView
android:id="@+id/iv_setting"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:padding="7dp"
android:src="@drawable/set_icon"/>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2px"
android:background="@color/colorLT_text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp">
<LinearLayout
android:id="@+id/ll_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_search"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="@drawable/wechat_p"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="搜索"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_bill"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_bill"
android:layout_width="match_parent"
android:layout_height="30dp"
android:src="@drawable/people"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="钱包"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_statistics"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_statistics"
android:layout_width="match_parent"
android:layout_height="30dp"
android:src="@drawable/find"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="统计"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</layout>
FragmentPagerAdapter
public class FragmentPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
public FragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
SearchFragment
public class SearchFragment extends BaseFragment implements SearchContract.View {
private SearchPresenter mPresenter;
private FragmentSearchBinding mBinding;
@Override
protected void setPresenter() {
mPresenter = new SearchPresenter(this);
}
@Override
protected int setLayout() {
return R.layout.fragment_search;
}
@Override
protected void setBinding(View view) {
mBinding = DataBindingUtil.bind(view);
}
@Override
protected void initEventAndData() {
}
}
MainActivity
private void initViewPager() {
List<Fragment> fragments = new ArrayList<>();
fragments.add(new SearchFragment());
fragments.add(new BillFragment());
fragments.add(new StatisticsFragment());
FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), fragments);
mBinding.vpMain.setAdapter(fragmentPagerAdapter);
mBinding.vpMain.setOffscreenPageLimit(2);
mBinding.vpMain.setCurrentItem(0);
mBinding.vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
mBinding.ivSearch.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.wechat_p));
mBinding.ivBill.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.people));
mBinding.ivStatistics.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.find));
break;
case 1:
mBinding.ivSearch.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.wechat));
mBinding.ivBill.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.people_p));
mBinding.ivStatistics.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.find));
break;
case 2:
mBinding.ivSearch.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.wechat));
mBinding.ivBill.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.people));
mBinding.ivStatistics.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.find_p));
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_setting:
PreferenceActivity.actionStart(this);
break;
case R.id.ll_search:
mBinding.vpMain.setCurrentItem(0);
break;
case R.id.ll_bill:
mBinding.vpMain.setCurrentItem(1);
break;
case R.id.ll_statistics:
mBinding.vpMain.setCurrentItem(2);
break;
}
}