首先来看一下效果图:
MainActivity.xml
下面是不可滑动的viewpager(注释部分也可以实现这个效果)
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* Created by ch on 2017/2/7.
*/
public class NoScrollViewPager extends ViewPager {
private boolean canloop = false;
public NoScrollViewPager(Context context) {
super(context);
}
public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
//去除滚动效果
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
super.setCurrentItem(item, smoothScroll);
}
@Override
public void setCurrentItem(int item) {
super.setCurrentItem(item, false);
}
//禁止滚动
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (canloop) {//如果可以滚动,不消费事件,继续往下传事件
return super.onInterceptTouchEvent(ev);
} else {//如果不可以滚动,事件传给上级的onTouchEvent
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (canloop) {//如果可以滚动,不消费事件,往下传
return super.onTouchEvent(ev);
} else {//如果不可以滚动,事件传给上级的onTouchEvent
return false;
}
}
/* public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
//去除滚动效果
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
super.setCurrentItem(item, false);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}*/
}
public class TabPageAdapter extends FragmentPagerAdapter{
private List fragments;
public TabPageAdapter(FragmentManager fm,List fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
}
MainActivity
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.lt.bottomtabdemo.adapter.TabPageAdapter;
import com.lt.bottomtabdemo.fragment.ClassifyFragment;
import com.lt.bottomtabdemo.fragment.DiscoverFragment;
import com.lt.bottomtabdemo.fragment.HomeFragment;
import com.lt.bottomtabdemo.fragment.MeFragment;
import com.lt.bottomtabdemo.fragment.tab2Fragment;
import com.lt.bottomtabdemo.view.NoScrollViewPager;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity implements
OnCheckedChangeListener {
private NoScrollViewPager mViewPager;
private RadioGroup mRadioGroup;
/**
* ��ť��ûѡ����ʾ��ͼ��
*/
private int[] unselectedIconIds = {R.drawable.ic_tab_home_gray,
R.drawable.ic_tab_classify_gray, R.drawable.ic_tab_discover_gray,
R.drawable.ic_tab_me_gray, R.drawable.ic_tab_classify_gray};
/**
* ��ť��ѡ����ʾ��ͼ��
*/
private int[] selectedIconIds = {R.drawable.ic_tab_home_yellow,
R.drawable.ic_tab_classify_yellow, R.drawable.ic_tab_discover_yellow,
R.drawable.ic_tab_me_yellow, R.drawable.ic_tab_me_yellow};
private List fragments = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initView();
selectPage(0); // Ĭ��ѡ����ҳ
}
protected void init() {
Fragment homeFragment = new HomeFragment();
Fragment classifyFragment = new ClassifyFragment();
Fragment discoverFragment = new DiscoverFragment();
Fragment meFragment = new MeFragment();
Fragment tab2Fragment = new tab2Fragment();
fragments.add(homeFragment);
fragments.add(classifyFragment);
fragments.add(discoverFragment);
fragments.add(meFragment);
fragments.add(tab2Fragment);
}
private void initView() {
mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
mViewPager = (NoScrollViewPager) findViewById(R.id.viewPager);
mViewPager.setHorizontalScrollBarEnabled(true);
mRadioGroup.setOnCheckedChangeListener(this);
TabPageAdapter tabPageAdapter = new TabPageAdapter(
getSupportFragmentManager(), fragments);
mViewPager.setAdapter(tabPageAdapter);
// mViewPager.setOnPageChangeListener(this);
}
/**
* ѡ��ijҳ
*
* @param position ҳ���λ��
*/
private void selectPage(int position) {
// �����е�tab��icon��ɻ�ɫ��
for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
Drawable gray = getResources().getDrawable(unselectedIconIds[i]);
gray.setBounds(0, 0, gray.getMinimumWidth(),
gray.getMinimumHeight());
RadioButton child = (RadioButton) mRadioGroup.getChildAt(i);
child.setCompoundDrawables(null, gray, null, null);
child.setTextColor(getResources().getColor(
R.color.dark_gray));
}
// �л�ҳ��
mViewPager.setCurrentItem(position, false);
// �ı�ͼ��
Drawable yellow = getResources().getDrawable(selectedIconIds[position]);
yellow.setBounds(0, 0, yellow.getMinimumWidth(),
yellow.getMinimumHeight());
RadioButton select = (RadioButton) mRadioGroup.getChildAt(position);
select.setCompoundDrawables(null, yellow, null, null);
select.setTextColor(getResources().getColor(
R.color.yellow));
}
/* @Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
*//* selectPage(position);*//*
}
@Override
public void onPageScrollStateChanged(int state) {
}
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.btn_home: // ��ҳѡ��
selectPage(0);
break;
case R.id.btn_classify: // ����ѡ��
selectPage(1);
break;
case R.id.btn_discover: // ����ѡ��
selectPage(2);
break;
case R.id.btn_me: // ��������ѡ��
selectPage(3);
break;
case R.id.btn_me2: // ��������ѡ��
selectPage(4);
break;
}
}
}
下面就是其中的一个fragment的例子
public class DiscoverFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); textView.setGravity(Gravity.CENTER); textView.setLayoutParams(params); textView.setText("表单"); return textView; } }
public abstract class BaseFragment extends Fragment { boolean isImmersive = false; boolean isTranslucentNavigation = false; /* * 1.onCreateView是创建的时候调用,onViewCreated是onCreateView后被触发的事件,前后关系 * */ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(getContentViewId(), container, false); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initData(); } @Override public void onDestroyView() { super.onDestroyView(); } public static boolean hasKitKat() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; } public static boolean hasLollipop() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; } public boolean isTranslucentNavigation() { return isTranslucentNavigation; } public void setTranslucentNavigation(boolean translucentNavigation) { isTranslucentNavigation = translucentNavigation; } //沉浸式状态栏结束 public abstract int getContentViewId(); protected abstract void initData(); public abstract void TitleBar(boolean isImmersive); }
当获取id时,使用getView().就行了
大致的应该就是这些,如果缺少东西,欢迎大家提意见