我有一个主要活动,其中包含一个带有ViewPager的TabLayout
我添加了3个标签,每个标签都有单独的片段,其中包含recyclerview,这些recyclerviews有一个复选框,每当我刷一下viewpager时我都应该同时更新/刷新(我在共享偏好和更新中保存已检查的位置)通过共享偏好)。
我的问题是每当我检查tab1中的复选框时,tab2都没有更新/刷新,直到我向下滚动Recyclerview。和tab3工作正常。 并且我也在logcat中收到一个奇怪的警告。
03-05 09:35:53.345 4317-4327/com.example.rubin W/art: Suspending all threads took: 6.805ms
03-05 09:35:58.310 4317-4317/com.example.rubin W/FragmentManager: moveToState: Fragment state for Tab3{10a5f1f0 #2 id=0x7f0d00b6} not updated inline; expected state 3 found 2
03-05 09:36:01.363 4317-4317/com.example.rubin W/FragmentManager: moveToState: Fragment state for Tab1{2d9aa887 #1 id=0x7f0d00b6} not updated inline; expected state 3 found 2
我的PagerAdapter
public class PagerAdapter1 extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter1(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tab1();
case 1:
return new Tab2();
case 2:
return new Tab3();
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Tablayout OnPageChangelistener
final PagerAdapter1 adapter = new PagerAdapter1
(getSupportFragmentManager(), tabLayout1.getTabCount(), getApplicationContext());
viewPager1.setAdapter(adapter);
viewPager1.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout1));
tabLayout1.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager1.setCurrentItem(tab.getPosition());
adapter.notifyDataSetChanged();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
每个片段的代码。
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_tab1_recycler, container, false);
RecyclerView rv = (RecyclerView) v.findViewById(R.id.home_recyclerview);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rv.setLayoutManager(llm);
rv.setHasFixedSize(true); // to improve performance
rv.setAdapter(new HomeManager()); // the projectdatabase manager is assigner to the RV
return v;
}
public class HomeManager extends RecyclerView.Adapter<HomeManager.RecyclerViewHolder> {
int Length,h;
View v1;
ArrayList<String> PROJECT_ID = new ArrayList<String>();
Set<String> set;
List<String> selected;
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
CheckBox mCheck;
RecyclerViewHolder(final View itemView) {
super(itemView);
mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
set = pref.getStringSet("FAV", null);
if (set != null) {
selected = new ArrayList<String>(set);
} else {
selected = new ArrayList<String>();
}
mCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
h = getAdapterPosition();
check = PROJECT_ID.get(h); // for saving the project id from json.
if (selected.contains(check)) {
selected.remove(check);
mCheck.setBackgroundResource(R.drawable.ic_favorite_white1_24dp);
Snackbar snackbar = Snackbar.make(v, "Property Unfavorited", Snackbar.LENGTH_SHORT);
snackbar.show();
} else {
selected.add(check);
mCheck.setBackgroundResource(R.drawable.ic_favorite_white_24dp);
Snackbar snackbar = Snackbar.make(v, "Property Favorited", Snackbar.LENGTH_SHORT);
snackbar.show();
}
Log.e("HF update checked", String.valueOf(selected));
Set<String> set = new HashSet<String>();
set.addAll(selected);
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet("FAV", set);
editor.commit();
}
}
});
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
return new RecyclerViewHolder(v1);
}
@Override
public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
set = pref.getStringSet("FAV", null);
if (set != null) {
selected = new ArrayList<String>(set);
} else {
selected = new ArrayList<String>();
}
Log.e("HF update UI", String.valueOf(selected));
if (String.valueOf(selected).contains(String.valueOf(PROJECT_ID.get(i)))) {
viewHolder.mCheck.setBackgroundResource(R.drawable.ic_favorite_white_24dp);
} else {
viewHolder.mCheck.setBackgroundResource(R.drawable.ic_favorite_white1_24dp);
}
}
@Override
public int getItemCount() {
//Code for Total length of json
return Length;
}
答案 0 :(得分:4)
FragmentPagerAdapter
在活动片段的邻域上调用setUserVisibleHint(true|false)
,它会更改此片段的状态。
这至少是“奇怪的警告信息”的答案,但它可能无法解决您的问题。
关于如何解决该警告消息的评论,我创建了自己的FragmentPagerAdapter,如下所示:
public abstract class AbstractTabPagerAdapter extends PagerAdapter {
private static final String TAG = AbstractTabPagerAdapter.class.getCanonicalName();
private final FragmentManager mFragmentManager;
private FragmentTransaction mCurTransaction;
private Fragment mCurrentPrimaryItem = null;
public AbstractTabPagerAdapter(FragmentManager fragmentManager) {
mFragmentManager = fragmentManager;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (mCurTransaction == null) {
throw new IllegalArgumentException("current transaction must not be null");
}
String fragmentTag = makeFragmentName(container.getId(), position);
Fragment fragment = (Fragment) mFragmentManager.findFragmentByTag(fragmentTag);
if (fragment != null) {
mCurTransaction.attach(fragment);
Log.d(TAG, "Attaching existing fragment " + fragment + " at position " + position);
//mCurTransaction.add(container.getId(), fragment, makeFragmentName(container.getId(), position));
} else {
fragment = getItem(position);
mCurTransaction.add(container.getId(), fragment, fragmentTag);
Log.d(TAG, "Attaching new fragment " + fragment + " at position " + position);
}
if (fragment != mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
//fragment.setUserVisibleHint(false);
}
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
if (mCurTransaction == null) {
throw new IllegalArgumentException("current transaction must not be null");
}
mCurTransaction.detach((Fragment) object);
//mCurTransaction.remove((Fragment)object);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
//super.setPrimaryItem(container, position, object);
Fragment fragment = (Fragment) object;
if (fragment != mCurrentPrimaryItem) {
Log.d(TAG, "set Primary item " + position + " to " + fragment);
if (mCurrentPrimaryItem != null) {
mCurrentPrimaryItem.setMenuVisibility(false);
// this command unexpectedly changes the state of the fragment which leads to a warning message and possible some strange behaviour
//mCurrentPrimaryItem.setUserVisibleHint(false);
}
if (fragment != null) {
fragment.setMenuVisibility(true);
//fragment.setUserVisibleHint(true);
}
mCurrentPrimaryItem = fragment;
}
}
@Override
public boolean isViewFromObject(View view, Object fragment) {
return ((Fragment) fragment).getView() == view;
}
public abstract Fragment getItem(int position);
@Override
public void startUpdate(ViewGroup container) {
super.startUpdate(container);
if (mCurTransaction != null) {
throw new IllegalArgumentException("current transaction must not be null");
}
mCurTransaction = mFragmentManager.beginTransaction();
Log.d(TAG, "FragmentTransaction started");
}
@Override
public void finishUpdate(ViewGroup container) {
if (mCurTransaction != null) {
mCurTransaction.commit();
mCurTransaction = null;
//mFragmentManager.executePendingTransactions();
Log.d(TAG, "FragmentTransaction committed");
} else {
throw new IllegalArgumentException("current transaction must not be null");
}
}
private String makeFragmentName(int viewId, int position) {
if (viewId <= 0)
throw new IllegalArgumentException("viewId " + viewId);
return "tabpageradptr:" + getPageTitle(position) + ":" + viewId + ":" + position;
}
}
警告信息现在已经消失,目前我没有遇到任何缺陷 - 但我仍处于研究阶段。
答案 1 :(得分:2)
这不重要。这只是一个警告。
请尝试将ViewPager
布局的背景更改为null(不设置背景)。
答案 2 :(得分:2)
我花了很多时间来解决这个问题。问题是你试图在OnPageChangeListener监听器中更新viewpager,它会抛出警告
不在线更新
解决方案:
所有数据更改和更新视图都应该调出OnPageChangeListener, 例如:片段的onCreateView
我的警告消失了!并且viewpager完全更新
答案 3 :(得分:2)
它迟到了,但我认为最好的方法是使用FragmentPagerAdapter,虽然它没有消除警告,但它解决了目的,如果有人能告诉我这个警告实际意味着什么,并且实际上是什么引起了警告,它将是我最感兴趣的。
首先使用viewpager create方法中使用的所有片段,如下所示。
public void updateView() { //Update whatever views or data you want to update }
还覆盖FragmentPagerAdapter中使用的方法setUserVisibleHint,以通知片段是否对用户可见。
@Override public void setUserVisibleHint(boolean isVisibleToUser) { this.isVisibleToUser = isVisibleToUser; super.setUserVisibleHint(isVisibleToUser); }
最后在你的片段中添加以下代码,以便在片段可见时更新视图/日期。
@Override public void onStart() { if (isVisibleToUser) updateView(); }
然后在TabFragment或Activity
中实现此功能viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: { ((Fragment) mFragmentList.get(0)).updateView(); break; } case 1: { ((Fragment) mFragmentList.get(1)).updateView(); break; } } }
这将负责更新数据,无论是刷卡还是单击选项卡,如果有人发现任何问题或错误,请发表评论并让我知道或随时编辑解决方案
我投票赞成上面的解决方案,这让我采用了解决这个问题的方法。谢谢@mikes
答案 4 :(得分:2)
在视图中,相邻标签会同时加载。因此,选项卡1中的操作将不会反映在选项卡2中,因为它已经加载。您应该使用广播接收器来解决此问题。在选项卡1中广播事件并在选项卡2中接收事件
答案 5 :(得分:1)
"W/FragmentManager: moveToState: … expected state 3 found 2"
警告可以忽略,并在支持库v24.0.0中删除。
来自官方开发者answer的引用:
你不需要做任何额外的事情;这只是一条信息日志消息。 [...]
此处描述的日志本身不会影响行为。请打开 如果你有其他问题,一个新的bug;混淆不同的问题 在同一个错误中,这些单独的问题更难以跟踪。
关闭评论,因为日志问题已在未来解决 释放。