我正在製作一個帶有Movie項目的簡單recyclerview。當我嘗試添加新項目時,他們將不會立即顯示在回收站中。出於某種原因,我必須向下滾動,以便第一項消失,然後再次顯示新項目(它被添加到索引0處)。RecyclerView僅在向下滾動時才刷新項目
適配器:
public class MovieCardAdapter extends RecyclerView.Adapter<MovieCardAdapter.MovieViewHolder> {
List<Movie> movieList;
public MovieCardAdapter(final List<Movie> movieList) {
this.movieList = movieList;
}
public void addItem(int position, final Movie movie){
movieList.add(position,movie);
notifyItemInserted(position);
}
public void removeItem(final Movie movie){
final int indexOf = movieList.indexOf(movie);
movieList.remove(movie);
notifyItemRemoved(indexOf);
}
public void updateItems(final List<Movie> items){
movieList = items;
notifyDataSetChanged();
}
@Override
public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.movie_row, viewGroup, false);
return new MovieViewHolder(itemView);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public void onBindViewHolder(final MovieViewHolder movieViewHolder, final int i) {
final Movie movie = movieList.get(i);
String posterUrl = "";
final int posterHeight = 240;
if (!isEmpty(movie.getImdb_id())) {
posterUrl = "http://img.omdbapi.com/?i=" + movie.getImdb_id() + "&apikey="+apiKey+"&h=" + posterHeight;
} else if (!isEmpty(movie.getPoster())) {
posterUrl = movie.getPoster();
}
if (!isEmpty(posterUrl)) {
Picasso.with(movieViewHolder.context)
.load(posterUrl)
.resize(220, 354)
.centerCrop()
.into(movieViewHolder.poster);
}
movieViewHolder.title.setText(movie.getTittel());
movieViewHolder.format.setText(movie.getFormat());
if (movie.getRuntime().isEmpty() || movie.getRuntime().equalsIgnoreCase("0")){
movieViewHolder.runtime.setText("");
}else{
movieViewHolder.runtime.setText(movie.getRuntime()+" mins");
}
if (!movie.getTagline().isEmpty()) {
movieViewHolder.tagline.setText(movie.getTagline());
} else if (!movie.getPlot().isEmpty()) {
movieViewHolder.tagline.setText(movie.getPlot());
}
}
@Override
public int getItemCount() {
return movieList.size();
}
public static class MovieViewHolder extends RecyclerView.ViewHolder {
protected Context context;
protected LinearLayout movieRowLinearLayout;
protected ImageView poster;
protected TextView title;
protected TextView runtime;
protected TextView tagline;
protected TextView format;
public MovieViewHolder(View v) {
super(v);
context = v.getContext();
movieRowLinearLayout = (LinearLayout) v.findViewById(R.id.movieRowLinearLayout);
poster = (ImageView) v.findViewById(R.id.poster);
title = (TextView) v.findViewById(R.id.title);
runtime = (TextView) v.findViewById(R.id.runtime);
tagline = (TextView) v.findViewById(R.id.tagline);
format = (TextView) v.findViewById(R.id.format);
}
}
}
初始化適配器:
adapter = new MovieCardAdapter(MovieList.movies);
recyclerView.setAdapter(adapter);
添加樣本影片:
if (id == R.id.add_movie) {
final Movie movie = new Movie();
movie.setTittel("0TEST");
movie.setFormat("HD-DVD");
MovieList.sortMoviesByTitle();
adapter.addItem(MovieList.movies.indexOf(movie), movie);
return true;
} else if (id == R.id.action_logOut) {
logOut(this);
}
Sry基因的英語不好,希望有人理解並知道wha問題可能是。 我已經嘗試從runOnUiThread([...])添加,但結果相同。
試了一下,但增加的項目是不是「上面」的recyclerView的。首先,當第一個項目被「回收」時,它就會變得可見。我嘗試過調試,並發現該項目已添加到適配器中的列表中。 – 2015-04-05 17:51:52
嗯,更新了答案w /在代碼中還有一個問題。 如果問題仍然存在,請隨時向我發送一個應用程序,或使用示例應用程序在b.android.com上創建一個錯誤報告。 – yigit 2015-04-05 18:00:01
我有項目打開https://github.com/olavbg/MyMoovs 但它的工作進行中,許多事情可以/應該會更好。 – 2015-04-05 18:55:07