ArrayList包含20个项目,我想显示从arraylist到Recycler视图的内容,有20个视图,我想添加一个页脚视图。但我得到了19个观点和页脚。这是Recycler View的Adapter类的代码。如果我增加了getItemCount的大小,我在BindViewHolder方法中得到indexoutofBound异常。请解决。
Result movie;
UrlConstants urlConstants = UrlConstants.getSingletonRef();
private ArrayList<Result> moviesList = new ArrayList<>();
private Context context;
private final int VIEW_ITEM = 0;
private final int VIEW_PROG = 1;
Fragment fragment;
public MoviesAdapter(ArrayList<Result> moviesList,Fragment fragment) {
this.moviesList = moviesList;
this.fragment = fragment;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView backdrop_image;
public TextView title, date;
public MyViewHolder(View view) {
super(view);
backdrop_image = (ImageView)view.findViewById(R.id.backdrop_image);
title = (TextView) view.findViewById(R.id.title);
date = (TextView) view.findViewById(R.id.date);
}
}
public class FooterViewHolder extends RecyclerView.ViewHolder
{
public TextView tag_text,previous_page,next_page,between_text;
public FooterViewHolder(View itemView) {
super(itemView);
between_text = (TextView) itemView.findViewById(R.id.between_text);
tag_text = (TextView)itemView.findViewById(R.id.tag_text);
previous_page = (TextView)itemView.findViewById(R.id.previous_page);
next_page = (TextView) itemView.findViewById(R.id.next_page);
}
}
@Override
public int getItemViewType(int position) {
if (isPositionItem(position))
return VIEW_ITEM;
return VIEW_PROG;
}
private boolean isPositionItem(int position) {
return position != getItemCount() -1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(v);
} else if (viewType == VIEW_PROG){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.footer_layout, parent, false);
return new FooterViewHolder(v);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder)
{
movie = moviesList.get(position);
((MyViewHolder)holder).title.setText(movie.getTitle());
((MyViewHolder)holder).date.setText(movie.getReleaseDate());
String url = urlConstants.URL_Image + movie.getBackdropPath();
Picasso.with(context)
.load(url)
.into(((MyViewHolder)holder).backdrop_image);
}
else if (holder instanceof FooterViewHolder)
{
movie = moviesList.get(position);
((FooterViewHolder)holder).tag_text.setText("Viewing " + movie.getPage() + " of " + movie.getTotal_pages() + " Pages" );
((FooterViewHolder)holder).previous_page.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
((MoviesMainFragment)fragment).prepareOnlineData(movie.getPage() - 1);
}
});
((FooterViewHolder)holder).next_page.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MoviesMainFragment)fragment).prepareOnlineData(movie.getPage() + 1);
}
});
}
}
@Override
public int getItemCount()
{
return this.moviesList.size();
}
答案 0 :(得分:3)
您必须将movilist大小增加1.为此,您必须将虚拟数据插入列表的最后一项,以便最后一项不具有空数据,并且您不会获得Indexoutofbound。
无需增加大小getItemCount,您已经在列表中添加了虚拟项目。
答案 1 :(得分:1)
<强>问题强>
这一行
return position != getItemCount() -1;
你有20个项目,即item0..item19。以上仅检查19项,即最多项目18。当回收者视图到达最后一个位置时,根据条件,它将使页脚视图膨胀,从而忽略最后一个项目。
<强>解决方案强>
下面的代码适用于我。
更改您的isPositionItem()
方法,如下所示
private boolean isPositionItem(int position) {
return position == moviesList.size();
}
更改您的getItemCount()
,如下所示
@Override
public int getItemCount()
{
return this.moviesList.size()+1;
}