Viewpager2和Fragments

huangapple go评论166阅读模式
英文:

Viewpager2 and Fragments

问题

ViewPager2不支持直接的子视图。

我正在尝试使用以下代码在片段之间进行过渡,但在使用ViewPager2时出现了上述错误。

在片段1中调用以过渡到片段2:

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

主布局中的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" />

在主Activity中的实例化:

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

如何避免在ViewPager2中出现这个错误?

英文:
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.

Call in fragment 1 to transition to fragment 2:

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

Viewpager2 XML in Main Layout:

&lt;androidx.viewpager2.widget.ViewPager2
            android:id=&quot;@+id/viewPager2&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_weight=&quot;10&quot;
            android:orientation=&quot;horizontal&quot;
            android:scaleType=&quot;fitXY&quot; /&gt;

Instantiation in 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?

答案1

得分: 5

我在将TabLayout放在ViewPager的闭合标签之前时遇到了相同的错误。

即:

&lt;androidx.viewpager2.widget.ViewPager2
        android:id=&quot;@+id/pager&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;com.google.android.material.tabs.TabLayout
            android:id=&quot;@+id/tab_layout&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
&lt;/androidx.viewpager2.widget.ViewPager2&gt;

这是不允许的!

只需移除结尾标签并将TabLayout分离,就可以正常工作。

&lt;androidx.viewpager2.widget.ViewPager2
        android:id=&quot;@+id/pager&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;/&gt;

        &lt;com.google.android.material.tabs.TabLayout
            android:id=&quot;@+id/tab_layout&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
英文:

I got the same error when I put the TabLayout before ViewPager 's closing tag

That is:

&lt;androidx.viewpager2.widget.ViewPager2
        android:id=&quot;@+id/pager&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;com.google.android.material.tabs.TabLayout
            android:id=&quot;@+id/tab_layout&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
&lt;/androidx.viewpager2.widget.ViewPager2&gt;

Which is not Allowed!

Just Removing the ending tag and separating TabLayout will work

&lt;androidx.viewpager2.widget.ViewPager2
        android:id=&quot;@+id/pager&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;/&gt;

        &lt;com.google.android.material.tabs.TabLayout
            android:id=&quot;@+id/tab_layout&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;

答案2

得分: 1

以下是翻译好的部分:

"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,在特定点上,那么您应该在 fragment 和 activity 之间使用 interface,以通知 activity 将 viewpager 移动到包含 fragment 2 的项目。)

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

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);
  }

}

Fragment1

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);
            } 
        }           

    }
英文:

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.

Interface Pattern for Fragment to 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);
            } 
        }           

    }

答案3

得分: 0

正如该语句所说:

ViewPager2不支持直接添加子视图
因此,不要试图将片段直接添加到viewPager2中
即以下代码行在viewPager2中不起作用。

<!-- language: lang-js -->

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

<!-- end snippet -->

如果您尝试使用fragmentManager将片段直接添加到viewPager2中,将会产生异常。
因此,只需删除上述代码行即可摆脱此异常。

英文:

As the statement says

ViewPager2 does not support direct child views
So never try to add the fragment to viewPager2 directly
i.e the following lines of code will not work with viewPager2.

<!-- language: lang-js -->

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

<!-- end snippet -->

The exception is produced if you try to add fragment directly to viewPager2 with the help of fragmentManager.
So simply remove the above lines of code to get rid of this exception.

答案4

得分: 0

让我们假设从 Fragment_1Fragment_2

Fragment_1 中的按钮点击事件内部

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

Fragment_2onCreate(Bundle savedInstanceState) 方法内部

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

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

英文:

Lets Say From Fragment_1 to Fragment_2:

In side the button click in Fragment_1

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

In side the Fragment_2 onCreate(Bundle savedInstanceState) method

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

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

答案5

得分: -1

使用 mPager.setNestedScrollingEnabled(true);

英文:

Use mPager.setNestedScrollingEnabled(true);

huangapple
  • 本文由 发表于 2020年9月13日 01:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63863320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定