android - 将滚动 fragment 设置为滚动 Activity

标签 android android-fragments android-viewpager android-collapsingtoolbarlayout custom-scrolling

我正在使用 Android 的预定义滚动 Activity (折叠工具栏布局),我想用 fragment 替换内容布局(使用 viewpager)。 我尝试通过不同的方式修改滚动 Activity 布局,但我无法实现目标。

我附上了一张图片,这是我要实现的最终目标。

任何人都可以帮助我以这种方式编辑滚动 Activity 以实现目标吗?

Layout To Create

content_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.DetailsActivity"
tools:showIn="@layout/activity_details">

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


</android.support.v4.widget.NestedScrollView>

ma​​in_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.DetailsActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentTop="true" />

            <me.relex.circleindicator.CircleIndicator
                android:id="@+id/indicator"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentBottom="true"
                android:gravity="top"
                android:paddingBottom="25dp" />

        </RelativeLayout>


    </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_layout" />

    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:srcCompat="@android:drawable/ic_dialog_email" />

 </android.support.design.widget.CoordinatorLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mViewPager = findViewById(R.id.container);

    setupViewPager(mViewPager);
    mViewPager.setCurrentItem(0, true);

    FloatingActionButton fab = (FloatingActionButton) 
    findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", 
            Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new 
    ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new Frag1(), "Frag1");
    adapter.addFragment(new Frag2(), "Frag2);
    adapter.addFragment(new Frag3(), "Frag3");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}


}

最佳答案

您可以简单地将 ViewPager 添加到 content_layoutTabLayout。您可以简单地使用 this tutorial用于选项卡和 ViewPager。

同时在 NestedScrollView 中添加 android:fillViewport="true"

关于android - 将滚动 fragment 设置为滚动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52736695/

相关文章:

java - RxJava - 等待异步任务然后调用 Api

android - 如何解决错误 "LifecycleOwners must call register before they are STARTED"

java - 更改屏幕旋转时 ViewPager fragment 消失

android - 当我第二次打开父 fragment 时,我的 Viewpager 崩溃了

java - 无法对参数化类型 ArrayList<Foo> 执行 instanceof 检查

android - 在什么情况下适配器不会更新附加 View

android - Android 是否支持渐变中的非线性渐变?

java - RecyclerView ListAdapter不显示特定项目

android - 来自不同 fragment 的 notifyDataSetChanged()

android - Google Currents 喜欢逐页阅读风格