讲道义的茴香 · java 读取excel后 ...· 2 天前 · |
谈吐大方的筷子 · No converter for ...· 9 月前 · |
腼腆的桔子 · System.Threading.Tasks ...· 1 年前 · |
阳光的墨镜 · 【Electron-vue】构建桌面应用(2 ...· 1 年前 · |
我将ViewPager2与FragmentStateAdapter结合使用,并调用notifyItemChanged(position)。但正如预期的那样,createFragment方法不会再次调用。是故意的行为还是错误,我该怎么做?
我的密码是
class EEditionPagerAdapter(
fragmentManager: FragmentManager, lifecycle: Lifecycle, private var date: String
) : FragmentStateAdapter(fragmentManager, lifecycle) {
private var menuList = emptyList<EEditionMenu>()
override fun getItemCount(): Int = menuList.size
override fun createFragment(position: Int): Fragment {
val menu = menuList[position]
return EEditionListingFragment.newInstance(menu, date)
fun submitList(list: List<EEditionMenu>) {
menuList = list
notifyItemRangeChanged(0, menuList.size)
fun changeDate(date: String, position: Int){
this.date = date
notifyItemChanged(position)
}
发布于 2020-02-26 07:28:37
当您调用notifyItemChanged for FragmentStateAdapter时,它调用
ensureFragment(position)
,如果片段已经创建,它就会恢复它。
private void ensureFragment(int position) {
long itemId = getItemId(position);
if (!mFragments.containsKey(itemId)) {
// TODO(133419201): check if a Fragment provided here is a new Fragment
Fragment newFragment = createFragment(position);
newFragment.setInitialSavedState(mSavedStates.get(itemId));
mFragments.put(itemId, newFragment);
}
尝试重写onBindViewHolder并将日期设置为片段
override fun onBindViewHolder(
holder: FragmentViewHolder,
position: Int,
payloads: MutableList<Any>
super.onBindViewHolder(holder, position, payloads)
val fragment: EEditionListingFragment? = fragmentManager.findFragmentByTag("f$position") as EEditionListingFragment?
fragment.updateDate(date)
}
发布于 2020-07-16 23:22:03
FragmentStateAdapter使用项ID来确定是否已经创建了一个片段,并且重用了已经创建的片段。默认情况下,项ID是项的位置。您可以重写
getItemId
和
containsItem
,根据位置和日期提供您自己的唯一ID,然后当日期更改时,它将请求一个新的片段。
发布于 2020-04-14 19:40:17
在mFragment中放置到FragementStateAdapter.class中的片段由
void placeFragmentInViewHolder(@NonNull final FragmentViewHolder holder)
方法,它是包私有的,我们没有访问权限,在这个方法中,片段被添加到mFragment管理器中,标记
"f" + holder.getItemId()
与
"f" + position
相同。
scheduleViewAttach(fragment, container);
mFragmentManager.beginTransaction()
.add(fragment, "f" + holder.getItemId())
.setMaxLifecycle(fragment, STARTED)
.commitNow();
mFragmentMaxLifecycleEnforcer.updateFragmentMaxLifecycle(false);
因此,我们可以在显示碎片时重写
onBindViewHolder
方法。
@Override
public void onBindViewHolder(@NonNull FragmentViewHolder holder, int position, @NonNull List<Object> payloads) {
super.onBindViewHolder(holder, position, payloads);
if (position == 0) {
String fragmentTag = "f" + holder.getItemId();
Log.v(TAG,"fragmentTag is : " + fragmentTag);
MyFragment fragment0 = (MyFragment) this.mFragmentManger.findFragmentByTag(fragmentTag);
if (fragment0 != null) {
Log.v(TAG,"onBindViewHolder updating fragment ...");
// do what ever you want to update the MyFragment
fragment0.update(payloads);
} else {
Log.v(TAG,"onBindViewHolder called, but fragment0 is null!");
}
在我的例子中,
this.mFragmentManager
是通过构造函数兑现的。
public MyFragmentStateAdapter(@NonNull Fragment fragment) {
super(fragment); // this calls the fragment.getChildFragmentManager().
this.mFragmentManger = fragment.getChildFragmentManager();
}
createFragment()
是:
@NonNull
@Override
public Fragment createFragment(int position) {
if(position == 0){
if (this.fragment0 == null) {
this.fragment0 = MyFragment.getInstance();
return this.fragment0;
谈吐大方的筷子 · No converter for [class java.util.LinkedHashMap] with preset Content-Type ‘application/vnd.ms-excel; 9 月前 |