一直在此卡住。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;
}
}
非常感谢您的帮助。
亚当
这应该工作,@ AdamFirst –
感谢您的。我一直在使用我的SlidingTabLayout中的onPageSelected,并使用简单的if(位置== 2),当用户滑过它时可以识别最喜欢的Fragment。认为你是对的,这将是一条路。我的下一部分是实现回调。任何好的例子? –
接受答案,如果你得到它的工作..接口回调将工作良好。 – suresh