gpt4 book ai didi

android - 如何重新填充 ViewPager 中的每个 fragment ?

转载 作者:行者123 更新时间:2023-11-30 03:54:38 24 4
gpt4 key购买 nike

我正在创建一个应用程序,我必须在其中显示问题类别和与每个类别相关的问题。每个问题类别都有不同数量的问题。在选择类别时,我必须将问题屏幕显示为滑动屏幕,这意味着用户可以在滑动时移动到下一个问题。因此,我设计了一个包含两个 Fragment 的屏幕。第一个 fragment 是 ListFragment 来显示类别列表,另一个是 ViewPager 来显示滑动问题。我能够在 ViewPager 中为第一个类别(包含 3 个问题)填充问题 fragment ,但是当我从列表中选择另一个类别(包含 5 个问题)时,我无法替换前三个 fragment 。它只是在 ViewPager 中添加了最后两个问题。我想用新的数据集重新填充 ViewPager。我在下面添加了代码。

QuestionFragmentActivity

  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions_activity);

if (findViewById(R.id.question_section_fragment_container) != null) {
/*
* if (getIntent().getExtras() != null) { return; }
*/
QuestionSection firstFragment = new QuestionSection();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.add(R.id.question_section_fragment_container,
firstFragment).commit();
}

categoryPosition = 0;//static variable to hold the position of category item selected.
CategoryVO cvo = QuestionFragmentActivity.categoryList
.get(categoryPosition);

if (findViewById(R.id.question_fragment_container) != null) {
/*
* if (getIntent().getExtras() != null) { return; }
*/
QuestionContainer firstFragment = new QuestionContainer();
firstFragment.setCvo(cvo);
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.add(R.id.question_fragment_container,
firstFragment).commit();
}
}

QuestionContainer.java

public class QuestionContainer extends Fragment {
ViewPager mPager;
CategoryVO cvo;

public CategoryVO getCvo() {
return cvo;
}
public void setCvo(CategoryVO cvo) {
this.cvo = cvo;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.question_container, container,
false);
ArrayList<QuestionFragment> fragmentList=new ArrayList<QuestionFragment>();
for(QuestionVO qvo: cvo.getQuestionList()){
fragmentList.add(new QuestionFragment(qvo));
}
mPager = (ViewPager) view.findViewById(R.id.question_viewpager);
MyAdapter mAdapter = new MyAdapter(getActivity().getSupportFragmentManager());
mAdapter.setFragmentList(fragmentList);
mPager.setAdapter(mAdapter);
return view;
}

public static class MyAdapter extends FragmentPagerAdapter {
ArrayList<QuestionFragment> fragmentList;

public ArrayList<QuestionFragment> getFragmentList() {
return fragmentList;
}
public void setFragmentList(ArrayList<QuestionFragment> fragmentList) {
this.fragmentList = fragmentList;
}
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return fragmentList.size();
}

@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
}

问题部分

public class QuestionSection extends ListFragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment1, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button hideListSection = (Button) getActivity().findViewById(
R.id.btn_hide_section);
hideListSection.setOnClickListener(this);
ListView lv = getListView();
ArrayList<String> listItem = new ArrayList<String>();
for (CategoryVO cvo : QuestionFragmentActivity.categoryList) {
listItem.add(cvo.getCategoryName());
}
lv.setAdapter(new SectionListAdapter(getActivity(), listItem));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long arg3) {
if (pos == 1) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
QuestionFragmentActivity.categoryPosition=pos; CategoryVO cvo=
QuestionFragmentActivity.categoryList.get(pos);

QuestionContainer fragment = new QuestionContainer();
fragment.setCvo(cvo);
fragmentTransaction.replace(R.id.question_fragment_container
, fragment); fragmentTransaction.commit();
}
}
});
}
}

最佳答案

ViewPager.setAdapter(pager_adapter)

强制重绘 ViewPager。首先进行适当的更改,然后执行此操作以进行刷新。

编辑 Adapter.notifyDataSetChanged() 使现有数据无效以准备新数据。如果您现在还没有使用它,请务必调查一下。

关于android - 如何重新填充 ViewPager 中的每个 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13543804/

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