滚动RecyclerView时,Cardview的数据已更改 [英] Cardview's data are changed while scrolling RecyclerView

查看:123
本文介绍了滚动RecyclerView时,Cardview的数据已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名开始的android开发人员.我已经用CardView制作了一个应用程序,但是当我滚动RecyclerView时,它正在更改其内容.)

I'm a beginning android developer. I have made an app with CardView, but when I scrolling RecyclerView, it's changing its content.)

视频: http://sendvid.com/60ui8cay

我的代码:

RecyclerViewActivity.java:

RecyclerViewActivity.java:

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {

  private List<Ingredient> ingredients = new ArrayList();
  private RecyclerView rv;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.recyclerview_activity);

    rv=(RecyclerView)findViewById(R.id.rv);

    LinearLayoutManager llm = new LinearLayoutManager(this);
    rv.setLayoutManager(llm);
    rv.setHasFixedSize(true);
    initializeAdapter();

    initializeData();

  }

  private void initializeData(){
    ingredients.add(new Ingredient("Арахис очищенный", "8 грамм в чайной ложке", "23,3 грамма в столовой ложке", "175 грамм в стакане (250 мл)", R.drawable.peanut));
    ingredients.add(new Ingredient("Брусника", "", "", "140 грамм в стакане (250 мл)", R.drawable.cranberry));
    ingredients.add(new Ingredient("Варенье", "19 грамм в чайной ложке", "46,7 грамм в столовой ложке", "330 грамм в стакане (250 мл)", R.drawable.jam));
}

  private void initializeAdapter(){
    RVAdapter adapter = new RVAdapter(ingredients);
    rv.setAdapter(adapter);
  }
}

RVAdapter.java:

RVAdapter.java:

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.IngredientViewHolder> {

    public static class IngredientViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        public static TextView ingredientName;
        public static TextView ingredientTeaspoon;
        public static TextView ingredientTablespoon;
        public static TextView ingredientGlass;
        public static ImageView ingredientPhoto;

        IngredientViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            ingredientName = (TextView)itemView.findViewById(R.id.ingredient_name);
            ingredientTeaspoon = (TextView)itemView.findViewById(R.id.ingredient_teaspoon);
            ingredientTablespoon = (TextView)itemView.findViewById(R.id.ingredient_tablespoon);
            ingredientGlass = (TextView)itemView.findViewById(R.id.ingredient_glass);
            ingredientPhoto = (ImageView)itemView.findViewById(R.id.ingredient_photo);
        }
    }

    List<Ingredient> ingredients;

    RVAdapter(List<Ingredient> ingredients){
        this.ingredients = ingredients;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {

        super.onAttachedToRecyclerView(recyclerView);
    }
    @Override
    public IngredientViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        IngredientViewHolder ivh = new IngredientViewHolder(v);
        return ivh;
    }

    @Override
    public void onBindViewHolder(IngredientViewHolder ingredientViewHolder, int i) {
        IngredientViewHolder.ingredientName.setText(ingredients.get(i).name);
        IngredientViewHolder.ingredientTablespoon.setText(ingredients.get(i).tablespoon);
        IngredientViewHolder.ingredientTeaspoon.setText(ingredients.get(i).teaspoon);
        IngredientViewHolder.ingredientGlass.setText(ingredients.get(i).glass);
        IngredientViewHolder.ingredientPhoto.setImageResource(ingredients.get(i).photoId);
    }

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

CardViewActivity.java:

CardViewActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class CardViewActivity extends Activity {

    TextView ingredientName;
    TextView ingredientTeaspoon;
    TextView ingredientTablespoon;
    TextView ingredientGlass;
    ImageView ingredientPhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.cardview_activity);
        ingredientName = (TextView)findViewById(R.id.ingredient_name);
        ingredientTeaspoon = (TextView)findViewById(R.id.ingredient_teaspoon);
        ingredientTablespoon = (TextView)findViewById(R.id.ingredient_tablespoon);
        ingredientGlass = (TextView)findViewById(R.id.ingredient_glass);
        ingredientPhoto = (ImageView)findViewById(R.id.ingredient_photo);

        ingredientPhoto.setImageResource(R.drawable.salt);
    }
}

推荐答案

从此代码块中删除所有static:

remove all static from this block of code:

    public static TextView ingredientName;
    public static TextView ingredientTeaspoon;
    public static TextView ingredientTablespoon;
    public static TextView ingredientGlass;
    public static ImageView ingredientPhoto;

,然后在onBindViewHolder期间更改为:

    // with lower case `i`
    ingredientViewHolder.ingredientName.setTe ... etc

,并尝试阅读有关静态字段和非静态字段的区别.我在Google上搜索后发现: http://www.tutorial4us.com/java/java-static-and-non-static-variable

and try to have a read about the difference of static and non-static fields. I Googled and found that: http://www.tutorial4us.com/java/java-static-and-non-static-variable

这篇关于滚动RecyclerView时,Cardview的数据已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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