android 中发生长按时如何删除网格视图中的选定图像
how to remove selected images in grid view when long press happens in android
我是 android 开发人员的新手,在完成一些示例应用程序后,我开始研究网格视图概念。在这里,我试图实现以下逻辑。
1.When 长按发生在图像上,selected 图像应突出显示。 2.Need知道selected图片删除selected图片
我已经完成 select 来自图库的图片,现在我需要长按图片并使用删除按钮删除它们。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_browse"
android:text="Browse"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_delete"
android:text="Delete"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="660dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
//适配器class
class ImageAdapter extends BaseAdapter {
//Context c|ass is use to bind xm| with java
private Context mcontext;
ArrayList<Uri> ImageUriList = new ArrayList<Uri>();
//Right c|ick and generate constructor
//imp|ement methods then auto generate
//Constructor ...Rigth c|ick and generate constructor
public ImageAdapter(Context mcontext, ArrayList<Uri> image) {
this.mcontext = mcontext;
ImageUriList = image;
}
@Override
public int getCount() {
return (ImageUriList == null) ? 0 : ImageUriList.size();
}
@Override
public Object getItem(int position) {
return ImageUriList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView =new ImageView(mcontext);
imageView.setImageURI(ImageUriList.get(position));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(340,340));
return imageView;
}
}
// 主活动
public class MainActivity extends AppCompatActivity {
public static final int BROWSE_RESULT = 1;
ImageAdapter imageAdapter;
ArrayList<Uri> imagesUri = new ArrayList<Uri>();
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
Button btn_browse;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView= findViewById(R.id.grid_view);
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
// imagesUri.remove(i) ;
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
//imageAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, i + " Value ", Toast.LENGTH_SHORT).show();
return false;
}
});
btn_browse = findViewById(R.id.btn_browse);
btn_browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
f_openImageExplorer();
}
});
}
public void f_openImageExplorer() {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, BROWSE_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BROWSE_RESULT) {
if (resultCode == MainActivity.RESULT_OK) {
if (data.getClipData() != null) {
Toast.makeText(this,Integer.toString(resultCode) , Toast.LENGTH_SHORT).show();
int imageCount = data.getClipData().getItemCount();
for(int i = 0; i< imageCount;i++){
Uri imageUri = data.getClipData().getItemAt(i).getUri();
imagesUri.add(imageUri);
imageAdapter= new ImageAdapter(getBaseContext(), imagesUri);
gridView.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
}
}
else
{
Uri singleImageUri = data.getData();
imagesUri.add(singleImageUri);
ImageAdapter imageAdapter= new ImageAdapter(getBaseContext(), imagesUri);
gridView.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
}
}
}
}
}
在您的 MainActivity
中,您可以跟踪所有点击的图像并将它们的位置保存在单独的 ArrayList
- 并且在您的删除 Button
中,您可以检查点击项目的位置和从主列表中删除它们 - 完成该操作后,只需使用新列表更新您的 ImageAdapter
:
ArrayList<int> clickedImagesIdx = new ArrayList<int>();
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
...
clickedImagesIdx.add(i);
return false;
}
});
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clickedImagesIdx.size() > 0) {
for (int i = 0; i < clickedImagesIdx.size(); i++) {
imagesUri.remove(i);
}
clickedImagesIdx.clear();
// here you will need to submit the new Uri list to your adapter
// and use notifyDataSetChange();
}
}
});