我已经在Android Studio中开发了一个基本的Android应用程序,其中包含一个包含两个片段/页面的ViewPager。第一页包含多个元素,其中只有一个按钮和一个ProgressBar与该问题有关。
当应用从零开始(没有待机状态)时,代码确实到达了隐藏ProgressBar的地步,但并没有走得更远-代码此时就中断了。我可以按需要多次单击“ Heute”按钮或其他按钮-ProgressBar将出现并消失,但是隐藏它之后的代码未执行,除非我一次切换到第二页并切换回主窗口页。 奇怪的是,一切正常之后。
注意:当我注释掉隐藏ProgressBar的部分时,应用程序卡在CoverPlanAdapter中的notifyDataSetChanged()处。
我感谢所有可能的解决方案! 问候
主要活动(ViewPager的初始化):
private SectionsPagerAdapter sectionsPagerAdapter;
private CoverPlanFragment coverPlanFragment;
private SettingsFragment settingsFragment;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gtp);
CoverPlanFetcher.setContext(getApplicationContext());
// Create the adapter that will return a fragment for each of the sections
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
viewPager = findViewById(R.id.pager);
viewPager.setAdapter(sectionsPagerAdapter);
ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (position == 0) {
coverPlanFragment.refreshCurrent();
}
}
};
viewPager.setOnPageChangeListener(pageChangeListener);
viewPager.post(() -> pageChangeListener.onPageSelected(0));
// Add Tabs for easier identifying
TabLayout tabLayout = findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final String[] tabTitles = {"Vertretungsplan", "Einstellungen"};
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Object fragment = super.instantiateItem(container, position);
if(position == 0) {
coverPlanFragment = (CoverPlanFragment) fragment;
} else if(position == 1) {
settingsFragment = (SettingsFragment) fragment;
}
return fragment;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
if (coverPlanFragment == null) {
coverPlanFragment = new CoverPlanFragment();
}
return coverPlanFragment;
} else if (position == 1) {
if (settingsFragment == null) {
settingsFragment = new SettingsFragment();
}
return settingsFragment;
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
第一个片段:
private ExpandedListView listVertretung;
private ProgressBar loading;
public CoverPlanFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cover_plan, container, false);
listVertretung = view.findViewById(R.id.list_vertretung);
listVertretung.setAdapter(new CoverPlanAdapter(getContext(), new ArrayList<>()));
loading = view.findViewById(R.id.loading);
view.findViewById(R.id.today).setOnClickListener(v -> today());
return view;
}
public void today() {
loading.setVisibility(View.VISIBLE);
CoverPlanFetcher.getInstance().getTodayCoverPlan(plan -> {
update(plan);
});
}
public void refreshCurrent() {
loading.setVisibility(View.VISIBLE);
CoverPlanFetcher.getInstance().getCoverPlan(null, plan -> {
update(plan);
});
}
private void update(CoverPlanFetcher.CoverPlan plan) {
// code goes until here
loading.setVisibility(View.INVISIBLE);
// ProgressBar is hidden, but following function is never executed
((CoverPlanAdapter) listVertretung.getAdapter()).update(plan.getVertretung());
}
第二个片段:
public SettingsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
return view;
}
CoverPlanAdapter(ListView的适配器):
private Context context;
private List<CoverPlanFetcher.CoverPlanRow> rows;
public CoverPlanAdapter(Context context, List<CoverPlanFetcher.CoverPlanRow> rows) {
this.context = context;
this.rows = rows;
}
@Override
public int getCount() {
return rows.size();
}
@Override
public CoverPlanFetcher.CoverPlanRow getItem(int position) {
return rows.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
CoverPlanRowViewHolder holder;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.cover_plan_row, null);
holder = new CoverPlanRowViewHolder(view);
view.setTag(holder);
} else {
holder = (CoverPlanRowViewHolder) view.getTag();
}
if (position % 2 == 0) {
view.setBackgroundColor(Color.parseColor("#e6e6ff"));
} else {
view.setBackgroundColor(Color.parseColor("#ffffff"));
}
CoverPlanFetcher.CoverPlanRow row = getItem(position);
holder.first.setText(row.getHour() + "\n" + row.getSubject() + "\n" + row.getGrade());
holder.second.setText(row.getNotice());
holder.third.setText(row.getTeacher() + "\n\n" + row.getRoom());
return view;
}
public void update(List<CoverPlanFetcher.CoverPlanRow> rows) {
this.rows.clear();
this.rows.addAll(rows);
// when hiding the ProgressBar is commented out, this doesnt even do anything -> the ListView shows no content
notifyDataSetChanged();
}
public class CoverPlanRowViewHolder {
private TextView first;
private TextView second;
private TextView third;
public CoverPlanRowViewHolder(View view) {
first = view.findViewById(R.id.row_first);
second = view.findViewById(R.id.row_second);
third = view.findViewById(R.id.row_third);
}
}