Я разрабатываю приложение android. В моем приложении есть Youtube Android Player Api. Под плеером у меня есть текстовое представление, обернутое внутри прокрутки. У меня есть два отдельных файла макета для активности игрока. Один для портретного режима и один для ландшафтного. Я также добавил SaveInstanceState
в свой класс Player, но мой Player перестал играть в ландшафтном режиме. Я также пытался добавить Android:configChanges="orientation|screenSize|keyboardHidden
в моем манифесте. Но когда я добавляю это, плеер теперь работает в ландшафтном режиме, но я больше не могу прокручивать в ландшафтном режиме. Пожалуйста, помогите мне решить эту проблему и заранее благодарим вас за помощь. Вот мои коды.
public class Player extends `YouTubeBaseActivity` {
private `TextView mtextView`;
private TextView mtextView1;
private static String API_KEY="AIzaSyD2v_wK-vFJHkfWw4wQ9CwU6xrgyNN5K_0";
private static String VIDEO_CODE= "";
private static String mText = "";
private static String mText1 = "";
YouTubePlayerView player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
mtextView = (TextView) findViewById(R.id.tv);
mtextView1 = (TextView) findViewById(R.id.tv1);
//Recieve data
Intent intent = getIntent();
mText = intent.getExtras().getString("text");
mText1 = intent.getExtras().getString("text1");
VIDEO_CODE = intent.getExtras().getString("videocode");
player = (YouTubePlayerView) findViewById(R.id.youtubePlay);
player.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
youTubePlayer.loadVideo(VIDEO_CODE);
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
mtextView.setText(mText);
mtextView1.setText(mText1);
if (savedInstanceState != null) {
VIDEO_CODE = savedInstanceState.getString("Video");
API_KEY = savedInstanceState.getString("api");
mText = savedInstanceState.getString("Tent");
mText1 =savedInstanceState.getString("Tent1");
player.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
youTubePlayer.loadVideo(VIDEO_CODE);
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
mtextView.setText(mText);
mtextView1.setText(mText1);
}
}
@Override
protected void onSaveInstanceState(Bundle outstate) {
super.onSaveInstanceState(outstate);
outstate.putString("Video",VIDEO_CODE);
outstate.putString("api", API_KEY);
outstate.putString("Tent", mText);
outstate.putString("Tent1", mText1);
}
}
publi c class RecyclerViewAdapter extends RecyclerView.Adapter {
private Context mContext;
private List<FoodName> mData;
public RecyclerViewAdapter(Context mContext, List<FoodName> mData) {
this.mContext = mContext;
this.mData = mData;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater minflater = LayoutInflater.from(mContext);
view = minflater.inflate(R.layout.cardview_item_food,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
holder.tv_food_title.setText(mData.get(position).getTitle());
holder.food_img.setImageResource(mData.get(position).getImage());
//set click listener
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,Player.class);
//passing data to player activity
intent.putExtra("text", mData.get(position).getText());
intent.putExtra("text1",mData.get(position).getText1());
intent.putExtra("videocode",mData.get(position).getVideocode());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public void filterList(ArrayList<FoodName> filteredList)
{
mData = filteredList;
notifyDataSetChanged();
}
public static class MyViewHolder extends RecyclerView.ViewHolder
{
TextView tv_food_title;
ImageView food_img;
CardView cardView;
public MyViewHolder(View itemView)
{
super(itemView);
tv_food_title =(TextView)itemView.findViewById(R.id.food_title_id);
food_img =(ImageView)itemView.findViewById(R.id.food_image_id);
cardView = (CardView)itemView.findViewById(R.id.cardview_id);
}
}
}
И вот это два файла макета для моей деятельности игрока: один для портретной ориентации и один для альбомной ориентации. Для книжной ориентации.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Player">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="108dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:gravity="center"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
/>
</LinearLayout>
<view
android:id="@+id/youtubePlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.google.android.youtube.player.YouTubePlayerView"
app:layout_constraintTop_toBottomOf="@+id/linear"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@+id/youtubePlay">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:text=""/>
</ScrollView>
</LinearLayout>
</RelativeLayout>
Для альбомной ориентации.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".Player">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="108dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:gravity="center"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
/>
</LinearLayout>
<view
android:id="@+id/youtubePlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.google.android.youtube.player.YouTubePlayerView"
app:layout_constraintTop_toBottomOf="@+id/linear"/>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@+id/youtubePlay"
android:text=""/>
</LinearLayout>
</ScrollView>
</LinearLayout>