位于ReFragmenterView中的YoutubePlayerView(位于ChildFragment

时间:2019-05-19 10:51:56

标签: java android

我需要有关YouTubePlayerView的帮助。我有一个父片段,一个子片段,一个位于该子片段中的recyclerview和一个youtubePlayerView,它是此recyclerView的项目

我尝试了简单的VideoView,但是它运行缓慢,我认为使用youtubePlayerView会比它更好。我为recyclerView创建了一个适配器,并在其中初始化了youtubePlayerView。我也不想扩展YouTubeBaseActivity,因为那样我就不能使用getSupportFragmentManager()方法将该父片段放置在活动中

♦♦♦这是我的活动:♦♦♦

@BindView(R.id.navigation_view_options)
ListView nav_view_options;

@BindView(R.id.fragment_container)
FrameLayout container;

@BindView(R.id.home_page)
ImageButton home_btn;

@BindView(R.id.news_page)
ImageButton news_btn;

@BindView(R.id.games_page)
ImageButton games_btn;

@BindView(R.id.gallery_page)
ImageButton gallery_btn;

private Constants constants = new Constants();

private Context context = this;

private List_NavigationViewOptionsAdapter adapter;

private int lang_id = 1;

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

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    ButterKnife.bind(this);

    adapter = new List_NavigationViewOptionsAdapter(context, constants.getNav_view_options());
    nav_view_options.setAdapter(adapter);

    setDefaultSettings();

}

private void setDefaultSettings() {
    homePageBtnClicked(null);
}

private void setUncheckedPageButtons(int id){

    home_btn.setImageResource(R.drawable.home_icon_dark);
    news_btn.setImageResource(R.drawable.live_icon_dark);
    games_btn.setImageResource(R.drawable.football_icon_dark);
    gallery_btn.setImageResource(R.drawable.gallery_icon_dark);

    if(id == home_btn.getId()) home_btn.setImageResource(R.drawable.home_bottom_navigation_bar);
    else if(id == news_btn.getId()) news_btn.setImageResource(R.drawable.live_bottom_navigation_bar);
    else if(id == games_btn.getId()) games_btn.setImageResource(R.drawable.football_bottom_navigation_bar);
    else gallery_btn.setImageResource(R.drawable.gallery_bottom_navigation_bar);

}

@OnClick(R.id.home_page)
void homePageBtnClicked(View view) {

    setUncheckedPageButtons(home_btn.getId());
    fragmentChanger(new HomeFragment(context));

}

@OnClick(R.id.news_page)
void newsPageBtnClicked(View view) {

    setUncheckedPageButtons(news_btn.getId());
    fragmentChanger(new MediaFragment(context));
}

@OnClick(R.id.games_page)
void gamesPageBtnClicked(View view) {

    setUncheckedPageButtons(games_btn.getId());
    //fragmentChanger(new GamesFragment());
}

@OnClick(R.id.gallery_page)
void galleryPageBtnClicked(View view) {

    setUncheckedPageButtons(gallery_btn.getId());
    //fragmentChanger(new GalleryFragment());
}

@SuppressLint("ResourceType")
private void fragmentChanger(Fragment fragment) {

    getSupportFragmentManager()
            .beginTransaction()
            .setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left)
            .replace(container.getId(), fragment)
            .addToBackStack(null)
            .commit();

}

@Override
public void openNavigationViewPressed() {
    ((DrawerLayout) findViewById(R.id.activity_home_page)).openDrawer(Gravity.START);
}

@Override
public void goToNewsDetailsActivity(String ID, View view) {

    Intent intent = new Intent(HomePageActivity.this, NewsDetailsActivity.class);

    Bundle bundle = new Bundle();

    bundle.putString("newsID",ID);
    bundle.putInt("lang_id",lang_id);

    intent.putExtra("newsDetails",bundle);

    startActivity(intent);

    startActivity(intent);
}

@Override
public void onFragmentInteraction() {

}

@Override
public void onFragmentInteraction(Uri uri) {

}

♦♦♦这是我的父片段:♦♦♦

private MediaFragmentInterface mListener;

private Context context;

@BindView(R.id.fragment_container)
FrameLayout container;

@BindView(R.id.media_tabs)
TabLayout mediaTabs;

@SuppressLint("ValidFragment")
public MediaFragment(Context context) {

    this.context = context;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_media, container, false);

    ButterKnife.bind(this, view);

    mediaTabs.addTab(mediaTabs.newTab().setText(Constants.NEWS_TAB),true);
    mediaTabs.addTab(mediaTabs.newTab().setText(Constants.VIDEOS_TAB));

    mediaTabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            if(tab.getText().equals(Constants.NEWS_TAB)) fragmentChanger(new NewsFragment());
            else fragmentChanger(new VideosFragment(context));

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    return view;
}

@OnClick(R.id.open_nav_menu_btn)
void OpenNavMenu(View view){

    onButtonPressed();

}

