问题
I am new to android and I met this tutorial at the official android site on how to create a GridView of images. I am using kotlin as the language of choice. In the adapter class which extends BaseAdapter, I am trying to set the imageview layoutparams as indicated in the tutorial, but I get an error saying GridView.LayoutParams is unresolved reference. Here is the complete code for the class:
class ImageAdapter(val context: Context) : BaseAdapter() {
// references to our images
val items: IntArray = intArrayOf(R.drawable.cat, R.drawable.daft_punk, R.drawable.gun, R.drawable.harvestors, R.drawable.intel, R.drawable.maserati, R.drawable.porsche, R.drawable.serveroom, R.drawable.spiderman, R.drawable.watch_dog)
override fun getItem(p0: Int): Any {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getItemId(p0: Int): Long {
return 0
}
override fun getCount(): Int {
return items.size
}
// create a new ImageView for each item referenced by the Adapter
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
var imageView: ImageView
if (p1 == null) {
// if it's not recycled, initialize some attributes
imageView= ImageView(context)
imageView.layoutParams= GridView.LayoutParams(85,85)
imageView.scaleType = ImageView.ScaleType.CENTER_CROP
imageView.setPadding(8, 8, 8, 8)
} else {
imageView=p1 as ImageView
}
imageView.setImageResource(items[p0])
return imageView
}
}
The error is in the getView method at the line imageView.layoutParams=GridView.LayoutParams(85,85). The LayoutParams is in red which means error, and when I hover on it, I get the message:
Unresolved reference: LayoutParams
How do I successfully set these LayoutParams? Because without them images in the GridView are displaying in their full height and I need a small image the same way the gallery app displays them in our phones.
Subsequent question:
When I replace the imageView.layoutParams=GridView.LayoutParams(85,85) with imageView.layoutParams=AbsListView.LayoutParams(85,85), I get a javaOutOfMemory error.
The following is the full stack trace:
08-16 06:58:11.412 4543-4543/gallerie.classmite.com.imageviewer E/dalvikvm-heap: Out of memory on a 18662416-byte allocation.
08-16 06:58:11.431 4543-4543/gallerie.classmite.com.imageviewer E/AndroidRuntime: FATAL EXCEPTION: main
Process: gallerie.classmite.com.imageviewer, PID: 4543 java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2166)
at android.content.res.Resources.getDrawable(Resources.java:710)
at android.widget.ImageView.resolveUri(ImageView.java:638)
at android.widget.ImageView.setImageResource(ImageView.java:367)
at gallerie.classmite.com.imageviewer.ImageAdapter.getView(ImageAdapter.kt:32)
at android.widget.AbsListView.obtainView(AbsListView.java:2372)
at android.widget.GridView.makeAndAddView(GridView.java:1345)
at android.widget.GridView.makeRow(GridView.java:345)
at android.widget.GridView.fillDown(GridView.java:287)
at android.widget.GridView.fillFromTop(GridView.java:421)
at android.widget.GridView.layoutChildren(GridView.java:1233)
at android.widget.AbsListView.onLayout(AbsListView.java:2166)
at android.view.View.layout(View.java:15127)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15127)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15127)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
at android.view.View.layout(View.java:15127)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15127)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2315)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2021)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6221)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
The error only occurs when I run the app in my phone, but when I run the app in the emulator, it works flawlessly after I made the replacement above.
回答1:
You can try with this:
imageView.layoutParams = AbsListView.LayoutParams(85, 85)
AbsListView.LayoutParams is used in GridView when retrieving childs' LayoutParams.
Since static member can be inherited by subclass in Java. When you call GridView.LayoutParams, it is actually AbsListView.LayoutParams. However, in Kotlin, you cannot do it the same way. Outside of the subclass, you can only reference the members of the companion object of the super class using the name of super class.
Updates for Subsequent question:
The OutOfMemory error indicates that you are loading large image to ImageView. To prevent this, you have to scale down the image before displaying it, referring to this tutorial. You may also use some image loading framework, such as Glide, to display your image.
Suggested reading: Strange out of memory issue while loading an image to a Bitmap object
回答2:
Don't set the layout params as u did
the correct way is imageParams=(GridView.LayoutParams) imageView.getLayoutParams();
in above line "GridView" should be replaced with parent View of your imageView
Happy coding.. :)
来源:https://stackoverflow.com/questions/45703751/how-to-set-layout-params-on-an-imageview-inside-gridview-in-android