Recyclerview没有显示cardview

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Recyclerview没有显示cardview相关的知识,希望对你有一定的参考价值。

你好,我是kotlin和app开发的初学者。我的recyclerview由于某种原因没有显示我的cardviews。我在另一个屏幕上创建卡片视图,当我添加它们时,由于某种原因它们不会出现在recyclerview中。我想知道为什么。

我激活了。

class MainActivity : AppCompatActivity(), GameAdapter.GameClickListener {

private var mGames: MutableList<Game>? = null
private var mAdapter: GameAdapter? = null
private var mRecyclerView: RecyclerView? = null
private var mModifyPosition: Int = 0
var db: AppDatabase? = null
val TASK_GET_ALL_GAMES = 0
val TASK_DELETE_GAME = 1
val TASK_UPDATE_GAME = 2
val TASK_INSERT_GAME = 3
val EDIT_REQUEST_CODE = 1337

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val toolbar = findViewById<Toolbar>(R.id.toolbar)
    setSupportActionBar(toolbar)
    //Initialize the local variables
    mGames = ArrayList()
    mRecyclerView = findViewById(R.id.recyclerView)
    mRecyclerView!!.layoutManager = LinearLayoutManager(this , LinearLayoutManager.VERTICAL, true)

    mAdapter = GameAdapter(mGames!!, this, resources)
    db = AppDatabase.getInstance(this)
    GameAsyncTask(TASK_GET_ALL_GAMES).execute()
    mRecyclerView!!.adapter = mAdapter

    updateUI()
    val fab = findViewById<FloatingActionButton>(R.id.fab)
    fab.setOnClickListener { view ->
        startActivityForResult(Intent(this@MainActivity, AddGameActivity::class.java), 1011)
    }

    val simpleItemTouchCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
        override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
            return false
        }

        //Called when a user swipes left or right on a ViewHolder
        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, swipeDir: Int) {
            //Get the index corresponding to the selected position
            val position = viewHolder.adapterPosition
            val game = (mGames as ArrayList<Game>)[position]
            GameAsyncTask(TASK_DELETE_GAME).execute(game)
        }
    }
    val itemTouchHelper = ItemTouchHelper(simpleItemTouchCallback)
    itemTouchHelper.attachToRecyclerView(mRecyclerView)

}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.menu_main, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in androidManifest.xml.
    val id = item.itemId


    return if (id == R.id.action_settings) {
        true
    } else super.onOptionsItemSelected(item)

}

private fun updateUI() {
    if (mAdapter == null) {
        mAdapter = mGames?.let { GameAdapter(it, this, resources) }
        mRecyclerView!!.adapter = mAdapter
    } else {
        mAdapter!!.swapList(mGames!!)
        mAdapter!!.notifyDataSetChanged()
    }
}

inner class GameAsyncTask(private val taskCode: Int) : AsyncTask<Game, Void, MutableList<Game>>() {
    override fun doInBackground(vararg games: Game): MutableList<Game> {
        when (taskCode) {

            TASK_DELETE_GAME ->
                db!!.gameDao().deleteGames(games[0])
            TASK_UPDATE_GAME ->
                db!!.gameDao().updateGames(games[0])
            TASK_INSERT_GAME ->
                db!!.gameDao().insertGames(games[0])
        }
        //To return a new list with the updated data, we get all the data from the database again.
        return db!!.gameDao().allGames

    }

    override fun onPostExecute(list: MutableList<Game>) {
        super.onPostExecute(list)
        GameDbUpdated(list)
    }
}

fun GameDbUpdated(list: MutableList<Game>) {
    mGames = list

    updateUI()

}

override fun gameOnClick(i: Int) {
    val intent = Intent(this@MainActivity, AddGameActivity::class.java)
    mModifyPosition = i
    intent.putExtra(EXTRA_GAME, mGames!![i])
    startActivityForResult(intent, REQUESTCODE)
}

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    if (requestCode == REQUESTCODE) {
        if (resultCode == Activity.RESULT_OK) {
            val addGame = data.getParcelableExtra<Game>(MainActivity.EXTRA_GAME)
            GameAsyncTask(TASK_INSERT_GAME).execute(addGame)
        }
    }else if (requestCode == EDIT_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            val editGame = data.getParcelableExtra<Game>(MainActivity.EXTRA_GAME)
            GameAsyncTask(TASK_UPDATE_GAME).execute(editGame)
        }
    }
}

