简体   繁体   English

如何向 CardView 添加“共享按钮”?

[英]How to add a "Share button" to a CardView?

I have a RecyclerView which has many CardViews.我有一个 RecyclerView,它有很多 CardView。 Each CardView has a unique ID.每个 CardView 都有一个唯一的 ID。

I need to add a button to each CardView so that when the user clicks this button he can share a simple link in the format of "www.domain.com/xx=ID" via Whatsapp or messenger or any other application.我需要为每个 CardView 添加一个按钮,以便当用户单击此按钮时,他可以通过 Whatsapp 或 messenger 或任何其他应用程序以“www.domain.com/xx=ID”格式共享一个简单的链接。

I tried the following code:我尝试了以下代码:

holder.myBTN.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = "text to be shared";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
});

but I am getting the following error:但我收到以下错误:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
V/FA: Inactivity, disconnecting from the service
D/AndroidRuntime: Shutting down VM
E/UncaughtException: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    at android.app.ContextImpl.startActivity(ContextImpl.java:1597)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1584)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:337)
    at com.myapp.myapp_rental.PropertiesAdapter$4.onClick(PropertiesAdapter.java:229)
    at android.view.View.performClick(View.java:5181)
    at android.view.View$PerformClick.run(View.java:20887)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5942)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=ListingActivity, _si=-3768936060483324961, timestamp=1484388033695, fatal=1}]
V/FA: Using measurement service
V/FA: Connecting to remote service
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.myapp_rental, PID: 20082
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    at android.app.ContextImpl.startActivity(ContextImpl.java:1597)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1584)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:337)
    at com.myapp.myapp_rental.PropertiesAdapter$4.onClick(PropertiesAdapter.java:229)
    at android.view.View.performClick(View.java:5181)
    at android.view.View$PerformClick.run(View.java:20887)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5942)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
    I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
    I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
    W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
    D/ResourcesManager: creating new AssetManager and set to /data/app/com.google.android.gms-2/base.apk
    W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
    W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.

For me, there is an easy solution for what you have to do.对我来说,你必须做的事情有一个简单的解决方案。

In layout of Cardview, you can add a Toolbar who contains an Easy Share Action : CardView toolbars在 Cardview 的布局中,您可以添加一个包含 Easy Share Action 的 Toolbar: CardView 工具栏

In your RecyClerView, each time you called the onBindViewHolder, you can assign items in your menuhttps://developer.android.com/training/sharing/shareaction.html with link and ID you want.在您的 RecyClerView 中,每次调用 onBindViewHolder 时,您都可以使用所需的链接和 ID 在菜单https://developer.android.com/training/sharing/shareaction.html 中分配项目。

If you have trouble with RecyclerView and CardView, follow this guide : https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156如果您在使用 RecyclerView 和 CardView 时遇到问题,请按照本指南操作: https ://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156

Edit : If you want to share via WhatsApp see : https://www.whatsapp.com/faq/en/android/28000012 or use API like https://developers.facebook.com/docs/messenger/android编辑:如果您想通过 WhatsApp 分享,请参阅: https : //www.whatsapp.com/faq/en/android/28000012或使用像https://developers.facebook.com/docs/messenger/android这样的 API

I hope I have helped.我希望我有所帮助。 :) :)

The share buttons you see normally are mostly ImageViews with an onClick attribute assigned to them.您通常看到的共享按钮主要是分配有onClick属性的ImageViews If you want to add the share button to share whatever is on the card just add an ImageView in the card layout and to the ImageView add the onClickListener and add an Intent to be deployed.如果您想添加共享按钮来共享卡片上的任何内容,只需在卡片布局中添加一个ImageView ,然后向ImageView添加onClickListener并添加一个要部署的Intent

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);

You can also add an setType function which can be set to share a link.您还可以添加可以设置为共享链接的 setType 函数。

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

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