我是Android编程的新手。我正在尝试制作一个包含视频列表的单页应用程序,我可以滚动并执行此操作,我正在使用RecyclerView。在我的活动中,它说视图无法转换为RecyclerView,据我所知,RecyclerView没有任何小部件。我该怎么办?我会附上我的代码。
我的活动:
package com.arvindsudheer.youtubeplayertesting;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
/**
* Created by ArvindSudheer on 6/10/2017.
*/
class RecyclerView {
public LinearLayoutManager layoutManager;
public MyAdapter adapter;
public boolean hasFixedSize;
private void setLayoutManager(LinearLayoutManager layoutManager) {
this.layoutManager = layoutManager;
}
private void setAdapter(MyAdapter adapter) {
this.adapter = adapter;
}
private void setHasFixedSize(boolean hasFixedSize) {
this.hasFixedSize = hasFixedSize;
}
/**
* Created by ArvindSudheer on 6/9/2017.
*/
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.childview1);
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
//to use RecycleView, you need a layout manager. default is
LinearLayoutManager
LinearLayoutManager linearLayoutManager=new
LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter adapter=new MyAdapter(MyActivity.this);
recyclerView.setAdapter(adapter);
}
}
}
我的适配器:
package com.arvindsudheer.youtubeplayertesting;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VideoInfoHolder> {
//these ids are the unique id for each video
String[] VideoID = {"kN6lRqzmcOY", "MzmSFocMdJk", "L3LixkCN5tM","EuNaQCUjtCw","d-I2JSlEKtA","rwSf_12aMfE","13HIRNVFRlk","BH5_7IYvcVE","Lxc8-INx5G0","iwE8foegHkM","kCKUZdqo600","dr36AF4MqjY","IrMLXTw0528","gyxgVsXMYq0","71MTp2Ley3U","oeNRrGaGV20","loxMW2honqw","Sv63K3ESstA","7U8NzRAvy-I","2kdcR1QBqAI","n3w5c2VYcoU","2zA8W7h7ays","qfkbvujyPpc","JVF8pE1KxaA","f0bi0yl7uZU","5DbdWFAs8EI","5DbdWFAs8EI","2BFUvmCrn1o","yuYbVcoq9nk","lkFlQ5ECM8Q","JuYrl_49WA4","r3L87CUTxeU","RJMvq0pgvkg","9TuDEhfOLUc","HFTTDvuPMOs","fHRh1ksUOJg","OW7Gwuz8vTU","NitQnVpvzd4","-QOEEyuKPMY"};
Context ctx;
public MyAdapter(Context context) {
this.ctx = context;
}
@Override
public VideoInfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_list_view, parent, false);
return new VideoInfoHolder(itemView);
}
@Override
public void onBindViewHolder(final VideoInfoHolder holder, final int position) {
final YouTubeThumbnailLoader.OnThumbnailLoadedListener onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener(){
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
youTubeThumbnailView.setVisibility(View.VISIBLE);
holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
}
};
holder.youTubeThumbnailView.initialize(Resource.KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(VideoID[position]);
youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
//write something for failure
}
});
}
@Override
public int getItemCount() {
return VideoID.length;
}
public class VideoInfoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected RelativeLayout relativeLayoutOverYouTubeThumbnailView;
YouTubeThumbnailView youTubeThumbnailView;
protected ImageView playButton;
public VideoInfoHolder(View itemView) {
super(itemView);
playButton=(ImageView)itemView.findViewById(R.id.btnYoutube_player);
playButton.setOnClickListener(this);
relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail);
}
@Override
public void onClick(View v) {
Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) ctx, Resource.KEY, VideoID[getLayoutPosition()]);
ctx.startActivity(intent);
}
}
private class Resource {
public static final String KEY = "AIzaSyDOv0yWBimW8L8zIiYfTDYH9_9jFpZWgEw";
}
}
每个项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="@drawable/image_ripple"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="0dp"
android:layout_marginTop="10dp"
card_view:cardBackgroundColor="@android:color/black"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtube_thumbnail"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:visibility="visible"/>
<RelativeLayout android:id="@+id/relativeLayout_over_youtube_thumbnail"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@color/color_background_transparent"
android:visibility="visible">
<ImageView android:id="@+id/btnYoutube_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@mipmap/ic_youtube_play_button"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
主要布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>