在创建CardView之后访问它,并将其放入RecyclerView中

时间:2015-09-10 22:22:10

标签: java android android-recyclerview android-cardview

我正在尝试弄清楚如何在创建ImageView之后修改某个TextViewCardView小部件并将其传递给主RecyclerView。

所以我的代码如下:

首先是包含项目的类:

 public class MovieDetails {
    protected static String title;
    protected static Bitmap imageViewPoster;
    protected static Bitmap imageViewFanart;}

现在我有了RecyclerView适配器,它还拥有ViewHolder内部类,如下所示:

public class MovieDetailsAdapter extends RecyclerView.Adapter<MovieDetailsAdapter.MovieViewHolder> {

       private List<MovieDetails> movieList;

public MovieDetailsAdapter(List<MovieDetails> movietList) {

    this.movieList = movietList;
}

@Override
public int getItemCount() {

    return movieList.size();
}

@Override
public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.card_layout_movies, viewGroup, false);

    return new MovieViewHolder(itemView);
}

@Override
public void onBindViewHolder(MovieViewHolder movieViewHolder, int i) {

    MovieDetails md = movieList.get(i);

    movieViewHolder.vTitle.setText(md.title);      
    movieViewHolder.vPoster.setImageBitmap(md.imageViewPoster);
    movieViewHolder.vFanart.setImageBitmap(md.imageViewFanart);

}


public static class MovieViewHolder extends RecyclerView.ViewHolder {

    protected TextView vTitle;
    protected ImageView vPoster;
    protected ImageView vFanart;

    public MovieViewHolder(View v)
    {
        super(v);
        vTitle = (TextView)v.findViewById(R.id.title);
        vPoster = (ImageView) v.findViewById(R.id.imageViewPoster);
        vFanart = (ImageView) v.findViewById(R.id.imageViewFanart);

    }
}} 

现在是我的主要Activity类,我在这里设置布局和视图,并创建对象的MovieList数组。

public class MoviesListActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_movies_list);
    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    MovieDetailsAdapter ca = new MovieDetailsAdapter(createList(5));
    recList.setAdapter(ca);

}

private List<MovieDetails> createList(int size){

    List<MovieDetails> moviedetailsList =  new ArrayList<MovieDetails>();

    for (int i=1; i <= size; i++) {
        MovieDetails moviedetails = new MovieDetails();
        moviedetails.title= "Name" +i;

        Bitmap bit = resizeBitmap(getResources(), R.drawable.poster_example, 640, 955);
        moviedetails.imageViewPoster = bit;

        Bitmap bit2 = resizeBitmap(getResources(), R.drawable.fanart_example, 800, 450);
        moviedetails.imageViewFanart = bit2;

        moviedetailsList.add(moviedetails);
    }
    return  moviedetailsList;
}

resizeBitmap是另一个用于调整图像大小的函数,与问题无关,我将其编辑出来。

现在,我已经创建了这个CardView列表,有没有办法从主Activity类中修改其中的ImageView和TextViews?我想要这个,因为我使用AsyncTask来检索我想要替换的新图像,所以我首先要用一些“默认”图像初始化Bitmaps,然后再从AsyncTask的结果中修改它们。从服务器检索它们。

搜索谷歌周围,发现了几个关于CardViews的教程,但没有关于此,任何帮助表示赞赏,谢谢。

1 个答案:

答案 0 :(得分:1)

CardView并不关心。它就像一个LinearLayout。它不需要任何不同于标准RecyclerView的处理。

首先,您需要只修改一个List<MovieDetails>,并且只需要修改一个MovieDetailsAdapter即可。 (如果需要,您可以将您的工作保留在临时工作中。)

因此,您应该在适配器的代码中创建列表public static

然后在Activity中,而不是

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    MovieDetailsAdapter ca = new MovieDetailsAdapter(createList(5));
    ...
}

DO

private MovieDetailsAdapter ca;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    ca = new MovieDetailsAdapter(createList(5));
    ...
}

将createList的通用MovieDetails(使用大小)移动到另一个方法(我称之为makeGenericMovieDetails())。 (在createList中,只需在for循环中替换它。)

private MovieDetails makeGenericMovieDetails() {
    MovieDetails moviedetails = new MovieDetails();
    // The size of the list, but add one.
    moviedetails.title= "Name" + (adapter.getSize() + 1);

    Bitmap bit = resizeBitmap(getResources(), R.drawable.poster_example, 640, 955);
    moviedetails.imageViewPoster = bit;

    Bitmap bit2 = resizeBitmap(getResources(), R.drawable.fanart_example, 800, 450);
   moviedetails.imageViewFanart = bit2; 
return moviedetails;
}

现在,当您想要更改它时,请执行此操作(一旦调整大小):

private void setMovieDetails(@Nullable Bitmap poster, @Nullable Bitmap fanart, @Nullable String title, int id) { 
    MovieDetails details;
    boolean isAlreadyThere = true;
    try {
        details = ca.movieList.get(id);
    } catch (IndexOutOfBoundsException e) {
        isAlreadyThere = false;
        // If we come with a bad ID we crash. No fun.
        details = makeGenericMovieDetails();
    }
    if (poster != null)
        details.imageViewPoster = poster;
    if (fanart != null)
        details.imageViewFanart = fanart;
    if (title != null)
        details.Title = title;
    ca.movieList.set(id, details);

    if (isAlreadyThere) {
        ca.notifyItemChanged(id);
    } else {
        ca.notifyItemInserted(id);
    }
}

这里的关键是

ca.notifyItemChanged(id);

告诉适配器更新该项目。

我希望这是有效的,我正在从StackExchange的应用程序中做到这一点并且它一直在丢失草稿,所以先测试。

现在,当您想要更改图片时,只需调用setMovieDetails(poster, fanart, title, id);并为您不想更改的值传递null。 (除了id)。

BTW尝试让变量以小写字母开头。因此,而不是Title执行titlemTitle之类的操作。它看起来更好,更容易区分。