静态碎片切换:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/fragment3_RG"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/fragment3_Bt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/activity_press"
android:button="@null" />
<RadioButton
android:id="@+id/fragment3_Bt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/home_press"
android:button="@null" />
</RadioGroup>
<FrameLayout
android:id="@+id/jiekou01_FLoo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a1cae1e7">
</FrameLayout>
</LinearLayout>
---------------------------------------------------------------------------------------------------------
活动代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.fragment3_Bt1).setOnClickListener(this);
findViewById(R.id.fragment3_Bt2).setOnClickListener(this);
}
//添加碎片的实现方法(3个按钮点击实现方法,后续switch使用动态)
private void init(Fragment fragment, String TAG) {
//添加碎片
FragmentManager fm = getSupportFragmentManager();
//开启事务
FragmentTransaction ft = fm.beginTransaction();
//添加碎片
ft.replace(R.id.jiekou01_FLoo, fragment);
ft.addToBackStack(null);
//提交事务
ft.commit();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fragment3_Bt1:
Fragment4 fragment4 = new Fragment4();
init(fragment4, "fragment4");
break;
case R.id.fragment3_Bt2:
Fragment5 fragment5 = new Fragment5();
init(fragment5, "fragment5");
break;
}
}
==============================================================================================================================================
动态切换碎片viewpage
布局:
<RadioGroup
android:id="@+id/gvhot_activity_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/xiangguandu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="相关度" />
<RadioButton
android:id="@+id/zuixin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="最新" />
</RadioGroup>
<View
android:id="@+id/gvhot_activity_view"
android:layout_width="100dp"
android:layout_height="0.5dp"
android:layout_below="@+id/gvhot_activity_rg"
android:background="#a61f1f" />
<android.support.v4.view.ViewPager
android:id="@+id/gvhot_activity_VP"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/gvhot_activity_rg" />
---------------------------------------------------------------------------------------------------------
代码:public class MainViewActivity extends AppCompatActivity {
FragmentManager fm;
int mCurronIndex;
private List<Fragment> fragmentList; //碎片集合
private List<RadioButton> buttonList; //按鈕集合
private ViewPager viewPager_gvhot;
private View gvhot_activity_view;
private int line_width;
//适配器监听事件
private FragmentPagerAdapter mFragmentPagerAdapter;
//視圖显示操作
private ViewPager.OnPageChangeListener mOnPageChangeListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
initDate();
fm = getSupportFragmentManager();
setLineWidth();
viewPager_gvhot = (ViewPager) findViewById(R.id.gvhot_activity_VP);
mFragmentPagerAdapter = new FragmentPagerAdapter(fm) {
//返回某一个碎片集合
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
//返回某一个碎片数字大小
@Override
public int getCount() {
return fragmentList.size();
}
};
viewPager_gvhot.setAdapter(mFragmentPagerAdapter);
buttonList.get(0).setTextColor(getResources().getColor(R.color.colorPrimary));
//监听
mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
//图片在改变(拖住图片不放)
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) gvhot_activity_view.getLayoutParams();
View parent = (View) gvhot_activity_view.getParent();
int measuredWidth = parent.getMeasuredWidth();
int i = measuredWidth / 2;
lp.leftMargin = (int) (i * (position + positionOffset));
gvhot_activity_view.setLayoutParams(lp);
}
//图片被选中,选中后操作(选中后的图片缓存下来)
@Override
public void onPageSelected(int position) {
mCurronIndex = position;
setColor();
buttonList.get(position).setTextColor(getResources().getColor(R.color.colorPrimary));
}
//图片在滑动(拖拽图片松开时滑动中)
@Override
public void onPageScrollStateChanged(int state) {
}
};
viewPager_gvhot.addOnPageChangeListener(mOnPageChangeListener);
for (int i = 0; i < buttonList.size(); i++) {
buttonList.get(i).setTag(i);
final int finalI = i;
buttonList.get(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setColor();
viewPager_gvhot.setCurrentItem((Integer) v.getTag());
buttonList.get(finalI).setTextColor(getResources().getColor(R.color.colorPrimary));
}
});
}
}
private void setLineWidth() {
Display defaultDisplay = this.getWindow().getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
defaultDisplay.getMetrics(outMetrics);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) gvhot_activity_view.getLayoutParams();
int width = outMetrics.widthPixels;
line_width = width / 2;
lp.width = line_width;
gvhot_activity_view.setLayoutParams(lp);
}
private void setColor() {
for (int i = 0; i < buttonList.size(); i++) {
buttonList.get(i).setTextColor(getResources().getColor(R.color.colorAccent));
}
}
private void initDate() {
gvhot_activity_view = findViewById(R.id.gvhot_activity_view);
buttonList = new ArrayList<>();
buttonList.add((RadioButton) findViewById(R.id.xiangguandu));
buttonList.add((RadioButton) findViewById(R.id.zuixin));
//集合裝碎片進來
fragmentList = new ArrayList<>();
fragmentList.add(new XiangGuanDuFragment());
fragmentList.add(new ZuiXinFragment());
}
}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/fragment3_RG"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/fragment3_Bt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/activity_press"
android:button="@null" />
<RadioButton
android:id="@+id/fragment3_Bt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/home_press"
android:button="@null" />
</RadioGroup>
<FrameLayout
android:id="@+id/jiekou01_FLoo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a1cae1e7">
</FrameLayout>
</LinearLayout>
---------------------------------------------------------------------------------------------------------
活动代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.fragment3_Bt1).setOnClickListener(this);
findViewById(R.id.fragment3_Bt2).setOnClickListener(this);
}
//添加碎片的实现方法(3个按钮点击实现方法,后续switch使用动态)
private void init(Fragment fragment, String TAG) {
//添加碎片
FragmentManager fm = getSupportFragmentManager();
//开启事务
FragmentTransaction ft = fm.beginTransaction();
//添加碎片
ft.replace(R.id.jiekou01_FLoo, fragment);
ft.addToBackStack(null);
//提交事务
ft.commit();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fragment3_Bt1:
Fragment4 fragment4 = new Fragment4();
init(fragment4, "fragment4");
break;
case R.id.fragment3_Bt2:
Fragment5 fragment5 = new Fragment5();
init(fragment5, "fragment5");
break;
}
}
==============================================================================================================================================
动态切换碎片viewpage
布局:
<RadioGroup
android:id="@+id/gvhot_activity_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/xiangguandu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="相关度" />
<RadioButton
android:id="@+id/zuixin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="最新" />
</RadioGroup>
<View
android:id="@+id/gvhot_activity_view"
android:layout_width="100dp"
android:layout_height="0.5dp"
android:layout_below="@+id/gvhot_activity_rg"
android:background="#a61f1f" />
<android.support.v4.view.ViewPager
android:id="@+id/gvhot_activity_VP"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/gvhot_activity_rg" />
---------------------------------------------------------------------------------------------------------
代码:public class MainViewActivity extends AppCompatActivity {
FragmentManager fm;
int mCurronIndex;
private List<Fragment> fragmentList; //碎片集合
private List<RadioButton> buttonList; //按鈕集合
private ViewPager viewPager_gvhot;
private View gvhot_activity_view;
private int line_width;
//适配器监听事件
private FragmentPagerAdapter mFragmentPagerAdapter;
//視圖显示操作
private ViewPager.OnPageChangeListener mOnPageChangeListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
initDate();
fm = getSupportFragmentManager();
setLineWidth();
viewPager_gvhot = (ViewPager) findViewById(R.id.gvhot_activity_VP);
mFragmentPagerAdapter = new FragmentPagerAdapter(fm) {
//返回某一个碎片集合
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
//返回某一个碎片数字大小
@Override
public int getCount() {
return fragmentList.size();
}
};
viewPager_gvhot.setAdapter(mFragmentPagerAdapter);
buttonList.get(0).setTextColor(getResources().getColor(R.color.colorPrimary));
//监听
mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
//图片在改变(拖住图片不放)
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) gvhot_activity_view.getLayoutParams();
View parent = (View) gvhot_activity_view.getParent();
int measuredWidth = parent.getMeasuredWidth();
int i = measuredWidth / 2;
lp.leftMargin = (int) (i * (position + positionOffset));
gvhot_activity_view.setLayoutParams(lp);
}
//图片被选中,选中后操作(选中后的图片缓存下来)
@Override
public void onPageSelected(int position) {
mCurronIndex = position;
setColor();
buttonList.get(position).setTextColor(getResources().getColor(R.color.colorPrimary));
}
//图片在滑动(拖拽图片松开时滑动中)
@Override
public void onPageScrollStateChanged(int state) {
}
};
viewPager_gvhot.addOnPageChangeListener(mOnPageChangeListener);
for (int i = 0; i < buttonList.size(); i++) {
buttonList.get(i).setTag(i);
final int finalI = i;
buttonList.get(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setColor();
viewPager_gvhot.setCurrentItem((Integer) v.getTag());
buttonList.get(finalI).setTextColor(getResources().getColor(R.color.colorPrimary));
}
});
}
}
private void setLineWidth() {
Display defaultDisplay = this.getWindow().getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
defaultDisplay.getMetrics(outMetrics);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) gvhot_activity_view.getLayoutParams();
int width = outMetrics.widthPixels;
line_width = width / 2;
lp.width = line_width;
gvhot_activity_view.setLayoutParams(lp);
}
private void setColor() {
for (int i = 0; i < buttonList.size(); i++) {
buttonList.get(i).setTextColor(getResources().getColor(R.color.colorAccent));
}
}
private void initDate() {
gvhot_activity_view = findViewById(R.id.gvhot_activity_view);
buttonList = new ArrayList<>();
buttonList.add((RadioButton) findViewById(R.id.xiangguandu));
buttonList.add((RadioButton) findViewById(R.id.zuixin));
//集合裝碎片進來
fragmentList = new ArrayList<>();
fragmentList.add(new XiangGuanDuFragment());
fragmentList.add(new ZuiXinFragment());
}
}