android – 只有在向下和向上滚动时,RecyclerView才会刷新项目

我正在使用电影项目进行简单的回收查看.当我尝试添加新项目时,它们不会立即显示在recyclerview中.出于某种原因,我必须向下滚动以便第一个项目消失,然后再次向上显示要显示的新项目(它在索引0处添加).

适配器:

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 bad english,希望有人理解并知道问题可能是什么.
我尝试从runOnUiThread([…])添加,但结果相同.

最佳答案
发生这种情况是因为位置0高于当前可见屏幕.
在实现脚本模式之前,当您将项目添加到位置N时,如果LinearLayoutManager#findFirstCompletelyVisibleItemPosition返回N,请尝试调用scrollToPosition(N).

另外,不要在onBindViewHolder方法中将position参数设为final.相反,如果在回调中需要它,请使用ViewHolder.getPosition().当项目的位置发生变化时,除非项目本身无效,否则您将不会收到onBind调用.

更新

您的更新逻辑也不正确,因为您没有清除视图中的值.

if (!movie.getTagline().isEmpty()) {
    movieViewHolder.tagline.setText(movie.getTagline());
} else if (!movie.getPlot().isEmpty()) {
    movieViewHolder.tagline.setText(movie.getPlot());
} else {
    movieViewHolder.tagline.setText(null);
}

海报网址相同.

转载注明原文:android – 只有在向下和向上滚动时,RecyclerView才会刷新项目 - 代码日志