gpt4 book ai didi

android - 在操作栏选项卡内滑动选项卡

转载 作者:可可西里 更新时间:2023-11-01 18:47:54 26 4
gpt4 key购买 nike

我正在尝试在 actionBar 选项卡内实现滑动选项卡,就像 flipboard 一样。因此,其中一个主要选项卡将具有三个选项卡。子选项卡将首先滚动,一旦所有选项卡都滚动,然后主选项卡进入焦点并开始滚动。

我附上了 flipBoard UI 的屏幕截图 FlipBoard Tabs

有人做过吗?请帮忙。

最佳答案

它似乎已经由 Android 管理:您只需要在外部 Activity 中创建一个 ViewPager,并为 ViewPager 托管的每个 fragment 创建一个 ViewPager。这是我的测试项目:

主 Activity .java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
ViewPager viewPager = (ViewPager) findViewById(R.id.pager_activity);
viewPager.setAdapter(adapter);
}

private class MyPagerAdapter extends FragmentStatePagerAdapter {

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public android.support.v4.app.Fragment getItem(int i) {
return new InnerFragment();
}

@Override
public int getCount() {
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
return "ACTIVITY TITLE " + (position+1);
}
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager_activity"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />

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

</RelativeLayout>

InnerFragment.java(托管在 Activity viewpager 中的 fragment 之一)

public class InnerFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_inner, container, false);
ViewPager pager = (ViewPager) root.findViewById(R.id.pager);
MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
return root;
}

private class MyPagerAdapter extends FragmentStatePagerAdapter {

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public android.support.v4.app.Fragment getItem(int i) {
return PageFragment.newInstance(i+1);
}

@Override
public int getCount() {
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
return "FRAGMENT " + (position+1) + " TITLE " + (position+1);
}
}
}

及其布局fragment_inner.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.test.testflipboardviewpager.InnerFragment">

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />

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

</FrameLayout>

PageFragment.java - InnerFragment viewpager 托管的 fragment 之一

public class PageFragment extends Fragment {

private int mPosition;

public static PageFragment newInstance(int position) {
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putInt("position", position);
fragment.setArguments(args);
return fragment;
}

public PageFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPosition = getArguments().getInt("position");
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) root.findViewById(R.id.text);
textView.setText("" + mPosition);
return root;
}
}

及其布局fragment_page.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.test.testflipboardviewpager.PageFragment">

<TextView
android:id="@+id/text"
android:textSize="25sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

就是这样!尝试让我知道。

编辑:忘了说第一个组件不是 ActionBar。

关于android - 在操作栏选项卡内滑动选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31787248/

26 4 0