Java tutorial
/* * * * Copyright 2015 Van Shu * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * */ package com.mobimvp.cliques.ui; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPager; import android.widget.ListView; import com.astuetz.PagerSlidingTabStrip; import com.mobimvp.cliques.R; import com.mobimvp.cliques.ui.fragment.ShotsFragment; import com.mobimvp.cliques.ui.widgets.SlidingTabLayout; import com.mobimvp.cliques.util.AnalyticsManager; import java.util.HashSet; import java.util.Set; import static com.mobimvp.cliques.util.LogUtils.LOGD; import static com.mobimvp.cliques.util.LogUtils.makeLogTag; public class ShotsActivity extends BaseActivity implements ShotsFragment.Listener { private static final String TAG = makeLogTag(ShotsActivity.class); private ViewPager mViewPager; private ViewPagerAdapter mViewPagerAdapter; private String[] mTitle = new String[] { "POPULAR", "RECENT", "MOST VIEWED" }; private Set<ShotsFragment> myShotFragment = new HashSet<>(); private SlidingTabLayout mSlidingTabLayout = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (isFinishing()) { return; } setContentView(R.layout.activity_shots); mViewPager = (ViewPager) findViewById(R.id.viewpager); mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mViewPagerAdapter); mViewPager.setOffscreenPageLimit(mTitle.length); mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs); mSlidingTabLayout.setCustomTabView(R.layout.tab_indicator, android.R.id.text1); Resources res = getResources(); mSlidingTabLayout.setSelectedIndicatorColors(res.getColor(R.color.tab_selected_strip)); mSlidingTabLayout.setDistributeEvenly(true); mSlidingTabLayout.setViewPager(mViewPager); if (mSlidingTabLayout != null) { mSlidingTabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { enableDisableSwipeRefresh(state == ViewPager.SCROLL_STATE_IDLE); } }); } ListView listView = (ListView) findViewById(R.id.listview); if (listView != null) { enableActionBarAutoHide(listView); enableDisableSwipeRefresh(true); } } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); } @Override protected void requestDataRefresh() { super.requestDataRefresh(); onRefreshingStateChanged(false); } @Override protected int getSelfNavDrawerItem() { return NAVDRAWER_ITEM_SHOTS; } @Override protected void onResume() { super.onResume(); invalidateOptionsMenu(); } @Override public boolean canSwipeRefreshChildScrollUp() { for (ShotsFragment fragment : myShotFragment) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { if (!fragment.getUserVisibleHint()) { continue; } } return ViewCompat.canScrollVertically(fragment.getListView(), -1); } return false; } @Override public void onFragmentViewCreated(ShotsFragment fragment) { fragment.getListView() .addHeaderView(getLayoutInflater().inflate(R.layout.reserve_action_bar_space_header_view, null)); } @Override public void onFragmentAttached(ShotsFragment fragment) { myShotFragment.add(fragment); } @Override public void onFragmentDetached(ShotsFragment fragment) { myShotFragment.remove(fragment); } public class ViewPagerAdapter extends FragmentPagerAdapter { public ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: ShotsFragment popularFragment = new ShotsFragment(); Bundle bundle = new Bundle(); bundle.putString("sort", ShotsFragment.POPULAR); popularFragment.setArguments(bundle); return popularFragment; case 1: ShotsFragment recentFragment = new ShotsFragment(); bundle = new Bundle(); bundle.putString("sort", ShotsFragment.RECENT); recentFragment.setArguments(bundle); return recentFragment; case 2: ShotsFragment viewsFragment = new ShotsFragment(); bundle = new Bundle(); bundle.putString("sort", ShotsFragment.VIEWS); viewsFragment.setArguments(bundle); return viewsFragment; } return null; } @Override public int getCount() { return mTitle.length; } @Override public CharSequence getPageTitle(int position) { return mTitle[position]; } } }