安卓開發:viewpager + fragment 實現滑動切換
時間緊迫 長話短說 以後再補上
佈局檔案、
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF"
android:orientation="vertical" >
<!-- <FrameLayout
android:id="@+id/frm_all"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>-->
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/mainpager"
>
</android.support.v4.view.ViewPager>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/home"
style="@style/main_tab_bottom"
android:layout_weight="1"
android:drawableTop="@drawable/homebottom"
android:text="" />
<RadioButton
android:id="@+id/freends"
style="@style/main_tab_bottom"
android:layout_weight="1"
android:drawableTop="@drawable/friendsbottom"
android:text="" />
<RadioButton
android:id="@+id/find"
style="@style/main_tab_bottom"
android:layout_weight="1"
android:drawableTop="@drawable/findbottom"
android:text="" />
<RadioButton
android:id="@+id/shop"
style="@style/main_tab_bottom"
android:layout_weight="1"
android:drawableTop="@drawable/shopbottom"
android:text="" />
<RadioButton
android:id="@+id/mycenter"
style="@style/main_tab_bottom"
android:layout_weight="1"
android:drawableTop="@drawable/mycenterbottom"
android:text="" />
</RadioGroup>
</LinearLayout>
</RelativeLayout>
activity 檔案
package com.example.jzd.surname;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;
import com.example.jzd.surname.activity.HomeFragment;
import com.example.jzd.surname.activity.MycenterFragment;
import com.example.jzd.surname.activity.TestFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private List<Fragment> fragmentList = new ArrayList<>();
private TestFragment fragmentM;
private TestFragment fragmentN;
private MyPagerAdapter adapter;
private RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.mainpager);
viewPager.setOffscreenPageLimit(5);
fragmentM = new TestFragment();
fragmentN = new TestFragment();
//填充資料
fragmentList.add(new HomeFragment());
fragmentList.add(fragmentN);
fragmentList.add(new TestFragment());
fragmentList.add(new TestFragment());
fragmentList.add(new MycenterFragment());
adapter = new MyPagerAdapter(getSupportFragmentManager(), fragmentList);
viewPager.setAdapter(adapter);
radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.home:
viewPager.setCurrentItem(0);
break;
case R.id.freends:
viewPager.setCurrentItem(1);
break;
case R.id.find:
viewPager.setCurrentItem(2);
break;
case R.id.shop:
viewPager.setCurrentItem(3);
break;
case R.id.mycenter:
viewPager.setCurrentItem(4);
}
}
});
radiogroup.check(R.id.home);
viewPager.addOnPageChangeListener(new MyPagerChangeListener());
}
class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mfragmentList;
TestFragment fragment111;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.mfragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return mfragmentList.get(position);
}
@Override
public int getCount() {
return mfragmentList.size();
}
}
public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
radiogroup.check(R.id.home);
break;
case 1:
radiogroup.check(R.id.freends);
break;
case 2:
radiogroup.check(R.id.find);
break;
case 3:
radiogroup.check(R.id.shop);
break;
case 4:
radiogroup.check(R.id.mycenter);
break;
}
}
}
}
相關文章
- 利用ViewPager和Fragment實現頁卡切換ViewpagerFragment
- 使用ViewPager和TabLayout來實現滑動切換效果ViewpagerTabLayout
- Android使用(TabLayout+ViewPager+fragment)與(FragmentTabHost+ViewPager+Fragment)實現底部狀態列切換AndroidTabLayoutViewpagerFragment
- ViewPager、Fragment和TabLayout實現切頁效果ViewpagerFragmentTabLayout
- TabLayout+ViewPager+Fragment實現切頁展示TabLayoutViewpagerFragment
- 手機直播原始碼,使用ViewPager2實現頁面滑動切換原始碼Viewpager
- 安卓開發:安卓底部選單欄的實現,RadioGroup 和Fragment安卓Fragment
- ToolBar、TabLayout、Fragment+ViewPager的開發實踐TabLayoutFragmentViewpager
- TabLayout + ViewPager +Fragment 實現頁面的動態重新整理TabLayoutViewpagerFragment
- 安卓-自動切換APP圖示安卓APP
- uniapp 滑動切換APP
- CSS實現頁面切換時的滑動效果CSS
- ViewPager(通過反射修改viewpager切換速度)Viewpager反射
- [MAUI]模仿iOS多工切換卡片滑動的互動實現UIiOS
- Android ViewPager2 + Fragment 聯動AndroidViewpagerFragment
- vue路由切換滑動效果Vue路由
- 安卓關閉fragment安卓Fragment
- Android ViewPager2 + Fragment + BottomNavigationView 聯動AndroidViewpagerFragmentNavigation
- 自定義Navigator切換fragmentFragment
- Android 禁止ViewPager左右滑動AndroidViewpager
- SpringBoot 這麼實現動態資料來源切換,就很絲滑!Spring Boot
- TabLayout+ViewPager+Fragment實現懶載入完全解決方案TabLayoutViewpagerFragment
- UI介面微信底部(ViewPager實現Tab,左右滑動+底部點選)UIViewpager
- RecyclerView 、ViewPager 左右滑動衝突Viewpager
- 【Android】 banner+tab吸頂+viewpager切換+重新整理載入之實現AndroidViewpager
- vue路由切換滑動效果 vue頁面跳轉互動 vue實現動畫跳轉Vue路由動畫
- 自定義 Behavior,實現巢狀滑動、平滑切換周月檢視的日曆巢狀
- WPF手動實現切換頁面
- ViewPager巢狀fragment簡單使用Viewpager巢狀Fragment
- 成品直播原始碼,點選滑動切換效果原始碼
- 帶貨直播原始碼,確定ViewPager滑塊滑動方向原始碼Viewpager
- 解決viewpager+fragment+tablayout的動態重新整理ViewpagerFragmentTabLayout
- Android——Activity切換炫酷動畫實現Android動畫
- JavaScript左右滑動切換的選項卡詳解JavaScript
- android可以無限迴圈滑動的ViewPagerAndroidViewpager
- 實現LCD屏啟動播放影片開機動畫與介面切換動畫
- 安卓之viewPager簡單用法圖片輪播安卓Viewpager
- 安卓學習筆記20:Fragment入門安卓筆記Fragment