public void onButtonPressed() {
    if (mListener != null) {

        mListener.openNavigationViewPressed();

    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof MediaFragmentInterface) {

        mListener = (MediaFragmentInterface) context;

    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

private void fragmentChanger(Fragment fragment) {

    getChildFragmentManager()
            .beginTransaction()
            .setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left)
            .replace(container.getId(), fragment)
            .addToBackStack(null)
            .commit();

}

public interface MediaFragmentInterface {

    void openNavigationViewPressed();

}

♦♦♦这是我的孩子片段:♦♦♦

private RetrofitInterface userService = HttpServiceSingleton.getInstance().getRetrofitInterface();

private VideosFragmentInterface mListener;

@BindView(R.id.videos_recycler)
RecyclerView videos_recycler;

private LinearLayoutManager mLayoutManager;

private int lang_id = 1;
private Context context;

@SuppressLint("ValidFragment")
public VideosFragment(Context context) {

    this.context = context;

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_videos, container, false);

    ButterKnife.bind(this, view);

    Call<List<Videos>> videoCall = userService.getVideos(lang_id);

    videoCall.enqueue(new Callback<List<Videos>>() {
        @Override
        public void onResponse(Call<List<Videos>> call, Response<List<Videos>> response) {

            videos_recycler.setAdapter(new Recycler_VideosAdapter(context, response.body()));
            videos_recycler.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(context);
            videos_recycler.setLayoutManager(mLayoutManager);
            videos_recycler.addItemDecoration(new EqualSpacingItemDecoration(15,EqualSpacingItemDecoration.HORIZONTAL));

        }

        @Override
        public void onFailure(Call<List<Videos>> call, Throwable t) {

        }
    });

    return view;
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof VideosFragmentInterface) {
        mListener = (VideosFragmentInterface) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface VideosFragmentInterface {

    void onFragmentInteraction(Uri uri);
}

♦♦♦这是我的recyclerView适配器:♦♦♦

private Context context;
private List<Videos> videosList;

@BindView(R.id.video)
YouTubePlayerView videoView;

@BindView(R.id.name_video)
TextView nameVideo;

private YouTubePlayer.OnInitializedListener onInitializedListener;

public Recycler_VideosAdapter(Context context, List<Videos> videosList) {

    this.context = context;
    this.videosList = videosList;

}

@NonNull
@Override
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    return new Recycler_VideosAdapter.ViewHolder(
            LayoutInflater.from(context)
                    .inflate(R.layout.recyclerview_videos, viewGroup, false));

}

@Override
public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, final int i) {

    ButterKnife.bind(this, baseViewHolder.itemView);

     onInitializedListener = new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

            youTubePlayer.loadVideo(videosList.get(i).getYoutubeApiUrl().substring(50,61));

        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

            Log.e("VideoAdapter","failed initialization");

        }
    };



    /*
    Videos video = videosList.get(i);

    nameVideo.setText(video.getTitle());
    */
}

@OnClick(R.id.play_btn)
void PlayBtnClicked(View view) {

    videoView.initialize(YouTubeConfig.getApiKey(), onInitializedListener);

}

@Override
public int getItemCount() {
    return videosList.size();
}

public class ViewHolder extends BaseViewHolder {

    ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    protected void clear() {

    }

    public void onBind(int position) {

        super.onBind(position);

    }
}

♦♦♦♦最后,这是我的recyclerview项的xml布局:♦♦♦

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/video"
    android:layout_width="@dimen/video_width_big"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<ImageButton
    android:id="@+id/play_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_play_arrow_black_24dp"
    android:background="@color/another_tone_purple"
    app:layout_constraintTop_toTopOf="@id/video"
    app:layout_constraintStart_toStartOf="@id/video"
    android:layout_marginTop="@dimen/margin_10"
    android:layout_marginStart="@dimen/margin_20"/>

<TextView
    android:id="@+id/name_video"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/video"
    app:layout_constraintStart_toStartOf="@id/video"
    app:layout_constraintEnd_toEndOf="@id/video"
    android:background="@color/another_tone_purple"
    android:textColor="@color/white"
    android:padding="@dimen/margin_5"
    android:textSize="@dimen/too_small_font"
    android:fontFamily="@font/avenir_regular"/>

错误:

Process: com.example.qarabagh_fc, PID: 26181
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.qarabagh_fc/com.example.qarabagh_fc.Activities.HomePageActivity}: java.lang.RuntimeException: Unknown animator name: translate
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6981)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
     Caused by: java.lang.RuntimeException: Unknown animator name: translate
        at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:691)
        at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:680)
        at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:642)
        at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:126)
        at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:106)
        at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:91)
        at android.app.FragmentManagerImpl.loadAnimator(FragmentManager.java:1108)
        at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1597)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1637)
        at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2219)
        at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2165)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2066)
        at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3055)
        at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3002)
        at android.app.FragmentController.dispatchActivityCreated(FragmentController.java:183)
        at android.app.Activity.performCreate(Activity.java:7333)
        at android.app.Activity.performCreate(Activity.java:7317)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3066)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:6981) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

0 个答案:

没有答案