我正在为Android制作一个图库应用程序,我正面临一个问题:
我在MainActivity中制作了一个ViewPager,它有2个片段,一个用于图像,一个用于视频。每个片段都包含一个recyclerView,用于显示受尊重的媒体类型的存储桶。例如,在图像片段中,将会出现用于"相机","下载"等等。我将onClick' s附加到Recycler视图中的项目,这些也正常工作。问题是:
每当设备的方向发生变化时,回收站视图中的存储桶(基本上是卡片视图)都是重复的。例如,如果片段最初包含2个文件夹" Camera"和"下载",现在片段将有4个文件夹,2个用于"相机" &2;"下载",每次方向改变时都会发生这种重复。
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];
}
}
}
为什么会这样?有没有办法解决这个问题?非常感谢提前!
答案 0 :(得分:0)
试试这个。
使foldersModelArrayList
非静态。
在onCreate()
内初始化。