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.
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>
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 } }
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); } }
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) {} });
- Avoid Fragment Overhead: Using
PagerAdapterdirectly keeps your code lightweight since you’re working with rawViewinstances 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
FrameLayoutlets you add anyViewtype—TextViews, ImageViews, custom layouts, etc.—dynamically.
内容的提问来源于stack exchange,提问作者AkshayT

