图像仅显示在网格视图的第一项中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像仅显示在网格视图的第一项中相关的知识,希望对你有一定的参考价值。
图像仅显示在gridview的第一项中。下面是我的GridImageActivity,我在其中从图库中拍摄图像或从相机中捕获图像。拍摄图像后,仅在gridview的第一项中设置图像。如何在gridview的不同项目中显示图像。任何帮助表示赞赏。预先感谢。
public class GridImage extends AppCompatActivity {
GridView gv_gridimg;
private Context context = this;
Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_image);
gv_gridimg = findViewById(R.id.gv_gridimg);
GridImgAdapter gridImgAdapter = new GridImgAdapter(this);
gv_gridimg.setAdapter(gridImgAdapter);
gv_gridimg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// gridImgAdapter.notifyDataSetChanged(); //to clear all the data that is assigned
String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(context, galleryPermissions)) {
pickImageFromGallery();
} else {
EasyPermissions.requestPermissions(activity, "Access for storage", 101, galleryPermissions);
}
}
});
}
private void pickImageFromGallery() {
final CharSequence[] options = {"Take photo", "Choose from gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose your profile picture");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take photo")) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);
} else if (options[item].equals("Choose from gallery")) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
ImageView iv_gridimg = (ImageView) gv_gridimg.findViewById(R.id.iv_gridimg);
if (resultCode != RESULT_CANCELED) {
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK && data != null) {
Bitmap selectedImage = (Bitmap) data.getExtras().get("data");
iv_gridimg.setImageBitmap(selectedImage);
}
break;
case 1:
if (resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = new String[]{MediaStore.Images.Media.DATA};
if (selectedImage != null) {
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturesPath = cursor.getString(columnIndex);
iv_gridimg.setImageBitmap(BitmapFactory.decodeFile(picturesPath));
cursor.close();
}
}
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
这是我的适配器类
public class GridImgAdapter extends BaseAdapter {
private Context context;
public GridImgAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return 4;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gv_gridimg;
gv_gridimg = layoutInflater.inflate(R.layout.item_grid_img,null);
ImageView iv_gridimg = (ImageView) gv_gridimg.findViewById(R.id.iv_gridimg);
return gv_gridimg;
}
}
这是我的主要xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Activity.GridImage">
<GridView
android:id="@+id/gv_gridimg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:numColumns="2"/>
</RelativeLayout>
这是我的项目xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_gridimg"
android:layout_width="200dp"
android:layout_height="200dp"
android:padding="40dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_camera_black_24dp"/>
</RelativeLayout>
答案
在item.xml中,将父布局的高度和宽度更改为wrap_content,因为您被设置为match_parent,所以它在整个屏幕中显示。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_gridimg"
android:layout_width="200dp"
android:layout_height="200dp"
android:padding="40dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_camera_black_24dp"/>
</RelativeLayout>
我希望这可以帮助您!
谢谢。
以上是关于图像仅显示在网格视图的第一项中的主要内容,如果未能解决你的问题,请参考以下文章
gridview中的第一项在AdapterView.OnItemClickListener中不会更改
如果集合视图嵌入到 ios 的表视图中,如何始终显示集合视图的第一项
RemoteViews 和 addView(水平):仅显示第一项(视图)