英文:
Textview not displaying in the card view of Recycler View
问题
I want to display a String of names in the text view of recycler view. the .xml of this step is below
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:id="@+id/youtube_row_card_view"
android:layout_height="120dp"
android:layout_marginLeft="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
app:cardCornerRadius="10dp"
app:contentPadding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/video_thumbnail_image_view"
android:layout_width="140dp"
android:layout_height="90dp"
android:contentDescription="@string/thumbnail_image_view_desc"
android:scaleType="centerCrop"></com.google.android.youtube.player.YouTubeThumbnailView>
<TextView
android:id="@+id/icon_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@android:color/black"></TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
the adapter class CustomAdapter is below
package com.CurrentMediaPakLiveNews;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.viewHolder> {
Context context;
ArrayList<itemModel> items;
public class viewHolder extends RecyclerView.ViewHolder {
public TextView iconName;
public viewHolder(View itemView) {
super(itemView);
this.iconName = itemView.findViewById(R.id.icon_name);
}
}
public CustomAdapter(Context context, ArrayList<itemModel> items) {
this.context = context;
this.items = items;
}
@Override
public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.youtube_video_custom_layout, parent, false);
viewHolder viewHolder = new viewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(viewHolder holder, int position) {
holder.iconName.setText(items.get(position).getName());
}
@Override
public int getItemCount() {
return items.size();
}
}
while the itemModel class is declared as:
package com.CurrentMediaPakLiveNews;
public class itemModel {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
the MainActivity declaration for textview is below:
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView firstrecyclerView;
ArrayList<itemModel> items;
String[] iconName = {"GEo", "Ary", "Sama", "Hum", "dawn", "gnn", "aj", "92", "news1", "neo"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateDummyVideoList();
setUpRecyclerView();
populateRecyclerView();
}
private void setUpRecyclerView() {
firstrecyclerView = findViewById(R.id.first_recycler_view);
firstrecyclerView.setHasFixedSize(true);
items = new ArrayList<>();
LinearLayoutManager firstlinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
firstrecyclerView.setLayoutManager(firstlinearLayoutManager);
firstrecyclerView.setItemAnimator(new DefaultItemAnimator());
for (int i = 0; i < iconName.length; i++) {
itemModel itemModel = new itemModel();
itemModel.setName(iconName[i]);
items.add(itemModel);
}
}
private void populateRecyclerView() {
CustomAdapter adapterf = new CustomAdapter(this, items);
firstrecyclerView.setAdapter(adapterf);
}
}
The output is displaying everything except for the TEXTVIEW. no list is being displayed.
英文:
I want to display a String of names in the text view of recycler view. the .xml of this step is below
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:id="@+id/youtube_row_card_view"
android:layout_height="120dp"
android:layout_marginLeft="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
app:cardCornerRadius="10dp"
app:contentPadding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/video_thumbnail_image_view"
android:layout_width="140dp"
android:layout_height="90dp"
android:contentDescription="@string/thumbnail_image_view_desc"
android:scaleType="centerCrop" ></com.google.android.youtube.player.YouTubeThumbnailView>
<TextView
android:id="@+id/icon_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@android:color/black"></TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
the adapter class CustomAdapter is below
package com.CurrentMediaPakLiveNews;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.viewHolder> {
Context context;
ArrayList<itemModel> items;
public class viewHolder extends RecyclerView.ViewHolder {
public TextView iconName;
public viewHolder(View itemView) {
super(itemView);
this.iconName = itemView.findViewById(R.id.icon_name);
}
}
public CustomAdapter(Context context, ArrayList<itemModel> items) {
this.context = context;
this.items = items;
}
@Override
public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.youtube_video_custom_layout, parent, false);
viewHolder viewHolder = new viewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(viewHolder holder, int position) {
TextView iconName =holder.iconName;
holder.iconName.setText(items.get(position).getName());
}
@Override
public int getItemCount() {
return items.size();
}
}
while the itemModel class is declared as:
package com.CurrentMediaPakLiveNews;
public class itemModel {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
the MainActivity declaration for textview is below:
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
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.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragmentX;
import java.util.ArrayList;
import java.util.Collections;
import adapter.SecondYoutubeVideoAdapter;
import adapter.YoutubeVideoAdapter;
import utils.Constants;
import utils.RecyclerViewOnClickListener;
import utils.RecyclerViewOnClickListener2;
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
private AdView mAdView2;
//custom adapter
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView firstrecyclerView;
private RecyclerView secondrecyclerView;
ArrayList<itemModel> items;
String[] iconName = {"GEo","Ary","Sama","Hum","dawn","gnn","aj","92","news1","neo"};
//youtube player fragment
private YouTubePlayerSupportFragmentX youTubePlayerFragment;
private ArrayList<String> youtubeVideoArrayList;
private ArrayList<String> secondyoutubeVideoArrayList;
//youtube player to play video when new video selected
private YouTubePlayer youTubePlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateDummyVideoList();
initializeYoutubePlayer();
setUpRecyclerView();
populateRecyclerView();
private void setUpRecyclerView() {
firstrecyclerView = findViewById(R.id.first_recycler_view);
firstrecyclerView.setHasFixedSize(true);
items = new ArrayList<>();
//Horizontal direction recycler view
LinearLayoutManager firstlinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
firstrecyclerView.setLayoutManager(firstlinearLayoutManager);
firstrecyclerView.setItemAnimator(new DefaultItemAnimator());
for (int i = 0; i < iconName.length; i++) {
itemModel itemModel = new itemModel();
itemModel.setName(iconName[i]);
items.add(itemModel);
}
private void populateRecyclerView() {
CustomAdapter adapterf = new CustomAdapter(this, items);
firstrecyclerView.setAdapter(adapterf);
final YoutubeVideoAdapter adapter = new YoutubeVideoAdapter(this, youtubeVideoArrayList);
firstrecyclerView.setAdapter(adapter);
the output is displaying everything except for the TEXTVIEW. no list is being displayed. I have tried ecery solution on stackoverflow regarding dependencies, class import etc but couldnt find any solution. Please can somebody help me in this as I am not a regular developer but a self learner.
My YoutubeVideoAdapter.java is below
package adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;
import com.CurrentMediaPakLiveNews.R;
import java.util.ArrayList;
import holder.YoutubeViewHolder;
import utils.Constants;
/**
* Created by sonu on 10/11/17.
*/
public class YoutubeVideoAdapter extends RecyclerView.Adapter<YoutubeViewHolder> {
private static final String TAG = YoutubeVideoAdapter.class.getSimpleName();
private Context context;
private ArrayList<String> youtubeVideoModelArrayList;
//position to check which position is selected
private int selectedPosition = 0;
public YoutubeVideoAdapter(Context context, ArrayList<String> youtubeVideoModelArrayList) {
this.context = context;
this.youtubeVideoModelArrayList = youtubeVideoModelArrayList;
}
@Override
public YoutubeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.youtube_video_custom_layout, parent, false);
return new YoutubeViewHolder(view);
}
@Override
public void onBindViewHolder(YoutubeViewHolder holder, final int position) {
//if selected position is equal to that mean view is selected so change the cardview color
if (selectedPosition == position) {
holder.youtubeCardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
} else {
//if selected position is not equal to that mean view is not selected so change the cardview color to white back again
holder.youtubeCardView.setCardBackgroundColor(ContextCompat.getColor(context, android.R.color.white));
}
/* initialize the thumbnail image view , we need to pass Developer Key */
holder.videoThumbnailImageView.initialize(Constants.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
//when initialization is sucess, set the video id to thumbnail to load
youTubeThumbnailLoader.setVideo(youtubeVideoModelArrayList.get(position));
youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
//when thumbnail loaded successfully release the thumbnail loader as we are showing thumbnail in adapter
youTubeThumbnailLoader.release();
}
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
//print or show error when thumbnail load failed
Log.e(TAG, "Youtube Thumbnail Error");
}
});
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
//print or show error when initialization failed
Log.e(TAG, "Youtube Initialization Failure");
}
});
}
@Override
public int getItemCount() {
return youtubeVideoModelArrayList != null ? youtubeVideoModelArrayList.size() : 0;
}
/**
* method the change the selected position when item clicked
* @param selectedPosition
*/
public void setSelectedPosition(int selectedPosition) {
this.selectedPosition = selectedPosition;
//when item selected notify the adapter
notifyDataSetChanged();
}
}
答案1
得分: 1
你正在将不同的适配器应用于同一个RecyclerView,这意味着最后一个可见的适配器将是最终显示的。
你可以在这里看到:
CustomAdapter adapterf = new CustomAdapter(this, items);
firstrecyclerView.setAdapter(adapterf);
final YoutubeVideoAdapter adapter = new YoutubeVideoAdapter(this, youtubeVideoArrayList);
firstrecyclerView.setAdapter(adapter);
如果你注意到,你首先使用了 firstrecyclerView.setAdapter(adapterf)
来附加CustomAdapter。然后,你再次使用 firstrecyclerView.setAdapter(adapter)
来附加YoutubeVideoAdapter。只有最后一个适配器会生效,因为它会覆盖前面的适配器。
编辑:
根据我理解,你想要渲染一个视图,其中显示一个Youtube缩略图和缩略图下方的TextView。在你的CustomAdapter中,你使用了一个itemModel
的列表。考虑向该itemModel
添加一个保存Youtube视频URL的属性。如果你这样做,你可以同时显示缩略图和TextView,你只需要将你的CustomAdapter
修改成与YoutubeVideoAdapter
相同的方式。
例如:
你的itemModel
将如下所示:
public class itemModel {
String name;
String youtubeUrl;
public itemoModel(String name, String youtubeUrl){
this.name = name;
this.youtubeUrl = youtubeUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYoutubeUrl(){
return this.youtubeUrl;
}
public void setYoutubeUrl(String youtubeUrl){
this.youtubeUrl = youtubeUrl;
}
}
以及你的CustomAdapter
:
public class viewHolder extends RecyclerView.ViewHolder {
public TextView iconName;
public YouTubeThumbnailView thumbnail;
public viewHolder(View itemView) {
super(itemView);
this.iconName = itemView.findViewById(R.id.icon_name);
this.thumbnail = itemView.findViewById(R.id.video_thumbnail_image_view);
}
}
@Override
public void onBindViewHolder(viewHolder holder, int position) {
TextView iconName = holder.iconName;
holder.iconName.setText(items.get(position).getName());
YouTubeThumbnailView thumbnail = holder.thumbnail;
String youtubeUrl = items.get(position).getYoutubeUrl();
//Do what you need to do with the youtubeUrl
}
这样,你只需要使用一个适配器和一个RecyclerView,就可以使用你已经在这里编写的代码:
CustomAdapter adapterf = new CustomAdapter(this, items);
firstrecyclerView.setAdapter(adapterf);
如果有帮助,请告诉我!
英文:
You are applying differente adapters to the same recyclerview, which means the last one that will be visible will be the last one.
You can see it here:
CustomAdapter adapterf = new CustomAdapter(this, items);
firstrecyclerView.setAdapter(adapterf);
final YoutubeVideoAdapter adapter = new YoutubeVideoAdapter(this, youtubeVideoArrayList);
firstrecyclerView.setAdapter(adapter);
If you notice, you are doing firstrecyclerView.setAdapter(adapterf)
where you attach the CustomAdapter. Then, you do again firstrecyclerView.setAdapter(adapter)
where you attach the YoutubeVideoAdapter. Only the last adapter will take place, since it overrides the previous one.
EDIT:
As I can understand, you want to render a view where you display an YoutubeThumbnail and a TextView underneath that thumbnail. In your CustomAdapter, you are using a list of itemModel
. Consider adding an attribute to that itemModel
that holds the Youtube video URL. If you do it this way, you can show the thumbnail and the TextView at the same time, you just need to modify your CustomAdapter
to the same as your YoutubeVideoAdapter.
I.e:
Your ItemModel would look like this:
public class itemModel {
String name;
String youtubeUrl;
public itemoModel(String name, String youtubeUrl){
this.name = name;
this.youtubeUrl = youtubeUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYoutubeUrl(){
return this.youtubeUrl;
}
public void setYoutubeUrl(String youtubeUrl){
this.youtubeUrl = youtubeUrl;
}
}
And your CustomAdapter:
public class viewHolder extends RecyclerView.ViewHolder {
public TextView iconName;
public YouTubeThumbnailView thumbnail;
public viewHolder(View itemView) {
super(itemView);
this.iconName = itemView.findViewById(R.id.icon_name);
this.thumbnail = itemView.findViewById(R.id.video_thumbnail_image_view);
}
}
@Override
public void onBindViewHolder(viewHolder holder, int position) {
TextView iconName =holder.iconName;
holder.iconName.setText(items.get(position).getName());
YouTubeThumbnailView thumbnail = holder.thumbnail;
String youtubeUrl = items.get(position).getYoutubeUrl();
//Do what you need to do with the youtubeUrl
}
This way you just need to use one adapter and one recyclerView with the code you've already done here:
CustomAdapter adapterf = new CustomAdapter(this, items);
firstrecyclerView.setAdapter(adapterf);
Let me know if it helped you!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论