视图重新添加到视图寻呼机中的片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了视图重新添加到视图寻呼机中的片段相关的知识,希望对你有一定的参考价值。
我正在为android制作一个画廊应用程序,我正面临一个问题:
我在MainActivity中创建了一个ViewPager,它有2个片段,一个用于图像,一个用于视频。每个片段都包含一个recyclerView,用于显示受尊重的媒体类型的存储桶。例如,在图像片段中,将有“相机”,“下载”等等的存储桶。我将onClick附加到Recycler视图中的项目,这些也正常工作。问题是:
每当设备的方向改变时,回收器视图中的桶(基本上是卡片视图)都是重复的。例如,如果片段最初包含2个文件夹“Camera”和“Downloads”,那么片段将有4个文件夹,2个用于“Camera”,2个用于“Downloads”,每次方向更改时都会发生这种复制。
MainActivity的代码:
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSIONS_CODE = 100;
ArrayList<FoldersModel> foldersModelArrayList = new ArrayList<>();
Toolbar toolbar;
ViewPager mainViewPager;
SpringIndicator indicator;
MainPagerAdapter mainPagerAdapter;
Context context;
HashMap<String, ArrayList<ImageModel>> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
map = new HashMap<>();
root = findViewById(R.id.root);
contentHamburger = findViewById(R.id.content_hamburger);
toolbar = findViewById(R.id.mainActivityToolBar);
setSupportActionBar(toolbar);
mainViewPager = findViewById(R.id.mainViewPager);
indicator = findViewById(R.id.indicator);
if ((ContextCompat.checkSelfPermission(
getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(
MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS_CODE);
}
} else {
loadImages();
}
mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), foldersModelArrayList);
mainViewPager.setAdapter(mainPagerAdapter);
indicator.setViewPager(mainViewPager);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSIONS_CODE) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults.length > 0 && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
loadImages();
} else {
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void loadImages() {
//load images code, not really useful for this question
//basically populates the folderModelArrayList
}
public static class ImageFoldersFragment extends Fragment {
public ImageFoldersFragment(){
}
public static ImageFoldersFragment newInstance(int position, ArrayList<FoldersModel> foldersModels) {
ImageFoldersFragment fragment = new ImageFoldersFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("foldersData", foldersModels);
bundle.putInt("position", position);
fragment.setArguments(bundle);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_image, container, false);
RecyclerView recyclerView = view.findViewById(R.id.imageFoldersRecyclerView);
Bundle bundle = getArguments();
ArrayList<FoldersModel> foldersModel = bundle.getParcelableArrayList("foldersData");
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
recyclerView.setAdapter(new FoldersAdapter(getContext(), MainActivity.foldersModelArrayList));
recyclerView.addOnItemTouchListener(new ImageClickedListener(getContext(), new OnItemClickListener() {
@Override
public void onItemClick(View view, final int position) {
Intent intent = new Intent(getContext(), SingleFolderActivity.class);
intent.putParcelableArrayListExtra("data", MainActivity.foldersModelArrayList.get(position).getImageModelsList());
intent.putExtra("bucket", MainActivity.foldersModelArrayList.get(position).getFoldersName());
startActivity(intent);
}
}));
return view;
}
}
class MainPagerAdapter extends FragmentPagerAdapter {
String[] tabs = {"Images", "Videos"};
ArrayList<FoldersModel> foldersModelArrayList;
public MainPagerAdapter(FragmentManager fm, ArrayList<FoldersModel> list) {
super(fm);
foldersModelArrayList = list;
}
@Override
public Fragment getItem(int position) {
ImageFoldersFragment fragment = ImageFoldersFragment.newInstance(position, foldersModelArrayList);
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
}
为什么会这样?有没有办法解决这个问题?非常感谢提前!
答案
试试这个。
让foldersModelArrayList
非静态。
在onCreate()
中初始化它。
以上是关于视图重新添加到视图寻呼机中的片段的主要内容,如果未能解决你的问题,请参考以下文章