gpt4 book ai didi

android - 使用 Android 设计支持库的带有嵌套 RecyclerView fragment 的 ViewPager fragment

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:02 25 4
gpt4 key购买 nike

我想使用 Android design support library实现以下 UI:

  1. 带有用于部分导航的 NavigationView 的 Activity。
  2. 当一个部分被选中时,一个新的(父级)Fragment 将被交换到容器空间 (FrameLayout)。
  3. (父级)Fragment 始终是 CoordinatorLayout。在布局中,可以使用 ViewPager。 (*问题)
  4. 如果 ViewPager 用于(父)Fragment,它几乎总是伴随着多个带有 RecyclerView< 的子 Fragment/。 (*问题)

问题

  1. ViewPager 上滑动时,滑动不正确(例如,每次滑动不是一页)。
  2. RecyclerView 项目(根本不显示)。

Activity 布局

Activity中,有一个带有NavigationViewDrawerLayout和一个用于固定内容的FrameLayout,如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

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

fragment 交换

每次选择导航项(部分)时,新的(父级) fragment 都会交换到 FrameLayout (R.id.container) 中,代码如下:

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, new ParentFragment())
.commit();

父 fragment (ViewPager)

在父 fragment 中,CoordinatorLayoutAppBarLayoutViewPager 一起使用,如下所示:

<?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="com.test.nestedviewpager.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

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

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

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

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

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

下面的代码用随机页数初始化 ParentFragment

package com.test.nestedviewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
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.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ParentFragment extends Fragment {
private static final int MAX_PAGE_COUNT = 5;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_parent, container, false);

AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar((Toolbar) root.findViewById(R.id.toolbar));

final ActionBar actionBar = activity.getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
}

ViewPager viewPager = (ViewPager) root.findViewById(R.id.view_pager);
viewPager.setAdapter(new MyPagerAdapter(activity.getSupportFragmentManager(), generatePages()));

TabLayout tabLayout = (TabLayout) root.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager, true);

return root;
}

private static Page[] generatePages() {
Random rng = new Random();
int size = rng.nextInt(MAX_PAGE_COUNT);
if (size <=0 ) { size = 1; }
Page[] pages = new Page[size];
for (int pos = 0; pos < size; pos++) {
pages[pos] = new Page("Page " + (pos + 1));
}
return pages;
}

private static class MyPagerAdapter extends FragmentPagerAdapter {
private final List<Page> mPageList = new ArrayList<>();

MyPagerAdapter(FragmentManager fm, Page[] pages) {
super(fm);
if ((pages != null) && (pages.length > 0)) {
mPageList.addAll(Arrays.asList(pages));
}
}

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

@Override
public Fragment getItem(int position) {
return new ChildFragment();
}

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

private static class Page {
final String title;

Page(String title) {
this.title = title;
}
}
}

子 fragment (RecyclerView)

在每个子 fragment 中,都包含一个RecyclerView,如下所示:

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

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

下面的代码用随机数量的项目初始化 ChildFragment

package com.test.nestedviewpager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ChildFragment extends Fragment {
private static final int MAX_ITEM_COUNT = 15;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_child, container, false);

RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
recyclerView.setAdapter(new MyRecyclerAdapter(generateItems()));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

return root;
}

private static Item[] generateItems() {
Random rng = new Random();
final int size = rng.nextInt(MAX_ITEM_COUNT);
if (size > 0) {
Item[] items = new Item[size];
for (int pos = 0; pos < size; pos++) {
items[pos] = new Item("Item " + (pos + 1));
}
return items;
}
return null;
}

private static class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
private final List<Item> mItemList = new ArrayList<>();

MyRecyclerAdapter(Item[] items) {
if ((items != null) && (items.length > 0)) {
mItemList.addAll(Arrays.asList(items));
}
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.recycler_item, parent, false);
return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.bind(mItemList.get(position));
}

@Override
public int getItemCount() {
return mItemList.size();
}

static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView mTitleView;

MyViewHolder(View itemView) {
super(itemView);
mTitleView = (TextView) itemView.findViewById(R.id.content);
}

void bind(Item item) {
mTitleView.setText(item.title);
}
}
}

private static class Item {
final String title;

Item(String title) {
this.title = title;
}
}
}

我完全不明白写的代码有什么问题。非常感谢任何帮助。

谢谢。

最佳答案

我看不出 ViewPager 会跳过滑动的原因,但是对于子 fragment ,您应该返回 root 而不是 super 调用。这可以解释为什么你什么都看不见

也许可以尝试使用 Fragment 类中的 getChildFragmentManager() 而不是 activity.getSupportFragmentManager()

关于android - 使用 Android 设计支持库的带有嵌套 RecyclerView fragment 的 ViewPager fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39523418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com