滚动RecyclerView时,Cardview的数据被更改

问题描述 投票:0回答:3

我是一名开始的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
3个回答
3
投票
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


1
投票

1
投票
public static TextView ingredientName; public static TextView ingredientTeaspoon; public static TextView ingredientTablespoon; public static TextView ingredientGlass; public static ImageView ingredientPhoto;
© www.soinside.com 2019 - 2024. All rights reserved.