java中如何编写视图,java-如何使用Swipe视图实现android TabLayout设计...

聚会有点晚了,但是要做到这一点,您必须使用ViewPager类并为每个视图(在选项卡下)使用片段.然后将ViewPager附加到您的TabLayout实例和宾果游戏!您有滑动tabLayout.

这是我的一些使用两个标签的工作代码:

MyActivity.java:

super.onCreate(savedInstanceState);

setContentView(R.layout.main_layout);

// Initializing tab and pager views

TabLayout tabLayout = (TabLayout) findViewById(R.id.my_tab_layout);

final ViewPager viewPager = (ViewPager) findViewById(R.id.my_view_pager);

// Making new tabs and adding to tabLayout

tabLayout.addTab(tabLayout.newTab().setText("First Tab"));

tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));

// Adding fragments to a list

List fragments = new Vector();

fragments.add(Fragment.instantiate(this, MyFirstTabFragment.class.getName()));

fragments.add(Fragment.instantiate(this, MySecondTabFragment.class.getName()));

// Attaching fragments into tabLayout with ViewPager

viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager(), fragments));

tabLayout.setupWithViewPager(viewPager);

SectionPagerAdapter.java

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

public class SectionPagerAdapter extends FragmentPagerAdapter {

private List fragments;

public SectionPagerAdapter(FragmentManager fm, List fragments) {

super(fm);

this.fragments = fragments;

}

@Override

public Fragment getItem(int position) {

return this.fragments.get(position);

}

@Override

public int getCount() {

return this.fragments.size();

}

@Override

public CharSequence getPageTitle(int position) {

switch (position) {

case 0:

return "First Tab";

case 1:

default:

return "Second Tab";

}

}

}

MyFirstTabFragment.java:

public class MyFirstTabFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Declare your first fragment here

return inflater.inflate(R.layout.my_first_fragment_layout, container, false);

}

}

MySecondTabFragment.java:

public class MySecondTabFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Declare your second fragment here

return inflater.inflate(R.layout.my_second_fragment_layout, container, false);

}

}

main_layout.xml

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/my_tab_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

android:id="@+id/my_view_pager"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@id/my_tab_layout"/>

my_first_fragment_layout.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent">

my_second_fragment_layout.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent">

注意:在这里,我们使用支持库v4中的ViewPager,Fragment,FragmentManager和FragmentPagerAdapter.

希望能帮助到你.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值