简体   繁体   English

Android/Kotlin:如何在从回收站视图中单击卡片时将活动显示为弹出窗口/对话框?

[英]Android/Kotlin: How to show an activity as a popup/dialog when clicking on a card from a recyclerview?

I've searched for it everywhere but I didn't found a satisfying answer yet.我到处搜索它,但我还没有找到满意的答案。 So, I'd would like to know: How to show an activity as a popup/dialog when clicking on a card from a recyclerview?所以,我想知道:如何在从回收站视图中单击卡片时将活动显示为弹出窗口/对话框? Is it made straight through the adapter?它是直接通过适配器制作的吗? Do I need a second activity to do that or just another layout.xml is enough?我需要第二个活动来做到这一点,还是只需要另一个布局。xml 就足够了?

I've tried:我试过了:

android:theme="@style/Theme.AppCompat.Dialog"

But it's throwing the following error everytime I click on the card from the recyclerview:但是每次我从 recyclerview 中点击卡片时都会抛出以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.softdevandre.marvelapi/com.softdevandre.marvelapi.ui.DetailCharacterActivity}: android.view.InflateException: Binary XML file line #11 in com.softdevandre.marvelapi:layout/activity_detail_character: Binary XML file line #11 in com.softdevandre.marvelapi:layout/activity_detail_character: Error inflating class <unknown>

AndroidManifest.xml:安卓清单.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.softdevandre.marvelapi" >

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MarvelAPI"
        tools:targetApi="31" >
        <activity
            android:name=".ui.DetailCharacterActivity"
            android:theme="@style/Theme.AppCompat.Dialog"
            android:autoRemoveFromRecents="true"
            android:parentActivityName=".MainActivity"
            android:exported="false" >
            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_detail_character.xml: activity_detail_character.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="255dp"
    android:layout_margin="8dp"
    app:cardElevation="5dp"
    app:cardCornerRadius="8dp"
    tools:context=".ui.DetailCharacterActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/ivCharacter"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="ImageContrastCheck"
            tools:srcCompat="@tools:sample/avatars" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#80CC0000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/tvCharacterName"
                android:layout_width="match_parent"
                android:textColor="@color/white"
                android:gravity="center"
                android:layout_height="match_parent"
                android:layout_marginStart="12dp"
                android:layout_marginBottom="12dp"
                android:layout_marginEnd="12dp"
                android:layout_marginTop="8dp"
                android:textSize="16sp"
                android:textStyle="bold"
                tools:text="Character Name" />

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

ItemCharacterAdapter.kt:物品角色适配器.kt:

class ItemCharacterAdapter(private val context: Context, private val dataset: List<Characters>) :
    RecyclerView.Adapter<ItemCharacterAdapter.CharacterViewHolder>() {

    class CharacterViewHolder(binding: ItemCharacterBinding) :
        RecyclerView.ViewHolder(binding.root) {
        val textView: TextView = binding.tvCharacterName
        val imageView: ImageView = binding.ivCharacter
        val materialCard: MaterialCardView = binding.mcvCharacter
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CharacterViewHolder {
        // create new view
        val adapterLayout =
            ItemCharacterBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return CharacterViewHolder(adapterLayout)
    }

    override fun onBindViewHolder(holder: CharacterViewHolder, position: Int) {
        val item = dataset[position]
        holder.textView.text = context.resources.getString(item.name)
        holder.imageView.setImageResource(item.image)
        holder.materialCard.setOnClickListener {
            val intent = Intent(context, DetailCharacterActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getItemCount() = dataset.size
}

MainActivity.kt:主要活动.kt:

class MainActivity : AppCompatActivity() {

    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        val myDataset = Datasource().loadCharacters()

        val recyclerView = binding.rvCharacterList
        recyclerView.adapter = ItemCharacterAdapter(this, myDataset)

    }
}

Thanks in advance.提前致谢。

To start activity as dialog defined it like this in AndroidManifest.xml :要像在AndroidManifest.xml中定义的对话框那样启动活动:

<activity android:theme="@android:style/Theme.Dialog" />

Use this property inside your activity tag to avoid that your Dialog appears in the recently used apps list在您的活动标签内使用此属性以避免您的对话框出现在最近使用的应用程序列表中

android:excludeFromRecents="true"

If you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog:如果您想在用户单击对话框外部时停止销毁您的对话框/活动:

After setContentView() in your Activity use:在您的 Activity 中使用setContentView()之后:

this.setFinishOnTouchOutside(false);

Now, call startActivity() on your recycler view card click it displays as a dialog, with the previous activity shown when the user presses the back button.现在,在您的回收器视图卡上调用startActivity() ,单击它显示为一个对话框,当用户按下后退按钮时会显示上一个活动。

Note that if you are using ActionBarActivity (or AppCompat theme), you'll need to use @style/Theme.AppCompat.Dialog instead.请注意,如果您使用的是 ActionBarActivity(或 AppCompat 主题),则需要改用@style/Theme.AppCompat.Dialog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM