访问在CardView项目已经建立并投入RecyclerView后, [英] Accessing items in CardView after it has been created and put into a RecyclerView

查看:176
本文介绍了访问在CardView项目已经建立并投入RecyclerView后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何修改一定的ImageView 的TextView 部件在 CardView 后,我创造了它,并将其传递到主RecyclerView。

i'm trying to figure out how to modify a certain ImageView or TextView widget in a CardView after i created it and passed it to the main RecyclerView.

所以,我的code是如下:

So my code is as follows :

首先保存的物品类:

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

现在我有RecyclerView适配器还持有ViewHolder内部类是这样的:

Now i have the RecyclerView Adapter that also holds the ViewHolder inner class like this :

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);

    }
}} 

而现在我的主要活动课,我在哪里设置布局和视图创建对象的MovieList阵列。

And now my main Activity class, where i am setting the layout and view and creating the MovieList array of objects.

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是我用来调整图像,无关的问题,另一个函数,我编不出来。

resizeBitmap is another function i use for resizing the images, irrelevant to the question, i edited it out.

现在,一个我已经创建了这个CardView名单,有没有办法从主Activity类修改它里面的ImageView和TextViews?我想这个,因为我真的使用的AsyncTask获取我想要更换新的图像,所以我先用一些默认的图像来初始化位图,再后来之一来修改他们从AsyncTask的结果来从服务器获取它们。

Now, one i've created this CardView list, is there any way to modify the ImageView and TextViews inside it from the main Activity class ? I want to to this since i`m using AsyncTask to retrieve the new images i want to replace, so i first have to initialize the Bitmaps with some "default" images, and then later one modify them from the results of the AsyncTask used to retrieve them from the server.

周围谷歌搜索,发现了几十个教程周围CardViews但没有这方面的任何帮助AP preciated,谢谢。

Searched around google, found dozens of tutorials around CardViews but nothing regarding this, any help is appreciated, thanks.

推荐答案

CardView 不关心。它就像一个的LinearLayout 。它并不需要比标准的不同的治疗 RecyclerView

The CardView does not care. It is just like a LinearLayout. It does not need any different treatment than a standard RecyclerView.

首先,你需要有和修改只有一个列表&LT; MovieDetails&GT; 且只有一个 MovieDetailsAdapter 为这工作。 (你可以把你的工作是暂时的,如果你需要)。

First, you need to have and modify only one List<MovieDetails> and only one MovieDetailsAdapter for this to work. (You can keep your work in a temporary one if you need).

所以,你应该在适配器的code中的列表公共静态

So, you should make the List public static in the code of the adapter.

然后在活动,而不是

@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循环。)

Move the createList's generic MovieDetails (using the size) to another method (I'll call it makeGenericMovieDetails()). (In the createList, just replace this in the for loop.)

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;
}

现在,当你想改变它,这样做(一旦你调整它):

Now when you want to change it, do this (once you resized it):

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);

它告诉适配器更新项目。

which tells the adapter to update that item.

我希望这个作品,我从StackExchange的应用副手这样做的,它不断失去选秀,所以先测试一下。

I hope this works, I am doing this from StackExchange's app off hand and it keeps losing the draft, so test first.

现在,当你要更改的图片,只需拨打 setMovieDetails(海报,同人画,标题,ID); 并传递null值,你不想更改。 (除ID)。

Now, when you want to change the pic, just call setMovieDetails(poster, fanart, title, id); and pass null for a value you do not want to change. (Except id).

BTW尽量使变量以小写字母。因此,而不是标题的做类似标题 mTitle 。它看起来更好,更容易分辨。

BTW try to make variables start with a lowercase letter. So instead of Title do something like title or mTitle. It looks better and makes it easier to distinguish.

这篇关于访问在CardView项目已经建立并投入RecyclerView后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