companion object {
    //Constants used when calling the update activity
    val EXTRA_GAME = "GAME"
    val REQUESTCODE = 1234
}


}

Game.kt

@Entity(tableName = "game")
class Game( @ColumnInfo(name = "title") private var mTitleText: String?,    @ColumnInfo(name = "platform") private var mPlatform: String?, @ColumnInfo(name =  "note") private var mNote: String?, @ColumnInfo(name = "status")private var mStatus: Int?, @ColumnInfo(name = "dateModified") private var mDateModified: String?) : Parcelable {

@PrimaryKey(autoGenerate = true) private var gameId: Long? = null
@Ignore
private val statusSpin = arrayOf("Want to play", "Playing", "Stalled", "Dropped")

constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readValue(Int::class.java.classLoader) as? Int,
        parcel.readString()) {
    gameId = parcel.readValue(Long::class.java.classLoader) as? Long
}

fun getPlatform(): String? {
    return mPlatform
}

fun setPlatform(mPlatform: String) {
    this.mPlatform = mPlatform
}

fun getNote(): String? {
    return mNote
}

fun setNote(mNote: String) {
    this.mNote = mNote
}


fun getTitleText(): String? {
    return mTitleText
}

fun setDateModified(mDateModified: String) {
    this.mDateModified = mDateModified
}

fun getDateModified(): String? {
    return mDateModified
}

fun setTitleText(mTitleText: String) {
    this.mTitleText = mTitleText
}

fun getGameId(): Long? {
    return gameId
}

fun setGameId(gameId: Long?) {
    this.gameId = gameId
}

fun getStatus(): Int? {
    return mStatus
}

fun getStatusSpin(): Array<String> {
    return statusSpin
}

fun setgameId(mStatus: Int) {
    this.mStatus = mStatus
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeString(mTitleText)
    parcel.writeString(mPlatform)
    parcel.writeString(mNote)
    parcel.writeValue(mStatus)
    parcel.writeString(mDateModified)
    parcel.writeValue(gameId)
}

override fun describeContents(): Int {
    return 0
}

companion object CREATOR : Parcelable.Creator<Game> {
    override fun createFromParcel(parcel: Parcel): Game {
        return Game(parcel)
    }

    override fun newArray(size: Int): Array<Game?> {
        return arrayOfNulls(size)
    }
}



}

GameAdapter.kt(带内部视图)

class GameAdapter(private var mGames: List<Game>, private val mGameClickListener: GameClickListener, val mResources: Resources) : RecyclerView.Adapter<GameAdapter.ViewHolder>() {
interface GameClickListener {
    fun gameOnClick(i: Int)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameAdapter.ViewHolder {
    val context = parent.context
    val inflater = LayoutInflater.from(context)
    val view = inflater.inflate(R.layout.game_card, parent, false)
    // Return a new holder instance
    return ViewHolder(view)
}

override fun onBindViewHolder(holder: GameAdapter.ViewHolder, position: Int) {
    val game = mGames.get(position)
    holder.textView.text = game.getTitleText()
    holder.platformTextView.text = game.getPlatform()
    holder.dateTextView.text = game.getDateModified()
    holder.statusTextView.text = mResources.getStringArray(R.array.status_array)[game.getStatus()!!];
}

override fun getItemCount(): Int {
    return mGames.size
}

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
    super.onAttachedToRecyclerView(recyclerView)
}

fun swapList(newList: List<Game>) {
    mGames = newList

    if (newList != null) {
        // Force the RecyclerView to refresh
        this.notifyDataSetChanged()

    }

}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
    var textView: TextView = itemView.findViewById(R.id.titleTextView)
    var platformTextView: TextView = itemView.findViewById(R.id.platformTextView)
    var statusTextView: TextView = itemView.findViewById(R.id.statusTextView)
    var dateTextView: TextView = itemView.findViewById(R.id.dateTextView)

    init {
        itemView.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        val clickedPosition = adapterPosition
        mGameClickListener.gameOnClick(clickedPosition)
    }

}


}

AddGameActivity.kt(我添加新的cardview的屏幕)

class AddGameActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.add_game_card)
    val titleInput = findViewById<EditText>(R.id.titleInput)
    val platformInput = findViewById<EditText>(R.id.platformInput)
    val notesInput = findViewById<EditText>(R.id.notesInput)
    val statusSpinner = findViewById<Spinner>(R.id.statusSpinner)
    var gameUpdate: Game? = intent.getParcelableExtra(MainActivity.EXTRA_GAME)
    if (gameUpdate != null) {
        titleInput.setText(gameUpdate.getTitleText())
        platformInput.setText(gameUpdate.getPlatform())
        notesInput.setText(gameUpdate.getNote())
        statusSpinner.setSelection(gameUpdate.getStatus()!!)
    } else {
        gameUpdate = Game("", "", "", -1, "")
    }
    val id = gameUpdate.getGameId()
    val fab = findViewById<FloatingActionButton>(R.id.saveFab)

    fab.setOnClickListener {
        val titleText = titleInput.text.toString()
        val platformText = platformInput.text.toString()
        val notesText = notesInput.text.toString()
        val status: Int = statusSpinner.selectedItemPosition

        if (titleText.isEmpty())
            Toast.makeText(this@AddGameActivity,
                    "title is empty", Toast.LENGTH_SHORT).show()
        else if (platformText.isEmpty())
            Toast.makeText(this@AddGameActivity,
                    "platform is empty", Toast.LENGTH_SHORT).show()
        else {
            val resultIntent = Intent()
            val dateFormat = DateFormat.getDateInstance()
            val currentDate = Date(System.currentTimeMillis())
            val game = Game(titleText, platformText, notesText, status,  dateFormat.format(currentDate))
            game.setGameId(id)
            resultIntent.putExtra(MainActivity.EXTRA_GAME, game)
            setResult(Activity.RESULT_OK, resultIntent)
            finish()
            Toast.makeText(this@AddGameActivity,
                    "added succesfully", Toast.LENGTH_SHORT).show()
        }
    }
    }
    }

content_main.xml(我有我的recyclerview)

<android.support.constraint.ConstraintLayout 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="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">


   <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="0dp"
    android:layout_height="429dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="22dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="60dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
   </android.support.constraint.ConstraintLayout>

game_card.xml(我有我的游戏卡)

   <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp">

  <android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Fallout 4"
        android:textSize="24sp"
        android:textStyle="italic"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/platformTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="PS4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titleTextView" />

    <TextView
        android:id="@+id/statusTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="Playing"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titleTextView" />

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="12/10/2018"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titleTextView" />
 </android.support.constraint.ConstraintLayout>
 </android.support.v7.widget.CardView>

add_game_card.xml(我添加我的游戏卡)

<android.support.constraint.ConstraintLayout     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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".AddGameActivity">


    <android.support.v7.widget.CardView
    android:id="@+id/cardView"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginStart="10dp"
     android:layout_marginTop="100dp"
     android:layout_marginEnd="10dp"
     android:layout_marginBottom="244dp"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toTopOf="parent">

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="302dp"
        android:orientation="vertical">

        <EditText
            android:id="@+id/titleInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:ems="10"
            android:hint="Title"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/platformInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:ems="10"
            android:hint="Platform"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/notesInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:ems="10"
            android:hint="Notes"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/statusLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Status" />
        <Spinner
            android:id="@+id/statusSpinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:entries="@array/status_array" />

    </LinearLayout>


   </android.support.v7.widget.CardView>

   <android.support.design.widget.FloatingActionButton
    android:id="@+id/saveFab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="120dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@android:drawable/ic_menu_save" />

 </android.support.constraint.ConstraintLayout>
答案

您的Recycler View Width是0dp可能是这个问题。更新您的布局content_main.xml,如下所示

    <android.support.constraint.ConstraintLayout 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="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">


   <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="wrap_content"
    android:layout_height="429dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="22dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="60dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
   </android.support.constraint.ConstraintLayout>

以上是关于Recyclerview没有显示cardview的主要内容,如果未能解决你的问题,请参考以下文章

可扩展的自定义 CardView 不显示来自 Firebase 的 RecyclerView 的所有内容

在RecyclerView中翻转CardView

如何在 RecyclerView 的 CardView 中实现对 Item Click 的操作以显示结果

Android Material Design系列之RecyclerView和CardView

创建卡片时如何在 recyclerview (cardview) 中添加日期。 (卡片创建日期)

如何在recyclerview的gridLayoutManager中从cardview中删除右边距