2015-04-16 55 views
0

一直在此卡住。Android - 将项目添加到片段更新中的列表视图

我目前有3个片段,每个包含列表 - A | B | C(收藏夹)

A和B从在线检索数据,C是离线收藏列表。

当用户最喜欢A的东西时,它会立即显示在收藏夹列表中,因为ViewPageAdapter会从屏幕上加载另外一页。所以A | B已经加载,这意味着当我去收藏夹(C)它必须重新加载。

我的问题是 - 当我在B中最喜欢的东西时,应用程序拒绝重新加载收藏夹(C),因为当我点击B时C已经被加载,唯一能看到我添加的东西是刷新应用程序。

我曾尝试:

  • 更改setOffscreenPageLimit();为0,所以它必须每次重新加载每个片段 - 即使笨重只是为了看到它的工作,它仍然拒绝。

  • NotifyDataSetChanged也没有工作,或者我不正确地理解它。

  • InstatiateItem在ViewPageAdapter,但不能得到那工作,找不到它的一个很好的例子来理解

  • 创建在喜欢代码的新listadapter只是设法得到它加载新的数据 - 我可以在它被添加到收藏列表的日志中看到,但它只是没有被该ViewPageAdapter

  • 很多谷歌搜索的

什么情况是,关于重载当用户从B转到C时,没有新的代码运行,因为C的代码全部运行,一旦我点击B,它就直接进入C的列表。

我使用谷歌的SlidingTabLayout和SlidingTabStrip的碎片,一切正常,没有任何改变。这里找到 - https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html

代码:

public void favourite() 
{ 
    //if the user wants to favourite 
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Favourite"); 
    //queries the id from the local data store in-case they have already favourited 
    query.whereEqualTo("id",id); 
    query.fromLocalDatastore(); 
    query.getFirstInBackground(new GetCallback<ParseObject>() { 
     public void done(ParseObject object, ParseException e) 
     { 
      if(object!=null) 
      { 
       Toast.makeText(getApplicationContext(),"You have already pinned this",Toast.LENGTH_SHORT).show(); 
      } 
      else 
      { 
       //otherwise create a new ParseObject and save the id,title and backdrop to 
       //the local datastore 
       final ParseObject favouriteShow = new ParseObject("FavouriteShow"); 
       favouriteShow.put("id", id); 
       favouriteShow.put("title", title); 
       favouriteShow.put("backdrop", backdropPath); 
       favouriteShow.pinInBackground(); 
       Toast.makeText(getApplicationContext(),"Pinned - "+ title,Toast.LENGTH_SHORT).show(); 
       //need to set favourite as null for it to redraw with the favourited icon 
       mFavourite.setImageDrawable(null); 
       mFavourite.setImageResource(R.drawable.favourited_already); 
      } 
     } 
    }); 
} 

ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

CharSequence Titles[]; 
int tabNumber; 

public ViewPagerAdapter(FragmentManager fm, CharSequence[] mTitles, int mTabNumber) { 
    super(fm); 
    this.Titles = mTitles; 
    this.tabNumber = mTabNumber; 
} 

@Override 
public Fragment getItem(final int position) { 
    Fragment fragment = null; 
    switch (position) { 
     case 0: 
      fragment = new FragmentA(); 
      break; 
     case 1: 
      fragment = new FragmentB(); 
      break; 
     case 2: 
      fragment = new FavouritesFragmentC(); 
      break; 
    } 
    return fragment; 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    return Titles[position]; 
} 

@Override 
public int getCount() { 
    return tabNumber; 
} 

}

非常感谢您的帮助。

亚当

回答

1

使用ViewPager.OnPageChangeListener和使用回调模式更新使用onPageSelected(位置)方法相应的 片段。

+0

这应该工作,@ AdamFirst –

+0

感谢您的。我一直在使用我的SlidingTabLayout中的onPageSelected,并使用简单的if(位置== 2),当用户滑过它时可以识别最喜欢的Fragment。认为你是对的,这将是一条路。我的下一部分是实现回调。任何好的例子? –

+0

接受答案,如果你得到它的工作..接口回调将工作良好。 – suresh

0

我个人也有类似的问题。

此问题的解决方案取决于每个选项卡中显示的数据是否与上下文相关。

如果不是,解决此问题的最佳方法实际上是更改应用程序的导航结构。因此,不要使用选项卡,而应使用导航抽屉。因此,每次用户通过抽屉进入屏幕时,都会重新创建片段。通过这样做,它将解决您的问题并改善用户体验。

如果是这样,您可能需要创建为基础片段的所有选项卡的继承,用函数调用刷新,那么你可以要求适配器刷新所有标签页。这种方法很不方便,主要是因为收藏夹部分不属于选项卡。

+0

我明白你在说什么,我已经考虑过了,通过操作栏中的按钮或导航画将用户加入收藏夹,但应用程序的流程并不会很好,因为内容全是有关。如果一切都失败了,我会沿着这些路线实施一些东西,但这不是我的首选方法。 –

相关问题