Java 在一个片段中添加回收器视图和youtube播放器视图
我试图在一个片段中获得youtube播放器和回收器视图。到目前为止,我成功地实现了布局和回收器视图数据(youtube视频的json解析),但不幸的是,我未能在回收器视图上应用onItemCLick侦听器Java 在一个片段中添加回收器视图和youtube播放器视图,java,android-fragments,android-recyclerview,youtube,Java,Android Fragments,Android Recyclerview,Youtube,我试图在一个片段中获得youtube播放器和回收器视图。到目前为止,我成功地实现了布局和回收器视图数据(youtube视频的json解析),但不幸的是,我未能在回收器视图上应用onItemCLick侦听器 package fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android
package fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.currentmedia.channellayout2.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.google.android.youtube.player.YouTubePlayerSupportFragmentX;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import adapter.AdapterHome;
import models.ModelHome;
import models.VideoID;
import models.VideoYT;
import network.YoutubeAPI;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import utils.Constants;
import utils.RecyclerViewOnClickListener;
import utils.RecyclerViewOnClickListener2;
import utils.RecyclerViewOnClickListener3;
import static android.content.ContentValues.TAG;
public class Fragment2 extends Fragment {
private AdapterHome adapter;
private YouTubePlayer youTubePlayer;
private LinearLayoutManager manager;
private List<VideoYT> videoList = new ArrayList<>();
private boolean isScroll = false;
private int currentItem, totalItem, scrollOutItem;
private String nextPageToken = "";
private YouTubePlayerSupportFragmentX youTubePlayerFragment2;
public
Fragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
// Inflate the layout for this fragment
RecyclerView rv = view.findViewById(R.id.recyclerView);
adapter = new AdapterHome(getContext(),videoList);
manager = new LinearLayoutManager(getContext());
rv.setAdapter(adapter);
rv.setLayoutManager(manager);
//Youtube Fragment
final YouTubePlayerSupportFragmentX youTubePlayerFragment2 = YouTubePlayerSupportFragmentX.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_layout, youTubePlayerFragment2).commit();
youTubePlayerFragment2.initialize(Constants.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public
void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer = player;
//set the player style default
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
//cue the 1st video by default
youTubePlayer.loadVideo(String.valueOf(videoList.get(0)));
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
}
}
@Override
public
void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.e(TAG, "Youtube Player View initialization failed");
}
});
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
isScroll = true;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentItem = manager.getChildCount();
totalItem = manager.getItemCount();
scrollOutItem = manager.findFirstVisibleItemPosition();
if (isScroll && (currentItem + scrollOutItem == totalItem)){
isScroll = false;
getJson();
}
}
});
rv.addOnItemTouchListener(new RecyclerViewOnClickListener3(this, new RecyclerViewOnClickListener3.OnItemClickListener() {
@Override
public
void onItemClick(View view, int position) {
}
}));
if (videoList.size() == 0){
getJson();
}
return view;
}
private void getJson() {
String url = YoutubeAPI.BASE_URL + YoutubeAPI.sch + YoutubeAPI.KEY + YoutubeAPI.mx + YoutubeAPI.ord
+ YoutubeAPI.part + YoutubeAPI.query1 + YoutubeAPI.type;
if (nextPageToken != ""){
url = url + YoutubeAPI.NPT + nextPageToken;
}
if (nextPageToken == null){
return;
}
Call<ModelHome> data = YoutubeAPI.getVideo().getHomeVideo(url);
data.enqueue(new Callback<ModelHome>() {
@Override
public void onResponse(Call<ModelHome> call, Response<ModelHome> response) {
if (response.errorBody() != null){
Log.w(TAG, "onResponse: " + response.errorBody() );
try {
Toast.makeText(getContext(), response.errorBody().string(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
} else {
ModelHome mh = response.body();
videoList.addAll(mh.getItems());
adapter.notifyDataSetChanged();
if (mh.getNextPageToken() != null){
nextPageToken = mh.getNextPageToken();
}
}
}
@Override
public void onFailure(Call<ModelHome> call, Throwable t) {
Log.e(TAG, "onFailure: ", t);
Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
我在活动中采用了此方法,但它不是在片段中运行的。
碎片的布局是
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@android:color/white"
tools:context="fragment.Fragment1">
<FrameLayout
android:id="@+id/youtube_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/recyclerView"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/youtube_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的主要问题是**我想在recycler视图中从youtube导入一个视频列表,然后在每个视频项目上应用onclicklistener,在同一片段中附加的youtube播放器中播放视频**如果有人能指导我在获取JSON的同时在片段中实现youtube播放器片段和recycler视图,请数据。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@android:color/white"
tools:context="fragment.Fragment1">
<FrameLayout
android:id="@+id/youtube_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/recyclerView"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/youtube_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>