此代码从图库中获取几张图像,并将其设置在Viewpager中。
通过在“ getimagefilepath”方法中访问适配器来添加图像
在ImageViewPager类中,我使用Glide设置了ImageView。
我想在ImageViewPager类中创建一个ImageButton,并在单击该按钮时删除该图像。
实现是在之前完成的,但是当我在删除后再次添加它时,则添加了前一个图像。
public class ImageViewPagerAdapter extends FragmentPagerAdapter {
public ArrayList<Fragment> fragments = new ArrayList<>();
public ImageViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
public void removeitem(int position){
fragments.remove(position);
}
@Override
public int getCount() {
return fragments.size();
}
// List에 Fragment를 담을 함수
public void addItem(Fragment fragment) {
fragments.add(fragment);
}
}
public class ImageViewPager extends Fragment {
ImageButton imageButton;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_image, container, false);
ImageView imageView = view.findViewById(R.id.imageView);
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.btn_x);
requestOptions.error(R.drawable.account);
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
requestOptions.skipMemoryCache(true);
requestOptions.override(1200,1000);
if (getArguments() != null) {
Bundle args = getArguments();
// MainActivity에서 받아온 Resource를 ImageView에 셋팅
Glide.with(getActivity()).load("file://" + Uri.parse(args.getString("imgRes"))).thumbnail(0.1f).apply(requestOptions).into(imageView);
//imageView.setImageURI(Uri.parse(args.getString("imgRes")));
//imageView.setImageResource(args.getInt("imgRes"));
}
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//I want to write code to remove it here.
}
});
return view;
}
}
public void doTakeMultiAlbumAction(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK && data != null){
Log.i("하@","1"+imagePathList+"2"+fragmentAdapter+"3"+viewPager);
if(data.getClipData() != null){
ClipData mClipData = data.getClipData();
mArrayUri = new ArrayList<Uri>();
for (int i=0; i<mClipData.getItemCount(); i++){
Uri imageUri = data.getClipData().getItemAt(i).getUri();
//getImageUri(imageUri);
getImageFilePath(imageUri);//2. 절대경로 구함 쓸데가 있겠지?
}
}
else if(data.getData() != null){
Uri imgUri = data.getData();
getImageFilePath(imgUri);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void getImageFilePath(Uri uri) {
File file = new File(uri.getPath());
String[] filePath = file.getPath().split(":");
String image_id = filePath[filePath.length - 1];
Cursor cursor = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{image_id}, null);
if (cursor!=null) {
cursor.moveToFirst();
imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
imagePathList.add(imagePath);
cursor.close();
viewPager = (ViewPager) findViewById(R.id.image_viewpager);
fragmentAdapter = new ImageViewPagerAdapter(getSupportFragmentManager());
// ViewPager와 FragmentAdapter 연결
viewPager.setAdapter(fragmentAdapter);
viewPager.setClipToPadding(false);
int dpValue = 16;
float d = getResources().getDisplayMetrics().density;
int margin = (int) (dpValue * d);
viewPager.setPadding(margin, 0, margin, 0);
viewPager.setPageMargin(margin / 2);
Log.i("image 가져옴",""+imagePathList);
// FragmentAdapter에 Fragment 추가, Image 개수만큼 추가
for (int i = 0; i < imagePathList.size(); i++) {
ImageViewPager imageFragment = new ImageViewPager();
Bundle bundle = new Bundle();
bundle.putString("imgRes", imagePathList.get(i));
imageFragment.setArguments(bundle);
fragmentAdapter.addItem(imageFragment);
}
fragmentAdapter.notifyDataSetChanged();
}
getItemposition(viewPager);
}