此申请表是一份问卷。
我使用ViewPager来托管片段。
当用户到达最后一个屏幕EndingQuestionsFragment时,他们有一个按钮然后按下它并将它们带到主屏幕(对我的问题不重要)。
如果有一个没有回答的问题,当点击该按钮时,它应该将它们带到包含未回答的问题的片段。
我用过:
MainActivity parent = (MainActivity) getActivity();
parent.setPagerFragment(0);
在setOnClickListener中。
但是,单击该按钮时,将重置所给出的任何答案。 (radioButtons被重置)。
如何在不丢失输入的情况下浏览片段?
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>
答案 0 :(得分:1)
在FragmentActivity中使用`ViewPager.setOffscreenPageLimit() 原因?? 您使用的默认片段寻呼机适配器,它会在每次滑过时销毁并重新创建片段,使用此代码告诉它不要破坏先前加载的片段的初始片段状态
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setOffscreenPageLimit(10); // i assumed there are 10 fragments in total
答案 1 :(得分:0)
而不是:
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;
}
在开始时创建每个片段的实例(可能在onCreate
中)
所以:
InitializerFragment fragmet = new InitializerFragment();
然后改变你的getItem方法来改为:
public Fragment getItem(int position) {
switch (position){
case 0:
return fragment;
case 1:
// And so on...
}
对viewpager中的所有片段执行此操作,这样视图分页器将保留片段的实例并保留片段中的答案。