You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ViewPagerAdapter场景下YouTube播放器因未授权覆盖停止播放,求非Fragment解决方案

Hey Akshay, I totally get how frustrating it is to spend hours hunting for solutions only to find every single one is built around Fragments—when you’re working with a plain ViewPagerAdapter and FrameLayout. Let’s fix that right up with a practical, tailored approach.


Step 1: Finalize Your Page Layout (With FrameLayout)

First, let’s complete the layout you started. We’ll add a FrameLayout as a dynamic container for your content, which you can update later:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout android:id="@+id/promotion_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- Dynamic content container -->
        <FrameLayout
            android:id="@+id/content_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <!-- Add any fixed UI elements here (buttons, headers, etc.) -->
        <Button
            android:id="@+id/action_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Next Page"/>
    </LinearLayout>
</RelativeLayout>
Step 2: Build a Custom ViewPagerAdapter (No Fragments)

Instead of using FragmentPagerAdapter, we’ll extend the base PagerAdapter to directly manage View instances. This lets you work with your FrameLayout directly:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;

public class CustomViewPagerAdapter extends PagerAdapter {
    private final Context mContext;
    private final List<View> mPageViews;

    public CustomViewPagerAdapter(Context context, List<View> pageViews) {
        mContext = context;
        mPageViews = pageViews;
    }

    @Override
    public int getCount() {
        return mPageViews.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object; // Verify the view matches the stored object
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View pageView = mPageViews.get(position);
        
        // Access the FrameLayout and add initial content
        FrameLayout contentContainer = pageView.findViewById(R.id.content_container);
        TextView initialText = new TextView(mContext);
        initialText.setText("Page " + (position + 1) + " - Initial Content");
        initialText.setGravity(View.TEXT_ALIGNMENT_CENTER);
        contentContainer.addView(initialText);

        container.addView(pageView);
        return pageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object); // Clean up unused views
    }
}
Step 3: Set Up the Adapter in Your Activity

Now, initialize your ViewPager and wire up the adapter with your page views:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private CustomViewPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewPager = findViewById(R.id.view_pager);
        LayoutInflater inflater = getLayoutInflater();

        // Create page views using your layout file
        List<View> pageViews = new ArrayList<>();
        for (int i = 0; i < 3; i++) { // Adjust the number of pages as needed
            View pageView = inflater.inflate(R.layout.your_page_layout, null);
            pageViews.add(pageView);
        }

        // Attach the adapter to the ViewPager
        mAdapter = new CustomViewPagerAdapter(this, pageViews);
        mViewPager.setAdapter(mAdapter);
    }
}
Step 4: Update FrameLayout Content On Page Switch (Optional)

If you need to refresh the FrameLayout content when the user swipes pages, add a page change listener:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        // Get the current page's FrameLayout
        View currentPage = mAdapter.mPageViews.get(position);
        FrameLayout contentContainer = currentPage.findViewById(R.id.content_container);
        
        // Clear existing content and add new content
        contentContainer.removeAllViews();
        TextView updatedText = new TextView(MainActivity.this);
        updatedText.setText("Page " + (position + 1) + " - Updated Content!");
        updatedText.setGravity(View.TEXT_ALIGNMENT_CENTER);
        contentContainer.addView(updatedText);
    }

    @Override
    public void onPageScrollStateChanged(int state) {}
});
Key Notes to Keep in Mind
  • Avoid Fragment Overhead: Using PagerAdapter directly keeps your code lightweight since you’re working with raw View instances instead of Fragments.
  • Reuse Views: For better performance, modify the adapter to reuse views instead of inflating new ones every time (similar to a RecyclerView.Adapter).
  • Flexible Content: The FrameLayout lets you add any View type—TextViews, ImageViews, custom layouts, etc.—dynamically.

内容的提问来源于stack exchange,提问作者AkshayT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:01:52