【问题标题】:Hide Recyclerview child apart from the one i clicked隐藏我点击的 Recyclerview 孩子
【发布时间】:2020-06-03 13:06:18
【问题描述】:

我正在使用 Pierfrancesco Soffritti 的 YouTubePlayerView 实现一个水平回收视图,以显示一系列视频。 当我单击按钮时,我添加了一个全屏侦听器以全屏显示。 我试图做的是在全屏时隐藏所有recyclerview子项,然后在退出全屏时恢复视图。 以某种方式可能吗? 提前谢谢你

已编辑

这是我的 Adapter 类,我想在 OnBind 中选择我点击的 Recyclerview 项并隐藏它

公共类 VideoAdapter 扩展 RecyclerView.Adapter { private static final String TAG = "VideoAdapter";

private List<Video> mVideos;
private Lifecycle mLifecycle;


public VideoAdapter(List<Video> mVideos, Lifecycle mLifecycle) {
    this.mVideos = mVideos;
    this.mLifecycle = mLifecycle;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    YouTubePlayerView youTubePlayerView = (YouTubePlayerView) LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_videos, parent, false);
    mLifecycle.addObserver(youTubePlayerView);

    return new ViewHolder(youTubePlayerView);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
    holder.cueVideo(mVideos.get(position).getVideoId());
}

@Override
public int getItemCount() {
    if(!(mVideos == null))
        return mVideos.size();
    return 0;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    private YouTubePlayerView youTubePlayerView;
    private YouTubePlayer youTubePlayer;
    private String currentVideoId;

    ViewHolder(YouTubePlayerView playerView) {
        super(playerView);
        youTubePlayerView = playerView;
        youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady(@NonNull YouTubePlayer initializedYouTubePlayer) {
                youTubePlayer = initializedYouTubePlayer;
                youTubePlayer.cueVideo(currentVideoId, 0);
            }
        });
    }

    void cueVideo(String videoId) {
        currentVideoId = videoId;

        if(youTubePlayer == null)
            return;

        youTubePlayer.cueVideo(videoId, 0);
    }
}

}

【问题讨论】:

  • 您可以尝试添加一些代码来显示您已经完成的工作吗?
  • 嗨,克里斯蒂,我编辑了我的帖子。谢谢
  • 这很难做到,也很难维护......我建议你添加一个带有回调的监听器,该回调从适配器中获取点击行的信息;并且该侦听器可以在RecyclerView 之上打开一些片段/视图;甚至你可以隐藏RecyclerView
  • 是的,我基本上退出了啊

标签: android android-recyclerview


【解决方案1】:

我没有使用 RecyclerView,但我正在使用 LinearLayout 在我的应用程序中解决(并解决)了一个类似问题 - 现在我修改了我的 代码只是为了 YT 全屏(将其视为“假”全屏“-您正在旋转整个布局,同时保持纵向模式:-))。

简而言之:

  • 创建全屏布局并将其添加到父布局;
  • .getPlayerUiController().setFullScreenButtonClickListener 中从布局中移除 YT 播放器视图(例如 trailerLayout)并将相同的 YT 播放器视图添加到全屏布局中;
  • 退出全屏时,从全屏布局中移除 YT 播放器视图,并将相同的 YT 播放器视图添加到 trailerLayout

并且除了全屏之外,我还添加了几行代码来覆盖后退按钮的行为,所以它会

  • 不退出应用,只将应用移至后台
  • 在全屏时退出全屏。

这是代码,希望对你有帮助:-)

  • activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:id="@+id/fullscreen">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/trailer">
    </LinearLayout>
</LinearLayout>
  • MainActivity.kt
