Ich habe eine App mit einer Schublade mit fünf Kategorien. Jede Kategorie öffnet einen Tab-Viewpager (abhängig von der gewählten Kategorie) und zeigt Fragen an, über die der Benutzer streichen kann. Das Problem, das ich habe, ist, dass, wenn die Antwort zum Beispiel category1, question0- ausgewählt wird, dieselbe Antwort auf category2, question 0 & category3, question 0 und so weiter erscheint. Egal wie oft ich callchecked() anrufe, die Antworten wiederholen sich in Kategorien, die ich noch nicht beantwortet habe. Wenn ich in onResume() clearchecked() setze, stoppt das diesen Effekt, aber alle Antworten werden jedes Mal gelöscht, wenn die Fragmente kopiert werden.FragmentStatePagerAdapter zerstört alte Ansichtszustände nicht
Ich habe darauf geachtet, die notifydataasetchanged/return position_none zu implementieren (dies half mir, den Teil der Änderung der Fragen basierend auf der Kategorie zu lösen). Es war zu meinem Verständnis, dass die Ansichten/Fragmente durch die Verwendung von FragmentStatePagerAdapter und nicht von FragmentPagerAdapter zerstört würden und somit bei der Wiederherstellung nicht die Ansichtszustände beibehalten würden.
Jede Hilfe würde diese angespannten Augen sehr schätzen.
FragmentStatePagerAdapter:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
SharedPreferences prefs;
int row;
int form;
Context mContext;
public void getContext(Context context) {
prefs = PreferenceManager.getDefaultSharedPreferences((context));
mContext = context;
}
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//form = category #1-5
return QuestionFragment.newInstance(position, form, mContext);
}
@Override
public int getCount() {
row = prefs.getInt("Valid", 0);
return prefs.getInt("TotalQuestions_" + row, 3);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public void getNumber(int n) {
form = n;
}
}
DrawerActivity:
public class Drawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
Blog blog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
mViewPager = (ViewPager) findViewById(R.id.container);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Hello! Swipe between screens!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mSectionsPagerAdapter.getContext(getApplicationContext());
FragmentManager fm = getSupportFragmentManager();
blog = new Blog();
fm.beginTransaction().replace(R.id.container1, blog).commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drawer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
FragmentManager fm = getSupportFragmentManager();
int id = item.getItemId();
if (id == R.id.leadership) {
mSectionsPagerAdapter.getNumber(0);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setAdapter(mSectionsPagerAdapter);
if(fm.findFragmentById(R.id.container1)!=null) {
fm.beginTransaction().remove(fm.findFragmentById(R.id.container1)).commit();
}
} else if (id == R.id.management) {
mSectionsPagerAdapter.getNumber(1);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setAdapter(mSectionsPagerAdapter);
if(fm.findFragmentById(R.id.container1)!=null) {
fm.beginTransaction().remove(fm.findFragmentById(R.id.container1)).commit();
}
} else if (id == R.id.wellbeing) {
mSectionsPagerAdapter.getNumber(2);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setAdapter(mSectionsPagerAdapter);
if(fm.findFragmentById(R.id.container1)!=null) {
fm.beginTransaction().remove(fm.findFragmentById(R.id.container1)).commit();
}
} else if (id == R.id.person_growth) {
mSectionsPagerAdapter.getNumber(3);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setAdapter(mSectionsPagerAdapter);
if(fm.findFragmentById(R.id.container1)!=null) {
fm.beginTransaction().remove(fm.findFragmentById(R.id.container1)).commit();
}
} else if (id == R.id.strategy) {
mSectionsPagerAdapter.getNumber(4);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setAdapter(mSectionsPagerAdapter);
if(fm.findFragmentById(R.id.container1)!=null) {
fm.beginTransaction().remove(fm.findFragmentById(R.id.container1)).commit();
}
} else if (id == R.id.blogentry) {
fm.beginTransaction().replace(R.id.container1, new Blog()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
QuestionFragments:
public class QuestionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private static final String FORM = "form";
public RadioGroup selected;
private boolean check1;
private boolean check2;
private boolean check3;
private boolean check4;
public int button;
String[] convertedQuestions;
public static Context mContext;
Bundle answers = new Bundle();
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static QuestionFragment newInstance(int sectionNumber, int form, Context context) {
QuestionFragment fragment = new QuestionFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
args.putInt(FORM, form);
fragment.setArguments(args);
mContext = context;
return fragment;
}
public QuestionFragment() {
super();
}
@Override
public void setRetainInstance(boolean retain) {
super.setRetainInstance(false);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
//GETS THE QUESTION NAMES
//convertedQuestions = Array of questions
Set<String> rawQuestions = prefs.getStringSet("Questions_" + getArguments().getInt(FORM) + "", null);
Converter(rawQuestions);
//GETS THE CORRESPONDING LAYOUT
//Types = set of question types
Set<String> types = prefs.getStringSet("Types_" + getArguments().getInt(FORM) + "", null);
GetQuestionLayout getDaLayouts = new GetQuestionLayout();
View rootView = inflater.inflate(getDaLayouts.sort(types, getArguments().getInt(ARG_SECTION_NUMBER)), container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
String fullQ = convertedQuestions[getArguments().getInt(ARG_SECTION_NUMBER)];
textView.setText(fullQ.substring(4, fullQ.length()));
//IF LINEAR SCALE
if (getDaLayouts.sort(types, getArguments().getInt(ARG_SECTION_NUMBER)) == R.layout.linear_scale) {
selected = (RadioGroup) rootView.findViewById(R.id.answer);
selected.clearCheck();
}
//IF MULTIPLE CHOICE
} else if (getDaLayouts.sort(types, getArguments().getInt(ARG_SECTION_NUMBER)) == R.layout.multiple_choice) {
CheckBox a = (CheckBox) rootView.findViewById(R.id.c1);
a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
check1 = true;
} else {
check1 = false;
}
}
});
CheckBox b = (CheckBox) rootView.findViewById(R.id.c2);
b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
check2 = true;
} else {
check2 = false;
}
}
});
CheckBox c = (CheckBox) rootView.findViewById(R.id.c3);
c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
check3 = true;
} else {
check3 = false;
}
}
});
CheckBox d = (CheckBox) rootView.findViewById(R.id.c4);
d.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
check4 = true;
} else {
check4 = false;
}
}
});
return rootView;
}
public String[] Converter(Set<String> set) {
convertedQuestions = new String[set.size()];
TreeSet<String> sorted = new TreeSet<>();
for (String sort : set) {
sorted.add(sort);
}
sorted.toArray(convertedQuestions);
return convertedQuestions;
}
}