简体   繁体   English

Viewpager2 和 Fragments

[英]Viewpager2 and Fragments

ViewPager2 does not support direct child views

I'm trying to transition between fragments using the following code but I get the above error when using viewpager2.我正在尝试使用以下代码在片段之间进行转换,但在使用 viewpager2 时出现上述错误。

Call in fragment 1 to transition to fragment 2:调用片段 1 以转换到片段 2:

getFragmentManager().beginTransaction().replace(R.id.viewPager2, new q2_fragment()).addToBackStack(null).commit();

Viewpager2 XML in Main Layout:主布局中的 Viewpager2 XML:

<androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="10"
            android:orientation="horizontal"
            android:scaleType="fitXY" />

Instantiation in Main: Main中的实例化:

final ViewPager2 viewPager2 = findViewById(R.id.viewPager2);
        viewPager2.setAdapter(new QuestionsActivity.ScreenSlidePagerAdapter(this));
        viewPager2.setUserInputEnabled(false);

How do I avoid this error with viewpager2?如何使用 viewpager2 避免此错误?

Would be happy to help, please elaborate on your requirement.很乐意为您提供帮助,请详细说明您的要求。 What exactly you want to do.你到底想做什么。

If you want to go to fragment 2 from fragment 1 , at a particular point then you should use interface between fragment and activity, to tell the activity to move the viewpager to the item which has fragment 2 .如果你想从fragment 1转到fragment 2 ,那么你应该使用片段和活动之间的interface ,告诉活动将viewpager移动到具有fragment 2的项目。

Interface Pattern for Fragment to Activity Fragment 到 Activity 的接口模式

Interface界面

public interface FragmentCallback{
    public void goTo(int pos);
}

Activity活动

public class MyActivity extends AppCompatActivity implements MyStringListener{

  @Override
  public void goTo(int pos){
        yourviewpagerAdapter.setCurrentItem(pos);
  }

}

public class Fragment1 {

        private FragmentCallback callBack;

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            try {
                callBack = (FragmentCallback) context;
            } catch (ClassCastException castException) {
                /** The activity does not implement the listener. */
            }
        }

       

        public void someEvent() {
            if(callBack!=null) {
                callBack.goTo(1);
            } 
        }           

    }

I got the same error when I put the TabLayout before ViewPager 's closing tag当我将TabLayout放在ViewPager的结束标记之前时,我遇到了同样的错误

That is:那是:

<androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
</androidx.viewpager2.widget.ViewPager2>

Which is not Allowed!这是不允许的!

Just Removing the ending tag and separating TabLayout will work只需删除结束标记并分离TabLayout

<androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

As the statement says正如声明所说

ViewPager2 does not support direct child views
So never try to add the fragment to viewPager2 directly ie the following lines of code will not work with viewPager2.所以永远不要尝试直接将片段添加到 viewPager2 即以下代码行不适用于 viewPager2。

getFragmentManager().beginTransaction().replace(R.id.viewPager2, new q2_fragment()).addToBackStack(null).commit();

The exception is produced if you try to add fragment directly to viewPager2 with the help of fragmentManager .如果你想直接添加片段viewPager2的帮助下将产生异常fragmentManager So simply remove the above lines of code to get rid of this exception.因此,只需删除上面的代码行即可摆脱此异常。

Lets Say From Fragment_1 to Fragment_2 :让我们说从Fragment_1Fragment_2

In side the button click in Fragment_1在按钮旁边单击Fragment_1

Bundle result = new Bundle(); result.putString("bundleKey", "result"); getParentFragmentManager().setFragmentResult("requestKey", result);

In side the Fragment_2 onCreate(Bundle savedInstanceState) methodFragment_2 onCreate(Bundle savedInstanceState)方法onCreate(Bundle savedInstanceState)

getParentFragmentManager().setFragmentResultListener("requestKey", this, new FragmentResultListener() {
 @Override public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle bundle)
 { 
supported String result = bundle.getString("bundleKey"); System.out.println("----------------------------"+result); 
}
 });

https://developer.android.com/guide/fragments/communicate#pass-between-fragments https://developer.android.com/guide/fragments/communicate#pass-between-fragments

使用 mPager.setNestedScrollingEnabled(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ViewPager2 不显示片段 - ViewPager2 doesn't show Fragments 应用架构:ViewPager2、Fragments 和 MutableLiveData - Application architecture: ViewPager2, Fragments and MutableLiveData ViewPager2 无法与片段和点击事件一起正常工作 - ViewPager2 not working properly with Fragments and click events 如何使用 viewpager2 和 fragmentstateadapter 动态添加和删除片段? - How do I dynamically add and delete fragments with viewpager2 and fragmentstateadapter? 在 TabLayout 和 ViewPager2 中执行异步任务后更新具有相同布局的多个片段 - Update multiple fragments with same layout after doing Async Task in TabLayout and ViewPager2 Android:将数据从由 ViewPager2 和 TabLayout 管理的片段发送到包含的活动 - Android: send data from fragments managed by a ViewPager2 and TabLayout to the containing activity 从第二个Fragment的recyclerView中选择项目时如何切换到第一个Fragment? 两个片段都在带有 viewPager2 的 tablayout 中 - How to switch to the first fragment when selecting an item from the recyclerView of second Fragment? Both fragments are in tablayout with viewPager2 在viewpager2中无限滚动 - Infinite scrolling in viewpager2 viewpager2 setoffscreenpagelimit(3) 不工作 - viewpager2 setoffscreenpagelimit(3) not working viewPager2 的实现 - implementation of viewPager2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM