我使用list数组填充我的Recycler视图。我已经尝试过String Array,因为它不适合我。我做错了还是我必须使用arrayadapter?
如果我不得不去阵列适配器我怎么能这样做?我已经使用arrayadapter填充了listview,但由于我处于海洋的中间,我不想尝试一些大的东西。我的recyclerview适配器的代码如下。
它没有显示错误。但视图填充了数组中的最后一项。
public class AudioRecycleViewAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> values = Arrays.asList ("Al Feel","Al Humazah","Al
Kausar","Al Maoon","Al Quresh","Dr Israr",
"Al Kafiroon","An Nasr","Al Lahab","Al Ikhlaas","Al Falaq","An
Naas","Iman Part 1",
"Iman Part 2","Iman Part 3","Iman Part 4","Iman Part 5");
Context context;
InterstitialAd mInterstitialAd;
private int audiolayout;
public TextView txtHeader;
public TextView txtFooter;
TextView txtarabic;
public AudioRecycleViewAdapter(List<String> values, int audiolayout, Context context, InterstitialAd intAdView ) throws IOException {
this.audiolayout = audiolayout;
this.context = context;
this.mInterstitialAd = intAdView;
}
public Context getContext() {
context = this.getContext();
return context;
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public View layout;
public ViewHolder(View v) {
super(v);
layout = v;
txtHeader = (TextView) v.findViewById(R.id.audio_subtitle);
txtFooter = (TextView) v.findViewById(R.id.audio_title);
txtarabic = (TextView) v.findViewById(R.id.arabic_title);
}
}
public void add(int position, String item) {
values.add(position, item);
notifyItemInserted(position);
}
public void remove(int position) {
values.remove(position);
notifyItemRemoved(position);
}
// Create new views (invoked by the layout manager)
@Override
public AudioRecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
LayoutInflater inflater = LayoutInflater.from(
parent.getContext());
View v =
inflater.inflate(R.layout.audio_row_layout, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int x;
for(position =0; position < values.size(); position++)
{
txtHeader.setText(values.get(position));
position++;
}
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return values.size();
}
}
答案 0 :(得分:1)
首先:在AudioRecycleViewAdapter
中使用自定义ViewHolder,因此请更改此
extends RecyclerView.Adapter<RecyclerView.ViewHolder>
到这个
extends RecyclerView.Adapter<AudioRecycleViewAdapter.ViewHolder>
第二:你要浏览所有元素,只是为了在textHeader中显示最后一个值,并使用holder.txtHeader
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.txtHeader.setText(values.get(position));
}
第三:项目布局视图的子视图应该在ViewHolder
以内
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public View layout;
public TextView txtHeader;
public TextView txtFooter;
public TextView txtarabic;
public ViewHolder(View v) {
super(v);
layout = v;
txtHeader = (TextView) v.findViewById(R.id.audio_subtitle);
txtFooter = (TextView) v.findViewById(R.id.audio_title);
txtarabic = (TextView) v.findViewById(R.id.arabic_title);
}
}
答案 1 :(得分:0)
除了Pavneet_Singh的答案之外,您还可以将bind()方法添加到自定义ViewHolder,并在此方法中绑定与RecyclerView项相关的数据。当事情变得更复杂时,我在简单的项目中使用这种方法。它应该像这样工作:
//adapter's inner
class SomeClassViewHolder(val view: View) extends RecyclerView.ViewHolder(view) {
//here can be view initializing, like
txtHeader = (TextView) view.findViewById(R.id.audio_subtitle);
void bind(int position){
// all view binding logic goes here, for example:
txtHeader.setText("someText");
}
}
//and then in adapter
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.bind(values.get(position));
}
修改,如果使用得当,这可以帮助您避免查找holder
绑定属性的问题,例如textView
等。此外,将来您可以使用bind()
方法操作持有者的项目听众。