gpt4 book ai didi

java - 如何从另一个 fragment 更改 ViewPager fragment ?不做新事

转载 作者:行者123 更新时间:2023-12-02 13:11:58 25 4
gpt4 key购买 nike

此应用程序是一份调查问卷。

我使用 ViewPager 来托管 fragment 。

当用户到达最后一个屏幕 EndingQuestionsFragment 时,他们有一个按钮,按下该按钮会将他们带到主屏幕(对我的问题来说并不重要)。

如果有一个未回答的问题,当单击该按钮时,应该将它们带到包含未回答的问题的 fragment 。

我用过:

MainActivity parent = (MainActivity) getActivity();
parent.setPagerFragment(0);

在 setOnClickListener 内。

但是,当单击该按钮时,给出的任何答案都会被重置。 (单选按钮已重置)。

如何在 fragment 中导航而不丢失输入?

FragmentCommunicator.java

package com.example.zdroa.testinggrounds;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.example.zdroa.testinggrounds.Question_Fragments.AnxiousQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.DependentQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.DepressiveQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.EndingQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.HistrionicQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.InitializerFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.NarcissistQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.ObsessiveQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.ParanoidQuestionsFragment;
import com.example.zdroa.testinggrounds.Question_Fragments.SchizoidQuestionsFragment;


public class FragmentCommunicator extends FragmentPagerAdapter{
public FragmentCommunicator(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new InitializerFragment();
case 1:
return new AnxiousQuestionsFragment();
case 2:
return new ParanoidQuestionsFragment();
case 3:
return new HistrionicQuestionsFragment();
case 4:
return new ObsessiveQuestionsFragment();
case 5:
return new NarcissistQuestionsFragment();
case 6:
return new SchizoidQuestionsFragment();
case 7:
return new DepressiveQuestionsFragment();
case 8:
return new DependentQuestionsFragment();
case 9:
return new EndingQuestionsFragment();
}
return null;
}

@Override
public int getCount() {
return 10;
}
}

MainActivity.java

package com.example.zdroa.testinggrounds;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.example.zdroa.testinggrounds.Question_Fragments.AnxiousQuestionsFragment;

public class MainActivity extends AppCompatActivity {

public static String PERSON_TYPE;
ViewPager viewPager;
public static FragmentCommunicator communicator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

viewPager = (ViewPager) findViewById(R.id.vpQuestionsActivity);
communicator = new FragmentCommunicator(getSupportFragmentManager());
viewPager.setAdapter(communicator);
viewPager.setCurrentItem(0);

int anxious_result = new AnxiousQuestionsFragment().getResult();
if (anxious_result != 0){
PERSON_TYPE = "Anxious person";
}

}

public String getPersonType() {
return PERSON_TYPE;
}

public void setPagerFragment(int a){
viewPager.setCurrentItem(a);
}
}

EndingQuestionsFragment.java

package com.example.zdroa.testinggrounds.Question_Fragments;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.zdroa.testinggrounds.MainActivity;
import com.example.zdroa.testinggrounds.R;

public class EndingQuestionsFragment extends Fragment {

public EndingQuestionsFragment() {
// Required empty public constructor
}


@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_ending_questions, container, false);

Button button1 = (Button) view.findViewById(R.id.bGotoMainAreaActivity);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity parent = (MainActivity) getActivity();
parent.setPagerFragment(0);
}
});



return view;
}

}

fragment_ending_questions.xml

<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"
android:background="#fefefe"
android:padding="16dp"
tools:context="com.example.zdroa.testinggrounds.Question_Fragments.AnxiousQuestionsFragment">


<TextView
android:id="@+id/ending_tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The End"
android:textAlignment="center"
android:textColor="#000"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/ending_tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ending_tv_title"
android:layout_marginTop="30dp"
android:text="Now that you have answered all the questions please click the Finish button.\n\nIf any questions have been left unanswered you will be taken back to the screen with the missing answeres. Your other answeres will not be affected."
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp" />

<Button
android:id="@+id/bGotoMainAreaActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="30dp"
android:text="Finish" />

</RelativeLayout>

activity_main.xml

<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

</android.support.v4.view.ViewPager>

</RelativeLayout>

最佳答案

在 FragmentActivity 中使用 `ViewPager.setOffscreenPageLimit()原因??您使用的默认 fragment 分页器适配器,每次您滑动时它都会销毁并重新创建 fragment ,使用此代码您告诉它不要破坏先前加载的 fragment 的初始 fragment 状态

ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setOffscreenPageLimit(10); // i assumed there are 10 fragments in total

关于java - 如何从另一个 fragment 更改 ViewPager fragment ?不做新事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43914490/

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