如何从gridview设置图像onClick?

时间:2017-12-27 17:22:05

标签: java android

我能够显示在网格视图中点击的项目的ID,但是我想要实现的是当我点击Gridview中的任何图像时,它也显示为图像,当我使用下面的代码点击任何图像时,它只显示ID,但是当我尝试显示图像本身时,应用程序崩溃并提出LinearLayout无法转换为ImageView

gifts_layout_2.xml

<?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"
android:id="@+id/fragment_history_menu_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">


<ImageView
    android:id="@+id/close"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:layout_gravity="right"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:src="@drawable/close" />


<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:text="Smiles"
    android:textColor="#000000"
    android:textSize="17sp"
    android:textStyle="bold"
    android:layout_gravity="left"
    android:layout_marginLeft="15dp"/>


<GridView
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_marginTop="10dp"
    android:numColumns="4"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center|center_horizontal|center_vertical"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:scrollbars="vertical"
    android:scrollbarSize="3dp"
    android:scrollIndicators="left"
    android:id="@+id/grid_view2"
    android:paddingLeft="10dp"
    android:layout_marginBottom="10dp"
    android:layout_gravity="center|center_horizontal|center_vertical"
    android:clipChildren="false"
    android:clipToPadding="false"/>


</LinearLayout>

smiles_items_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/smile_image_view"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_gravity="center"/>


</LinearLayout>

BottomSheetDialog_Smiles.java

@Override
public View getView(final int position, View convertView, ViewGroup parent) 
{

final Holder holder = new Holder();
LayoutInflater inflater = (LayoutInflater) 
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.smile_items_layout, null);
lottieImage = inflater.inflate(R.layout.activity_streaming2, null);

holder.img = (ImageView) grid.findViewById(R.id.smile_image_view);
holder.img.setImageResource(mThumbIds[position]);

gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long 
l) {
JSONDictionary imageChat = new JSONDictionary();
int imageId = (int) getItem(i);
imageChat.put("message",imageId);
Communicator.getInstance().emit("new chat message", imageChat);
}
});

return grid;
}

1 个答案:

答案 0 :(得分:1)

尝试以下代码,单击

的OnItemClick Retrun视图
gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
   ImageView imageView = (ImageView)view;
   //if you want to get image from imageview
   imageView .getDrawable()
  }
});

如果你想从drawable使用输入流:

BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable .getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);

更新

你是通过2路

完成的

第一道: 从xml中删除LinerLayout并使ImageView Parent为LinerLayout只包含一个视图像这样,如果使用它,则无需更改java代码

smiles_items_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/smile_image_view"
        android:layout_width="45dp"
        android:layout_height="45dp"
 />

第二路

如果你想保持你的xml,那么改变下面的java代码

 LinearLayout linearLayout= (LinearLayout)view;
 ImageView imageView = (ImageView) linearLayout.getChildAt(0);
 //if you want to get image from imageview
 imageView .getDrawable()