其他分享
首页 > 其他分享> > Android-FragmentPagerAdapter刷新无效的解决方案,快来收藏

Android-FragmentPagerAdapter刷新无效的解决方案,快来收藏

作者:互联网

private SparseArray mFragmentPositionMap;

private SparseArray mFragmentPositionMapAfterUpdate;

public BaseFragmentPagerAdapter(FragmentManager fm, List fragments) {

super(fm);

mFragmentList = fragments;

mFragmentManager = fm;

mFragmentList = fragments;

mFragmentPositionMap = new SparseArray<>();

mFragmentPositionMapAfterUpdate = new SparseArray<>();

setFragmentPositionMap();

setFragmentPositionMapForUpdate();

}

/**

*/

private void setFragmentPositionMap() {

mFragmentPositionMap.clear();

for (int i = 0; i < mFragmentList.size(); i++) {

mFragmentPositionMap.put(Long.valueOf(getItemId(i)).intValue(), String.valueOf(i));

}

}

/**

*/

private void setFragmentPositionMapForUpdate() {

mFragmentPositionMapAfterUpdate.clear();

for (int i = 0; i < mFragmentList.size(); i++) {

mFragmentPositionMapAfterUpdate.put(Long.valueOf(getItemId(i)).intValue(), String.valueOf(i));

}

}

/**

*/

@Override

public int getItemPosition(Object object) {

int hashCode = object.hashCode();

//查找object在更新后的列表中的位置

String position = mFragmentPositionMapAfterUpdate.get(hashCode);

//更新后的列表中不存在该object的位置了

if (position == null) {

return POSITION_NONE;

} else {

//如果更新后的列表中存在该object的位置, 查找该object之前的位置并判断位置是否发生了变化

int size = mFragmentPositionMap.size();

for (int i = 0; i < size ; i++) {

int key = mFragmentPositionMap.keyAt(i);

if (key == hashCode) {

String index = mFragmentPositionMap.get(key);

if (position.equals(index)) {

//位置没变依然返回POSITION_UNCHANGED

return POSITION_UNCHANGED;

} else {

//位置变了

return POSITION_NONE;

}

}

}

}

return POSITION_UNCHANGED;

}

/**

*/

public void replaceFragment(BaseFragment oldFragment, BaseFragment newFragment) {

int position = mFragmentList.indexOf(oldFragment);

if (position == -1) {

return;

}

//从Transaction移除旧的Fragment

removeFragmentInternal(oldFragment);

//替换List中对应的Fragment

mFragmentList.set(position, newFragment);

//刷新Adapter

notifyItemChanged();

}

/**

*/

public void replaceFragment(int position, BaseFragment newFragment) {

BaseFragment oldFragment = mFragmentList.get(position);

removeFragmentInternal(oldFragment);

mFragmentList.set(position, newFragment);

notifyItemChanged();

}

/**

*/

public void removeFragment(BaseFragment fragment) {

//先从List中移除

mFragmentList.remove(fragment);

//然后从Transaction移除

removeFragmentInternal(fragment);

//最后刷新Adapter

notifyItemChanged();

}

/**

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

aseFragment fragment)}

*/

public void removeFragment(int position) {

BaseFragment fragment = mFragmentList.get(position);

//然后从List中移除

mFragmentList.remove(fragment);

//先从Transaction移除

removeFragmentInternal(fragment);

//最后刷新Adapter

notifyItemChanged();

}

/**

*/

public void addFragment(BaseFragment fragment) {

mFragmentList.add(fragment);

notifyItemChanged();

}

/**

*/

public void insertFragment(int position, BaseFragment fragment) {

mFragmentList.add(position, fragment);

notifyItemChanged();

}

private void notifyItemChanged() {

//刷新之前重新收集位置信息

setFragmentPositionMapForUpdate();

notifyDataSetChanged();

setFragmentPositionMap();

}

/**

*/

private void removeFragmentInternal(BaseFragment fragment) {

FragmentTransaction transaction = mFragmentManager.beginTransaction();

transaction.remove(fragment);

transaction.commitNow();

}

/**

*/

@Override

public long getItemId(int position) {

// 获取当前数据的hashCode,其实这里不用hashCode用自定义的可以关联当前Item对象的唯一值也可以,只要不是直接返回position

return mFragmentList.get(position).hashCode();

}

@Override

public Fragment getItem(int position) {

return mFragmentList.get(position);

}

@Override

public int getCount() {

return mFragmentList.size();

}

public List getFragments() {

return mFragmentList;

}

}

好了,现在这个类可以用来实现Fragment列表中的Fragment替换、删除、添加等操作了,并且可以实时刷新adapter, 你可以试验一下。

测试代码:

Activity代码

public class TestActivity extends FragmentActivity implements View.OnClickListener {

List mFragmentList;

ViewPager mViewPager;

public BaseFragmentPagerAdapter mAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_test);

mViewPager = findViewById(R.id.vp);

findViewById(R.id.btn_change).setOnClickListener(this);

mFragmentList = new ArrayList<>();

mFragmentList.add(getFg(“AAA”));

mFragmentList.add(getFg(“BBB”));

mFragmentList.add(getFg(“CCC”));

mFragmentList.add(getFg(“DDD”));

mAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), mFragmentList);

mViewPager.setAdapter(mAdapter);

}

private TestFragment getFg(String a){

TestFragment fragment = new TestFragment();

fragment.setTest(a);

return fragment;

}

@Override

public void onClick(View view) {

TestFragment eee = getFg(“EEE”);

//新增

mAdapter.addFragment(eee);

//插入

mAdapter.insertFragment(1, eee);

//删除

mAdapter.removeFragment(1);

//删除

mAdapter.removeFragment(mFragmentList.get(1));

//替换

mAdapter.replaceFragment(1, eee);

//替换

mAdapter.replaceFragment(mFragmentList.get(0), eee);

}

}

用到的TestFragment:

public class TestFragment extends Fragment {

private final static String TAG = TestFragment.class.getSimpleName();

private String test;

public View mContentView;

public void setTest(String test) {

this.test = test;

}

@Override

public void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Log.e(TAG, "onCreate: test = "+test);

}

@Nullable

@Override

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

mContentView = inflater.inflate(R.layout.layout_fg, null);

Log.e(TAG, "onCreateView: test = "+test);

return mContentView;

}

@Override

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

Log.e(TAG, "onViewCreated: test = "+test);

TextView testText = mContentView.findViewById(R.id.tv_test);

testText.setText(test);

}

@Override

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

Log.e(TAG, "onActivityCreated: test = "+test);

}

@Override

public void onDestroy() {

super.onDestroy();

Log.e(TAG, "onDestroy: test = "+test);

}

}

布局文件:

标签:FragmentPagerAdapter,Fragment,解决方案,void,fragment,mFragmentList,position,Android,
来源: https://blog.csdn.net/m0_64383184/article/details/121764695