gpt4 book ai didi

android - ListFragment 从 BackStack 返回后加载空列表

转载 作者:太空狗 更新时间:2023-10-29 16:17:19 24 4
gpt4 key购买 nike

这是我关于同一问题的第 2 篇文章,我仍在等待有人指导我。我面临的问题可能非常简单,简而言之,我有这个 ListFragment 填充调用 web 服务的列表,这工作正常。当用户选择任何项目时,我替换另一个 fragment 并将这个 fragment 添加到 BackStack

问题从这里开始,它弹出 ListFragment 但它始终为空或空白。但是我已经以各种可能的方式检查了日志并发现数据在那里,由于某种原因列表保持空白。请有人帮助我...

在 MainActivity 中,我使用 DrawerItemCustomAdapter,这就是我替换 fragment 的方式

private void selectItem(int position) {

android.support.v4.app.Fragment fragment = null;

switch (position) {
case 0:
{
fragment = new EventFragment();
}
break;
case 1:
fragment = new ScheduleFragment();
break;
case 2:
fragment = new NewsFragment();
break;
case 3:
fragment = new TeamFragment();
break;
case 4:
fragment = new VenueFragment();
break;
case 5:
fragment = new NewsFragment();
break;

default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);

} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}

}

在 EventFragment 中,我使用 FragmentStatePagerAdapter 创建 ViewPager

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.event_fragment, container, false);

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

viewpager = (ViewPager)rootView.findViewById(R.id.pager);

viewpager.setAdapter(new MyAdapter(fragmentManager));

return rootView;
}

在 MyAdapter 类中

    class MyAdapter extends FragmentStatePagerAdapter {

FragmentManager fragmentManager;
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
fragmentManager = fm;
}

@Override
public Fragment getItem(int i) {
Fragment fragment = null;

if(i == 0)
{
fragment = new LiveFragment();
}
if(i == 1)
{
fragment = new UpcomingFragment();
}
if(i == 2)
{
fragment = new CompletedFragment();
}

return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
if(position == 0)
{
return "Live";
}
if(position == 1)
{
return "Up-Coming";
}
if(position == 2)
{
return "Completed";
}

return null;
}
}

三个tab都是ListFragment我们选第一个

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);

if (isVisibleToUser) {

LiveMatchList = new ArrayList<HashMap<String, Object>>();
adapter = null;
Log.d("LiveMatchList setUserVisibleHint","setUserVisibleHint");
new LoadUpcomingMatchList().execute();
}
}

我的 LoadUpcomingMatchList 类扩展了 AsyncTask,在 onPreExecute 和 doInBackground 之后我像这样设置适配器

protected void onPostExecute(String file_url) {
try
{
if (pDialog != null) {
pDialog.dismiss();
}
//pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
getActivity(),LiveMatchList,
R.layout.live_list_row, new String[] { TAG_DateTimeLabel, "eventName", TAG_roundName, TAG_Venue, "team1Flag", "team2Flag" },
new int[] { R.id.livematchDate, R.id.livematchName, R.id.liveround, R.id.livevenue, R.id.liveteam1flag, R.id.liveteam2flag });
// updating listview
setListAdapter(adapter);
Log.d("LiveMatchList onPostExecute",String.valueOf(LiveMatchList));
}
catch(Exception ex)
{
Log.d("onPostExecute", ex.toString());
}

}

我已经实现了 onListItemClick 以从 MainActivity 转到详细信息页面,就像您建议的那样。

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

@SuppressWarnings("unchecked")
HashMap<String,String> selectedItem = (HashMap<String, String>) getListView().getItemAtPosition(position);
Bundle dataBundle = new Bundle();
dataBundle.putString("team1Id", selectedItem.get(TAG_homeTeamId));
dataBundle.putString("team2Id", selectedItem.get(TAG_awayTeamId));


((MainActivity)getActivity()).doTransaction("LiveFragment", dataBundle);

Fragment fragment = new LiveMatchDetailMainFragment();
fragment.setArguments(dataBundle);
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack("LiveFragment");
fragmentTransaction.commit();
}

详细信息页面也是一个实现了 viewPager 的 fragment 。现在,当我从详细信息页面按下后退按钮时,它会按预期加载上一个 fragment ,并且我检查了日志,我可以看到调用了 setUserVisibleHint 并且按预期加载了选项卡,但选项卡中没有任何内容,应该有一个列表.请引用图表,如果有帮助,非常感谢您的帮助。我已经在这个问题上浪费了 3/4 天。

   [MainActivity]
|
V
[EventFragment]
| | |
V V V
[Tab1] [Tab2] [Tab3]

[Tab1] 是一个 ListFragment 实现 onListItemClick -> Detail Fragment。

最佳答案

您是否在 ListFragment 中执行 fragment 事务?在 FragmentActivity 中执行此操作并使用 onListItemClick() 回调方法调用 FragmentActivity 方法,该方法应运行事务。

在 EventFragment 中替换:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
viewpager.setAdapter(new MyAdapter(fragmentManager));

与:

viewpager.setAdapter(new MyAdapter(getChildFragmentManager()));

关于android - ListFragment 从 BackStack 返回后加载空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23820023/

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