Android--ViewPager点击按钮切换下一页

在一个ViewPage中设置两个frament, 点击下一步跳转到下一个fragment
fragment_adde1_one.xml

新建一个ViewPage控件(activity_add_e1,xml)`

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v4.view.ViewPager>
</LinearLayout>

在一个ViewPage控件配置两个碎片(AddE1Activity.java)

public class AddE1Activity extends BaseActivity {

    @BindView(R.id.viewPager)
    public ViewPager viewPager;
	//新建两个碎片
    private AddE1OneFragment oneFragment = new AddE1OneFragment();
    private AddE1TwoFragment twoFragment = new AddE1TwoFragment();

    private List<Fragment> fragments = new ArrayList<>();
    private AddE1Adapter addE1Adapter;

    private int fragmentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_e1);
        ButterKnife.bind(this);

        initTitleBar();
        initView();
    }
	//将定义好的碎片配置
    private void initView() {
        fragments.add(oneFragment);
        fragments.add(twoFragment);

        addE1Adapter = new AddE1Adapter(getSupportFragmentManager());
        addE1Adapter.setFragments(fragments);
        viewPager.setAdapter(addE1Adapter);
    }

    private void initTitleBar() {
        Tools.setStatusBarColor(this, getResources().getColor(R.color.bg_title_blue));
        titleBar.setTitleText(getString(R.string.add_device));
        titleBar.setLeftIco(R.mipmap.icon_back_nor);
        titleBar.setLeftLayoutListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
	//跳转到下一页的函数封装
    public void onNextFragment() {
        fragmentIndex = (fragmentIndex + 1) % fragments.size();
        viewPager.setCurrentItem(fragmentIndex);
    }
}

两个fragment, AddE1OneFragment和AddE1TwoFragment的定义

//fragment_adde1_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:src="@mipmap/img_bg1_nor"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请先确保左右耳机在充电盒中"
        android:textSize="12sp"
        android:layout_margin="10dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="长按充电盒两秒,进入蓝牙配对模式"
        android:textSize="12sp"
        android:layout_margin="10dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="如果充电盒快闪,请按下一步"
        android:textSize="12sp"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/addE1Btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一步"
        android:textSize="10sp"
        android:textColor="#fff"
        android:background="@drawable/bg_button_experience"
        android:layout_margin="10dp"

        />
</LinearLayout>
//fragment_adde1_two.xml;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:src="@mipmap/img_card2_dis"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开手机系统'设置'->'蓝牙'选项"
        android:textSize="12sp"
        android:layout_margin="10dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜索'eMeet Eliet E1',并点击配送"
        android:textSize="12sp"
        android:layout_margin="10dp"
        />-
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定后完成耳机于与手机的配对"
        android:textSize="12sp"
        android:layout_margin="10dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="前往进行手机蓝牙配对"
        android:textSize="12sp"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/btn_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="前往蓝牙设置"
        android:textSize="10sp"
        android:textColor="#fff"
        android:background="@drawable/bg_button_experience"
        android:layout_margin="10dp"

        />
</LinearLayout>

获取fragment_adde1_one

public class AddE1OneFragment extends BaseFragment {
	//获取按钮id
    @BindView(R.id.addE1Btn)
    public Button btnNext;

    private AddE1Activity mActivity;

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_adde1_one;
    }

    @Override
    protected void init() {
    	//获取activity的引用
        mActivity = (AddE1Activity) getActivity();
        //监听点击事件
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	//执行下一页的函数
                mActivity.onNextFragment();
            }
        });
    }

}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一段实现android tablelayout和viewpager切换页面的代码: 1. 首先在XML文件中添加TableLayout和ViewPager: ``` <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="*"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tab1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" android:text="Tab 1" /> <TextView android:id="@+id/tab2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" android:text="Tab 2" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_span="2" /> </TableRow> </TableLayout> ``` 2. 在Java文件中,创建一个适配器以配置ViewPager: ``` public class MyPagerAdapter extends FragmentPagerAdapter { private static final int NUM_PAGES = 2; public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new Tab1Fragment(); case 1: return new Tab2Fragment(); default: return null; } } @Override public int getCount() { return NUM_PAGES; } } ``` 3. 创建两个Fragment来显示不同的页面内容: ``` public class Tab1Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab1_layout, container, false); return view; } } public class Tab2Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab2_layout, container, false); return view; } } ``` 4. 在MainActivity中配置ViewPager和TabLayout: ``` public class MainActivity extends AppCompatActivity { private ViewPager mViewPager; private TabLayout mTabLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPager = findViewById(R.id.view_pager); mTabLayout = findViewById(R.id.tab_layout); mTabLayout.addTab(mTabLayout.newTab().setText("Tab 1")); mTabLayout.addTab(mTabLayout.newTab().setText("Tab 2")); final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); mViewPager.setAdapter(adapter); mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout)); mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); } } ``` 以上就是实现android tablelayout和viewpager切换页面的示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值