我是一名刚开始的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);
}
}
答案 0 :(得分:3)
从此代码块中删除所有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
并尝试阅读有关静态和非静态字段的区别。我用Google搜索并发现:http://www.tutorial4us.com/java/java-static-and-non-static-variable
答案 1 :(得分:1)
删除static
定义及其字段
IngredientViewHolder
答案 2 :(得分:1)
从此代码块中删除所有static
:
public static TextView ingredientName;
public static TextView ingredientTeaspoon;
public static TextView ingredientTablespoon;
public static TextView ingredientGlass;
public static ImageView ingredientPhoto;