【问题标题】:Cardview's data are changed while scrolling RecyclerViewCardview 的数据在滚动 RecyclerView 时发生变化
【发布时间】:2016-07-15 09:38:30
【问题描述】:

我是一名刚入门的 android 开发人员。我用 CardView 制作了一个应用程序,但是当我滚动 RecyclerView 时,它正在改变它的内容。)

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

我的代码:

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:

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:

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

【问题讨论】:

    标签: java android scroll android-recyclerview android-cardview


    【解决方案1】:

    从此代码块中删除所有static

        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
    

    并尝试了解静态和非静态字段的区别。我用谷歌搜索发现:http://www.tutorial4us.com/java/java-static-and-non-static-variable

    【讨论】:

      【解决方案2】:

      删除IngredientViewHolder 定义及其字段中的static

      【讨论】:

      • 请在答案中添加更多上下文!
      【解决方案3】:

      从此代码块中删除所有static

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-10
        • 2021-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多