我在引用ViewHolders中的容器“ youtube_fragmento”时遇到问题
我的想法是制作一个带有viewHolders的RecyclerView,以便在YoutubePlayerFragment和PreviewFragment之间切换(通过更改容器内的片段), 仅激活点击视频的YoutubeVideoPlayerFragment
将RecyclerView放在其中的片段:
public class CanalesFragment extends Fragment {
private static final String ARG_PARAM1 = "1";
private static final String ARG_PARAM2 = "2";
ListView lvVideo;
String searchName;
String TAG="ChannelActivity";
String URL="https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UC9CYT9gSNLevX5ey2_6CK0Q&maxResults=25&key="+YoutubeConfig.getApiKey();
private String mParam1;
private String mParam2;
private static CanalesRecyclerAdapter canalesRecyclerAdapter;
public static ArrayList<video> videos;
private OnFragmentInteractionListener mListener;
public CanalesFragment() {
// Required empty public constructor
}
public static CanalesFragment newInstance(String param1, String param2) {
CanalesFragment fragment = new CanalesFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_canales, container, false);
videos = new ArrayList<video>();
videoParser.getVideosFromUrl(URL);
RecyclerView recyclerView = v.findViewById(R.id.recyclerViewCanales);
canalesRecyclerAdapter = new CanalesRecyclerAdapter(getContext(),getActivity(),
this.getChildFragmentManager(),videos);
canalesRecyclerAdapter.setOnItemClickListener(new
CanalesRecyclerAdapter.OnItemClickListener() {
@Override
public void onItemClick(FrameLayout b, View view, video obj, int position)
{//custom made method to input a ClickListener to the adapter
for (int i = 0; i < videos.size(); i++) { //when one video is clicked i set them all to PreviewFragment
videos.get(i).reproduciendo=false;
}
canalesRecyclerAdapter.notifyDataSetChanged();
videos.get(position).reproduciendo=true; //then change to
YoutubePlayerFragment at the one clicked
canalesRecyclerAdapter.notifyItemChanged(position);
}
});
recyclerView.setAdapter(canalesRecyclerAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public static void updateRecyclerView(){
canalesRecyclerAdapter.notifyDataSetChanged();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
这是RecyclerAdapter:
public class CanalesRecyclerAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
@NonNull
Context context;
Activity activity;
private OnItemClickListener mOnItemClickListener;
ArrayList<video> videos;
private StorageReference mStorageRef;
private FragmentManager childFragmentManager;
final private int HACK_ID_PREFIX = 12345678; //preset number to generate IDs
public CanalesRecyclerAdapter(Context context, Activity activity,
FragmentManager childFragmentManager, ArrayList<video> videos) {
this.context = context;
this.videos = videos;
this.activity=activity;
this.childFragmentManager = childFragmentManager;
this.mStorageRef = FirebaseStorage.getInstance().getReference();
}
public interface OnItemClickListener {
void onItemClick(FrameLayout b , View view, video obj, int position);
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.itemyoutube, viewGroup, false);
videoItem item = new videoItem(row,context,videos);
return item;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
bindVideo((videoItem)viewHolder, i);
final video s = videos.get(i);
if(videos.get(i).reproduciendo){ //if this video is the one that should play
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = childFragmentManager.beginTransaction();
transaction.replace(HACK_ID_PREFIX+i, youTubePlayerFragment).commit();
YouTubePlayer.OnInitializedListener OIL = new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo(videos.get(i).videoID);
youTubePlayer.setShowFullscreenButton(false);
youTubePlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
@Override
public void onPlaying() {
youTubePlayer.seekToMillis(0);
}
@Override
public void onPaused() {
}
@Override
public void onStopped() {
}
@Override
public void onBuffering(boolean b) {
}
@Override
public void onSeekTo(int i) {
}
});
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
};
youTubePlayerFragment.initialize(YoutubeConfig.getApiKey(), OIL);
}else{ //if its not playing we replace container with a Preview
FragmentTransaction transaction = childFragmentManager.beginTransaction();
videoPreviewFragment frag = videoPreviewFragment.newInstance(videos.get(i).videoID,"");
transaction.replace(HACK_ID_PREFIX+i,frag).commit();
}
((videoItem) viewHolder).youtubeVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(((videoItem) viewHolder).youtubeVideo,v, s, i);
}
}
});
}
private void bindVideo(final videoItem viewHolder, int i) { //para darle un tag al momento de asignar el video
View view = viewHolder.itemView.findViewWithTag("video_container");
if (view != null) {
Log.e("idGenerado", String.valueOf(HACK_ID_PREFIX+i));
view.setId(HACK_ID_PREFIX + i);
}
}
@Override
public int getItemCount() {
return videos.size();
}
}
class videoItem extends RecyclerView.ViewHolder{
FrameLayout youtubeVideo;
ArrayList<video> videos = new ArrayList<video>();
Context context;
public videoItem(View itemView, Context context, ArrayList<video> videos) {
super(itemView);
this.videos = videos;
this.context = context;
youtubeVideo = itemView.findViewById(R.id.youtube_fragmento);
}}
item_youtube的布局,为viewHolder夸大了:
<FrameLayout 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="260dp"
android:background="#FFFFFF"
android:id="@+id/youtube_fragmento"
android:layout_centerHorizontal="true"
android:tag="video_container"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.532"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
当我将onBindViewHolder放在容器中的片段下面的代码中时,它始终替换为第一个RecyclerView项的Container [R.id.youtube_fragmento]:
FragmentTransaction transaction =
getChildFragmentManager().beginTransaction();
transaction.replace(R.id.youtube_fragmento,
youTubePlayerFragment).commit();
显然,在这种情况下,它为容器[R.id.youtube_fragmento]赋予了每个viewHolder相同的ID,
所以我选择给每个容器一个自己的ID(使用bindVideo()函数),以根据位置为容器设置自定义ID
它适用于前3个viewHolders(适合第一个绘制的屏幕),但是在滚动查看更多内容后,出现此错误,即ID为“ 0xbc6151”,将bindVideo()设置为第4个viewHolder容器
错误:“ 无效的ID 0x00bc6151。 找不到片段videoid的ID 0xbc6151的视图(未知){32586d8#25 id = 0xbc6151}“
我还尝试设置viewHolder.isRecyclable(false)来检查回收是否给我带来了自定义ID问题,并且问题仍然存在