如何从gridview设置onClick图像?

我能够显示在gridview中点击的项目的ID,但是我想实现的是,当我点击Gridview中的任何图像时,它就会以图像的形式出现,当我点击任何使用下面的代码,它只是显示了ID,但是当我尝试显示图像本身的应用程序崩溃与LinearLayout中出现不能被强制转换为ImageView的如何从gridview设置onClick图像?

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

}

});

,如果你想从绘制使用得到的InputStream如下:

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种方式实现

1路: 从XML删除LinerLayout并ImageView的母公司为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代码ow

LinearLayout linearLayout= (LinearLayout)view; 

ImageView imageView = (ImageView) linearLayout.getChildAt(0);

//if you want to get image from imageview

imageView .getDrawable()

以上是 如何从gridview设置onClick图像? 的全部内容, 来源链接: utcz.com/qa/266568.html

回到顶部