【问题标题】:How to disable the transition of fragments?如何禁用片段的过渡?
【发布时间】:2020-01-20 19:26:50
【问题描述】:

我正在构建一个包含三个问题的测验应用程序,因此我有 6 个片段,但我想禁用从一个片段到另一个片段的默认转换,我该如何实现?我已经禁用了您可以在片段之间滑动,您必须单击一个按钮才能到达下一个片段,但是单击按钮后仍然存在某种滑动过渡。我搜索了这个,但从来没有一个适合我的问题的答案。这是一个片段示例:

public class FragmentQuestion1 extends Fragment {

private Button btnNavFrag1;
private EditText editText;
private ProgressBar m_bar;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable       ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_question_1, container, false);

btnNavFrag1 = view.findViewById(R.id.btn_question1);

editText = view.findViewById(R.id.edit_text_question_1);
editText.addTextChangedListener(new NumberTextWatcher(editText));

m_bar = view.findViewById(R.id.progress_bar_question_1);





btnNavFrag1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        ((GameActivity)getActivity()).setViewPager(2);

    }
});

return view;
 }

 // Method that is used so the countdown starts when the user gets to               this fragment
 @Override
 public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
    startCountdownTimer();
}
 }

  // Countdown 17 seconds

int i = 0;


private void startCountdownTimer() {

m_bar.setProgress(i);

final int totalMsecs = 17 * 1000; // 17 seconds in milli seconds
int callInterval = 100;

/** CountDownTimer */
new CountDownTimer(totalMsecs, callInterval) {

    public void onTick(long millisUntilFinished) {

        int secondsRemaining = (int) millisUntilFinished / 1000;

        float fraction = millisUntilFinished / (float) totalMsecs;

        // progress bar is based on scale of 1 to 100;
        m_bar.setProgress((int) (fraction * 100));
    }

    public void onFinish() {
    }
}.start();

}

【问题讨论】:

    标签: android android-fragments transition


    【解决方案1】:

    在您的布局 xml 中定义一个 tablayout 和一个 FrameLayout,如下所示:

            ..................your xml code...............
    
     .............................................................   
    
            <com.google.android.material.tabs.TabLayout
                android:id="@+id/simpleTabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabBackground="@color/colorPrimary"
                app:tabIndicatorColor="#0080FF"
                app:tabSelectedTextColor="#050505"
                app:tabTextColor="@color/colorAccent">
    
                <com.google.android.material.tabs.TabItem
                    android:id="@+id/abcd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Item 1" />
    
                <com.google.android.material.tabs.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="item 2" />
    
                <com.google.android.material.tabs.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="item 3" />
            </com.google.android.material.tabs.TabLayout>
    
    
    
            <FrameLayout
                android:id="@+id/simpleFrameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </LinearLayout>
    

    此布局是为三个选项卡定义的。如果您愿意,可以通过添加更多选项卡来实现更多选项卡。

    然后为每个选项卡定义片段。在这种情况下: item_one_fragment:

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context="your.activity.context">
    
        ......................................
    ............................................
    fragments xml 
    .............................
    ,.....................................
    
    </RelativeLayout>
    

    类似于 item 2 和 item 3 的片段布局。

    然后在java中定义片段:

    public class FirstItemFragment extends Fragment {
    
        public ListView CallListView;
    
        public FirstFragment() {
    // Required empty public constructor
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            final View view = inflater.inflate(R.layout.item_one_fragment, container, false);
    
    
    // Inflate the layout for this fragment
            return view;
    
        }
    
    
    
    
    }
    

    类似的第二个和第三个片段。

    然后在您的主要活动中:

    FrameLayout simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
           TabLayout tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
    
    
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.simpleFrameLayout, new FirstItemFragment());
            ft.commit();
    
    
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    // get the current selected tab's position and replace the fragment accordingly
                    Fragment fragment = null;
                    switch (tab.getPosition()) {
                        case 0:
                            fragment = new FirstItemFragment();
                            break;
                        case 1:
                            fragment = new SecondItemFragment();
                            break;
                        case 2:
                            fragment = new ThirdItemFragment();
                            break;
                    }
    
                    getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.simpleFrameLayout, fragment)
                    .addToBackStack(null)
                    //Commit the transaction.
                    .commit();
    
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
    
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            }); 
    

    现在,当您运行活动时,您将拥有三个选项卡,当您单击任何一个选项卡时,您无需滑动即可转到相应的片段

    【讨论】:

    • 但我不想在我的布局中使用标签。用户流程是:问题 1 片段 --> 按钮单击 --> 回答 1 片段,看起来像问题 1 片段,但有一个额外的 TextView 和另一个按钮
    • 您可以根据需要从上到下完全更改每个片段布局(包括高度,宽度)。唯一难以改变的是标签本身。
    • 如果你不想要标签,你可以使用相同代码的按钮。
    【解决方案2】:

    因为您使用的是 viewPager,所以您可以使用像 this 这样的库,它可以让您在进入下一个片段时向 viewPager 添加一个转换器(某种效果)。我会为您的用例推荐ZoomOutTranformer。如果过渡不是您所期望的,您总是可以从该类扩展并覆盖过渡,这样它就更符合您的喜好了。

    ViewPagerTransformers 是原生的,因此您不需要使用库。只需创建一个类,实现PageTransformer 接口并覆盖方法transformPage

    【讨论】:

    • 我不想要任何过渡。问题是片段 1 与片段 2 相同,除了还有两个其他文本视图,所以当我单击片段一中的按钮时,我只想让文本视图消失。我只有在没有过渡的情况下才能做到这一点
    • 啊哈,那样的话你可以看看TransitionManager.beginDelayedTransition()。使用它,您可以拥有具有不同“场景”的布局并在它们之间转换,在需要时隐藏和显示不同的视图。如果这似乎是您需要的更多内容,我会将其添加为新答案并且更详细。更多信息请访问developer.android.com/training/transitions#java
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多