【问题标题】:My gridView is not showing on the mainactivity我的 gridView 没有显示在 mainactivity
【发布时间】:2020-02-17 14:19:28
【问题描述】:

我这里有一个 gridView 适配器:

package com.example.myapplication

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.View.inflate
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import androidx.cardview.widget.CardView

class ImageAdapter constructor(private val mContext: Context, private val images: Array<Int>) :  BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var convertView = convertView

        if(convertView == null) {
            val layoutInflater = LayoutInflater.from(mContext)
            convertView = layoutInflater.inflate(R.layout.card_view, null)
        }
        val image = convertView!!.findViewById<ImageView>(R.id.added_picture)
        image.setImageResource(images[position])

        return convertView
    }

    override fun getItem(position: Int): Any? {
        return null
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getCount(): Int {
        return 0
    }

}

我正在尝试在 gridView 的每个单元格上设置 cardView,这是我的 card_view.xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="110dp"
  android:layout_height="110dp"
  android:layout_margin="3dp"
  app:layout_columnSpan="1"
  app:cardCornerRadius="8dp"
  app:layout_rowSpan="1"
  app:layout_column="2"
  app:layout_row="0" >
  <ImageView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/added_picture"
    />
</androidx.cardview.widget.CardView>

我正在尝试这样调用适配器:

class MainActivity : AppCompatActivity() {
    private var initialGridPosition = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        gridView.adapter = ImageAdapter(this, arrayOf(R.drawable.filled_rectangle, R.drawable.filled_rectangle))
     }
}

这是我活动中的 gridView:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingHorizontal="30dp"
    android:paddingVertical="30dp"
    tools:context=".MainActivity"
    >
    <GridView
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:numColumns="3"
      android:id="@+id/gridView"
      />
  </LinearLayout>
</ScrollView>

当我是应用程序时,它没有向我显示 gridView,但在 mainActivity gridView.adapter 在将其分配给 ImageAdapter 对象后不为空。

【问题讨论】:

  • 不要从BaseAdapter继承,而是应该从RecyclerView.Adapter&lt;T&gt;继承

标签: android kotlin gridview baseadapter


【解决方案1】:

问题是 getcount() 方法总是返回 0,所以你有返回图像大小。

override fun getCount(): Int {
        return images.size
 }

【讨论】:

  • 现在在适配器中初始化图像后添加 glide Glide.with(mContext).load(R.drawable.filled_rectangle).into(image) 后,gridView 变为空白
猜你喜欢
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 2012-07-08
  • 2010-11-04
相关资源
最近更新 更多