2017-12-27 3289 views
0

我能夠顯示在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; 
} 
+0

您的網格圖像是否正確?單擊網格的一個圖像時會發生什麼? – Greggz

+0

準確地說,當圖像被點擊時,它應該被髮送到服務器,因爲我使用套接字io,我能夠實現這一點,如果我把一個字符串或一個int而不是一個圖像,但我需要的是顯示圖像本身 –

+0

我建議你將新圖像放入一個片段中,並使用片段轉換讓圖像顯示在屏幕上。我可以發佈一個實際的答案,如果你感到迷失 – Greggz

回答

0

嘗試,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() 
+0

我已經嘗試了你寫的代碼從imageview中獲取圖像,但是應用程序崩潰了,並且出現了java.lang.ClassCastException錯誤:android.widget.LinearLayout無法轉換爲android.widget.ImageView,請幫助 –

+0

我已經嘗試了一切,從https://stackoverflow.com/questions/11191040/android-java-lang-classcastexception-android-widget-imageview-cannot-be-cast-t但仍然無法工作 –

+0

分享您的activity_streaming2 xml代碼 – Munir

相關問題