数据正在三个不同的适配器中被覆盖

时间:2018-07-19 08:04:07

标签: java android android-recyclerview

我正在尝试将三个不同的数据放入具有相同适配器的三个recyclerView中,并且仅使用一个modelClass,因为放入其中的数据属于同一类型,但是数据被覆盖并且仅显示最后一个数据。我正在使用Picasso库下载并显示图像
这是我的mainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.rcyclvw);
    recyclerView2 = (RecyclerView) findViewById(R.id.rcyclvw2);
    recyclerView3 = (RecyclerView) findViewById(R.id.rcyclvw3);

    mAdapter = new MoviesAdapter(this,movieList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL, false);
    RecyclerView.LayoutManager nLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
    RecyclerView.LayoutManager oLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);

    nAdapter = new MoviesAdapter(this,batDetails);
    recyclerView2.setLayoutManager(nLayoutManager);
    recyclerView2.setItemAnimator(new DefaultItemAnimator());
    recyclerView2.setAdapter(nAdapter);

    oAdapter = new MoviesAdapter(this,theWoman);
    recyclerView3.setLayoutManager(oLayoutManager);
    recyclerView3.setItemAnimator(new DefaultItemAnimator());
    recyclerView3.setAdapter(oAdapter);

    prepareMovieData();
}

private void prepareMovieData() {
    ModelClass model = new ModelClass();
    model.setImg("http://www.twothirdsoftheplanet.com/wp-content/uploads/2015/11/iron-man-e1447971107543.png");
    model.setGenre("I love Ironman");
    model.setTitle("Ironman 1");
    movieList.add(model);


    ModelClass model2 = new ModelClass();
    model2.setImg("https://vignette.wikia.nocookie.net/marvelcinematicuniverse/images/f/fb/Marvel-avengers-infinity-war-iron-man-sixth-scale-figure-hot-toys-silo-903421.png/revision/latest?cb=20180318221316");
    model2.setGenre("Shuri is better than Ironman only in technology.");
    model2.setTitle("Ironman 2");
    movieList.add(model2);

    ModelClass model3 = new ModelClass();
    model3.setImg("https://i.pinimg.com/originals/4e/8c/ca/4e8cca10fd6b2bd509090078cdb0a19c.png");
    model3.setGenre("Everyone loves Ironman.");
    model3.setTitle("Ironman 3");
    movieList.add(model3);

    ModelClass model4 = new ModelClass();
    model4.setImg("https://images-na.ssl-images-amazon.com/images/I/91qvAndeVYL._RI_.jpg");
    model4.setGenre("Who the hell hates Ironman.");
    model4.setTitle("Ironman 4");
    movieList.add(model4);

    ModelClass model5 = new ModelClass();
    model5.setImg("https://images-na.ssl-images-amazon.com/images/I/51oRuNf5hpL._SY355_.jpg");
    model5.setGenre("Avengers: The End Game,is going to be an awesome movie.");
    model5.setTitle("Ironman 5");
    movieList.add(model5);

    ModelClass model1 = new ModelClass();
    model1.setImg("https://www.sideshowtoy.com/assets/products/903341-iron-man-mark-iv/lg/marvel-iron-man-2-iron-man-mark-4-sixth-scale-figure-hot-toys-903340-08.jpg");
    model1.setGenre("Ironman is awesome.");
    model1.setTitle("Ironman 6");
    movieList.add(model1);

    ModelClass bat1 = new ModelClass();
    bat1.setImg("https://cdn.pixabay.com/photo/2014/04/03/11/51/batman-312342_960_720.png");
    bat1.setTitle("Batman 1");
    bat1.setGenre("It Is Black");
    batDetails.add(bat1);

    ModelClass bat2 = new ModelClass();
    bat2.setImg("https://media1.popsugar-assets.com/files/thumbor/0HCrBI6oAjeS76tdNN_79aOCUN4/fit-in/1024x1024/filters:format_auto-!!-:strip_icc-!!-/2012/07/29/2/192/1922153/eccf49d595e40bcb_Anne-Hathaway/i/Anne-Hathaway.jpg");
    bat2.setTitle("Batman 2");
    bat2.setGenre("It Is Black");
    batDetails.add(bat2);

    ModelClass bat3 = new ModelClass();
    bat3.setImg("https://www.sideshowtoy.com/wp-content/uploads/2018/03/dc-comics-batman-premium-format-figure-sideshow-feature-300542-1.jpg");
    bat3.setTitle("Batman 3");
    bat3.setGenre("It Is Black");
    batDetails.add(bat3);

    ModelClass bat4 = new ModelClass();
    bat4.setImg("https://www.sideshowtoy.com/wp-content/uploads/2017/11/dc-comics-justice-league-batman-statue-prime1-studio-feature-903246-1.jpg");
    bat4.setTitle("Batman 4");
    bat4.setGenre("It Is Black");
    batDetails.add(bat4);

    ModelClass woman = new ModelClass();
    woman.setGenre("Lorem Ipsum");
    woman.setTitle("Wondr Woman 1");
    woman.setImg("https://www.dccomics.com/sites/default/files/Movies-Gallery_JusticeLeague2017_WW_59f8be1fcea248.19685240.jpg");
    theWoman.add(woman);

    ModelClass woman2 = new ModelClass();
    woman2.setGenre("Lorem Ipsum");
    woman2.setTitle("Wondr Woman 1");
    woman2.setImg("https://assets.change.org/photos/7/wk/bh/pUWkBhduwOtRAVV-800x450-noPad.jpg?1509858171");
    theWoman.add(woman2);

    ModelClass woman3 = new ModelClass();
    woman3.setGenre("Lorem Ipsum");
    woman3.setTitle("Wondr Woman 1");
    woman3.setImg("https://images-na.ssl-images-amazon.com/images/I/91I2JspDFLL._RI_.jpg");
    theWoman.add(woman3);

    ModelClass woman4 = new ModelClass();
    woman4.setGenre("Lorem Ipsum");
    woman4.setTitle("Wondr Woman 1");
    woman4.setImg("https://nerdist.com/wp-content/uploads/2018/01/wonder-woman-pictures-3840x2160.jpg");
    theWoman.add(woman4);

    mAdapter.notifyDataSetChanged();
    nAdapter.notifyDataSetChanged();
    oAdapter.notifyDataSetChanged();
}

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {


    Context context;
    ArrayList<ModelClass> listtr;
    public MoviesAdapter(Context context,ArrayList<ModelClass> listtr) {
        this.context = context;
        this.listtr=listtr;

    }

    @Override
    public MoviesAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.movie_row_list, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MoviesAdapter.MyViewHolder holder, int position) {
        ModelClass movie = movieList.get(position);
       ModelClass bat =batDetails.get(position);
        ModelClass womn=theWoman.get(position);
        holder.titl.setText(movie.getTitle());
        holder.titl.setText(bat.getTitle());
        holder.titl.setText(womn.getTitle());
        holder.genr.setText(movie.getGenre());
        holder.genr.setText(bat.getGenre());
        holder.genr.setText(womn.getGenre());
        //holder.year.setText(movie.getYear());
        // holder.imgvw.setImageResource(Picasso.with(context).load(moviesList.get(position).getImg()));
        Picasso.with(context).load(movie.getImg()).into(holder.imgvw);
       Picasso.with(context).load(bat.getImg()).into(holder.imgvw);
        Picasso.with(context).load(womn.getImg()).into(holder.imgvw);



    }

    @Override
    public int getItemCount() {
        return listtr.size();


    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView imgvw;
        private TextView titl, genr;


        public MyViewHolder(View view) {
            super(view);
            titl = (TextView) view.findViewById(R.id.heading);
            genr = (TextView) view.findViewById(R.id.description);
            imgvw = (ImageView) view.findViewById(R.id.imgview);
        }
    }

这是我的modelClass.java

public class ModelClass {
private String img, Title, genre;

public ModelClass() {

}

public ModelClass(String img, String title, String genre) {

    this.img = img;
    Title = title;
    this.genre = genre;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
}

public String getTitle() {
    return Title;
}

public void setTitle(String title) {
    Title = title;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}
}

}

这是我的movie_row_list.xml

<?xml version="1.0" encoding="utf-8"?>
 <xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="225dp"
    android:layout_height="150dp"
    android:layout_margin="8dp"
    android:layout_marginRight="24dp"
    app:cardCornerRadius="6dp"
    app:cardElevation="8dp">

    <RelativeLayout
        android:layout_width="225dp"
        android:layout_height="150dp">

        <TextView
            android:id="@+id/settext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:text="Lorem Ipsum"
            android:textColor="#000"
            android:textSize="12sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/imgview"
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_below="@+id/settext" />

        <TextView
            android:id="@+id/heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"

            android:layout_marginTop="8dp"
            android:layout_toRightOf="@id/imgview"
            android:text=""
            android:textSize="24sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/heading"
            android:layout_margin="8dp"
            android:layout_toRightOf="@id/imgview"
            android:text=""
            android:textSize="18sp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

感谢Adavance。

1 个答案:

答案 0 :(得分:0)

无需为每个recylerviews设置值。使其变得常见。

更改

   @Override
public void onBindViewHolder(MoviesAdapter.MyViewHolder holder, int position) {
    ModelClass movie = movieList.get(position);
   ModelClass bat =batDetails.get(position);
    ModelClass womn=theWoman.get(position);
    holder.titl.setText(movie.getTitle());
    holder.titl.setText(bat.getTitle());
    holder.titl.setText(womn.getTitle());
    holder.genr.setText(movie.getGenre());
    holder.genr.setText(bat.getGenre());
    holder.genr.setText(womn.getGenre());
    //holder.year.setText(movie.getYear());
    // holder.imgvw.setImageResource(Picasso.with(context).load(moviesList.get(position).getImg()));
    Picasso.with(context).load(movie.getImg()).into(holder.imgvw);
   Picasso.with(context).load(bat.getImg()).into(holder.imgvw);
    Picasso.with(context).load(womn.getImg()).into(holder.imgvw);
}

收件人

 @Override
public void onBindViewHolder(MoviesAdapter.MyViewHolder holder, int position) {
    ModelClass model = listtr.get(position);

    holder.titl.setText(model.getTitle());     
    holder.genr.setText(model.getGenre());
    //holder.year.setText(model.getYear());
    // holder.imgvw.setImageResource(Picasso.with(context).load(moviesList.get(position).getImg()));
    Picasso.with(context).load(model.getImg()).into(holder.imgvw);   

}

希望有帮助。