import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.LinearLayout
import androidx.activity.OnBackPressedCallback
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView

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

        this?.onBackPressedDispatcher?.addCallback(
                this,
                object : OnBackPressedCallback(
                        true
                ) {
                    override fun handleOnBackPressed() {
                        moveTaskToBack(true)
                    }
                })

        val trailerLayout = findViewById<LinearLayout>(R.id.trailer)
        val fullScreenLayout = findViewById<LinearLayout>(R.id.fullscreen)

        val youTubeButton = Button(this)
        youTubeButton.text = "Load YT video"
        trailerLayout.addView(youTubeButton)

        youTubeButton.setOnClickListener {
            val rectangle = Rect()
            val window: Window = window
            window.decorView.getWindowVisibleDisplayFrame(rectangle)
            val statusBarHeight: Int = rectangle.top
            val sizeFullscreen = statusBarHeight + supportActionBar!!.height

            val fullScreenYouTubeTrailerLayout = LinearLayout(this)
            fullScreenYouTubeTrailerLayout.layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            )
            fullScreenYouTubeTrailerLayout.orientation = LinearLayout.VERTICAL

            val youTubeTrailerView = YouTubePlayerView(this)
            youTubeTrailerView.getPlayerUiController().showFullscreenButton(true)
            lifecycle.addObserver(youTubeTrailerView)
            youTubeTrailerView.addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {
                override fun onReady(youTubePlayer: YouTubePlayer) {
                    val videoId = "VppjX4to9s4"
                    youTubePlayer.cueVideo(videoId, 0f)
                }
            })
            trailerLayout.removeAllViewsInLayout()
            trailerLayout.addView(youTubeButton)
            trailerLayout.addView(youTubeTrailerView)
            fullScreenLayout.addView(fullScreenYouTubeTrailerLayout)

            this?.onBackPressedDispatcher?.addCallback(
                    this,
                    object : OnBackPressedCallback(
                            true
                    ) {
                        override fun handleOnBackPressed() {
                            if (window.decorView.systemUiVisibility ==
                                    View.SYSTEM_UI_FLAG_FULLSCREEN
                            ) {
                                val w = fullScreenLayout.width - sizeFullscreen
                                val h = fullScreenLayout.height
                                fullScreenLayout.rotation = 0f
                                fullScreenLayout.translationX = 0f
                                fullScreenLayout.translationY = 0f
                                val lp = fullScreenLayout.layoutParams as ViewGroup.LayoutParams
                                lp.height = w
                                lp.width = h
                                
                                fullScreenLayout.requestLayout()
                                fullScreenYouTubeTrailerLayout.removeAllViewsInLayout()
                                trailerLayout.addView(youTubeButton)
                                trailerLayout.addView(youTubeTrailerView)
                                youTubeTrailerView.exitFullScreen()
                                window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
                                window.decorView.systemUiVisibility = View.VISIBLE
                                window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
                                supportActionBar?.show()

                            } else moveTaskToBack(true)
                        }
                    })

            youTubeTrailerView.getPlayerUiController().setFullScreenButtonClickListener {
                fullScreenYouTubeTrailerLayout.layoutParams = LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT
                        )
                if (youTubeTrailerView.isFullScreen()) {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
                    actionBar?.show()
                    supportActionBar?.show()
                    val w = fullScreenLayout.width - sizeFullscreen
                    val h = fullScreenLayout.height
                    fullScreenLayout.rotation = 0f
                    fullScreenLayout.translationX = 0f
                    fullScreenLayout.translationY = 0f
                    val lp = fullScreenLayout.layoutParams as ViewGroup.LayoutParams
                    lp.height = w
                    lp.width = h
                    
                    fullScreenLayout.requestLayout()
                    fullScreenYouTubeTrailerLayout.removeAllViewsInLayout()
                    trailerLayout.addView(youTubeButton)
                    trailerLayout.addView(youTubeTrailerView)
                    youTubeTrailerView.exitFullScreen()
                    window.decorView.systemUiVisibility = View.VISIBLE
                    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
                } else {
                    window.setFlags(
                            WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    )
                    window.addFlags(
                            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    )

                    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN

                    actionBar?.hide()
                    supportActionBar?.hide()

                    val w = fullScreenLayout.width
                    val h = fullScreenLayout.height + sizeFullscreen
                    fullScreenLayout.rotation = 90f
                    fullScreenLayout.translationX = (w - h) / 2.toFloat()
                    fullScreenLayout.translationY = (h - w) / 2.toFloat()
                    val lp = fullScreenLayout.layoutParams as ViewGroup.LayoutParams
                    lp.height = w
                    lp.width = h
                    
                    fullScreenLayout.requestLayout()
                    trailerLayout.removeAllViewsInLayout()
                    fullScreenYouTubeTrailerLayout.addView(youTubeTrailerView)
                    youTubeTrailerView.enterFullScreen()
                }
            }
        }
    }
}

我也有一个“screenOrientation="portrait"

  • AndroidManifest.xml
...
<activity android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

对于新手,不要忘记在

中实现 YT 库
  • build.gradle (:app)
...
dependencies {
...
    implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5'
...
}
...

【讨论】:

    猜你喜欢
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2020-07-07
    • 2013-06-06
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多