gpt4 book ai didi

java - 如何从gridview设置onClick图像?

转载 作者:行者123 更新时间:2023-12-02 11:49:10 26 4
gpt4 key购买 nike

我能够显示在 GridView 中单击的项目的 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;
}

最佳答案

尝试下面的代码,OnItemClick 返回您单击的 View

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()
}
});

如果您想从可绘制对象中获取输入流,请使用以下内容:

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);

已更新

你可以通过两种方式做到这一点

第一种方法:从 xml 中删除 LinerLayout 并将 ImageView Parent 作为 LinerLayout 仅包含一个 View 像这样,如果您使用它,则无需更改 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()

关于java - 如何从gridview设置onClick图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47996195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com