2016-03-24 15 views
2

5つのカテゴリを持つ引き出しを持つアプリがあります。各カテゴリは、(選択されたカテゴリに応じて)タブ付きのビューページを開き、ユーザがスワイプできる質問を表示します。私が抱えている問題は、答えが選択されたとき - カテゴリ1、質問0 - カテゴリ2、質問0 &カテゴリ3、質問0などに同じ答えが表示されるということです。私がclearchecked()を何回呼び出しても、私はまだ回答していないカテゴリで回答が繰り返されます。私がonResume()にclearchecked()を入れると、この効果は止まりますが、フラグメントがスワイプされるたびにすべての回答が削除されます。FragmentStatePagerAdapterは古いビューの状態を破棄しません

私はnotifydatasetchanged/return position_noneを実装しました(これはカテゴリに基づいて質問を変更する部分を解決するのに役立ちました)。ビュー/フラグメントが破棄されるため、再作成時にビューステートを保持しないFragmentStagePagerAdapterではなくFragmentPagerAdapterを使用することが私の理解になりましたか?

これらの緊張した目にはどんな助力も非常に高く評価されます。

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

}

答えて

1

私はそれを実現しました。私は引き出しカテゴリがクリックされるたびにFragmentStatePagerAdapterの新しいインスタンスを作成する必要があったため、フラグメント内のビューステートを更新しました!

 if (id == R.id.leadership) { 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
     mSectionsPagerAdapter.getContext(getApplicationContext()); 
     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 = new SectionsPagerAdapter(getSupportFragmentManager()); 
     mSectionsPagerAdapter.getContext(getApplicationContext()); 
     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 = new SectionsPagerAdapter(getSupportFragmentManager()); 
     mSectionsPagerAdapter.getContext(getApplicationContext()); 
     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 = new SectionsPagerAdapter(getSupportFragmentManager()); 
     mSectionsPagerAdapter.getContext(getApplicationContext()); 
     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 = new SectionsPagerAdapter(getSupportFragmentManager()); 
     mSectionsPagerAdapter.getContext(getApplicationContext()); 
     mSectionsPagerAdapter.getNumber(4); 
     mSectionsPagerAdapter.notifyDataSetChanged(); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
     if(fm.findFragmentById(R.id.container1)!=null) { 
      fm.beginTransaction().remove(fm.findFragmentById(R.id.container1)).commit(); 
     } 
関連する問題