我的recyclerview损失数据向下滚动

时间:2018-07-14 13:21:12

标签: android android-recyclerview

我有一个recyclerview,并在其中设置了一些textview的文本。当我向下滚动或片段进入onPause状态时,我的数据丢失。 我该怎么办?

import static com.test.mohammaddvi.snappfood.Adapter.SectionListDataAdapter.decodeSampledBitmapFromResource;
public class RecyclerViewMenuFragmentAdapter extends RecyclerView.Adapter<RecyclerViewMenuFragmentAdapter.SingleItemInMenuFragment> {

private ArrayList<Food> foodList;
private Context mContext;

public RecyclerViewMenuFragmentAdapter(ArrayList<Food> foodList, Context mContext) {
    this.foodList = foodList;
    this.mContext = mContext;

}

@NonNull
@Override
public RecyclerViewMenuFragmentAdapter.SingleItemInMenuFragment onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.foodlist, null);
    return new RecyclerViewMenuFragmentAdapter.SingleItemInMenuFragment(v);
}

@Override
public void onBindViewHolder(final RecyclerViewMenuFragmentAdapter.SingleItemInMenuFragment holder, int position) {
    Food food = foodList.get(position);
    holder.foodName.setText(food.getName());
    holder.foodDetails.setText(food.getDetails());
    holder.foodPrice.setText(food.getPrice() + " تومان ");
    holder.foodOrderNumber.setVisibility(View.INVISIBLE);
    holder.foodMinusButton.setVisibility(View.INVISIBLE);
    holder.foodOrderNumber.setText(0 + "");
    holder.foodImage.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), mContext.getResources().getIdentifier(food.getImage(),
            "drawable", mContext.getPackageName()), 50, 50));
    handleClick(holder, position);
}

private void handleClick(final SingleItemInMenuFragment holder, final int position) {
    holder.foodPlusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int orderNumber = Integer.parseInt(holder.foodOrderNumber.getText().toString());
            holder.foodOrderNumber.setText(orderNumber + 1 + "");
            holder.foodOrderNumber.setVisibility(View.VISIBLE);
            holder.foodMinusButton.setVisibility(View.VISIBLE);

        }
    });
    holder.foodMinusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int orderNumber = Integer.parseInt(holder.foodOrderNumber.getText().toString());
            if (orderNumber > 1) {

                holder.foodOrderNumber.setText(orderNumber - 1 + "");
                holder.foodOrderNumber.setVisibility(View.VISIBLE);

            }
            if (orderNumber == 1) {

                holder.foodOrderNumber.setText(orderNumber - 1 + "");
                holder.foodOrderNumber.setVisibility(View.INVISIBLE);
                holder.foodMinusButton.setVisibility(View.INVISIBLE);

            }
        }
    });
}

@Override
public int getItemCount() {
    return (null != foodList ? foodList.size() : 0);
}


public class SingleItemInMenuFragment extends RecyclerView.ViewHolder {
    TextView foodName;
    TextView foodPrice;
    Button foodPlusButton;
    Button foodMinusButton;
    TextView foodOrderNumber;
    ImageView foodImage;
    TextView foodDetails;

    SingleItemInMenuFragment(View itemView) {

        super(itemView);
        this.foodName = itemView.findViewById(R.id.foodName);
        this.foodImage = itemView.findViewById(R.id.imageFood);
        this.foodPrice = itemView.findViewById(R.id.foodPrice);
        this.foodDetails = itemView.findViewById(R.id.foodDetails);
        this.foodPlusButton = itemView.findViewById(R.id.plusbutton);
        this.foodMinusButton = itemView.findViewById(R.id.minusbutton);
        this.foodOrderNumber = itemView.findViewById(R.id.ordernumber);

    }
}

} 这是我在其中使用recyclerview的片段:

public class MenuFragment extends Fragment{
private static final String TAG = "menufragment";
ArrayList<Food> allfoods = new ArrayList<>();
RecyclerView recyclerview;
private static Bundle bundle;
private final String KEY_RECYCLER_STATE= "recycler_state";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.menufragment, container, false);
}

@Override
public void onStart() {
    super.onStart();
    String jsonFilePath = "foods.json";
    recyclerview = getActivity().findViewById(R.id.lstitems);
    RecyclerViewMenuFragmentAdapter adapter = new RecyclerViewMenuFragmentAdapter(allfoods, getContext());
    recyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    recyclerview.setHasFixedSize(true);
    recyclerview.setAdapter(adapter);
    parsJson(jsonFilePath);
}

//this method is for read a local json and return a string
public String readLocalJson(String jsonFile) {
    String json;
    try {
        InputStream is = getActivity().getAssets().open(jsonFile);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return json;
}

public void parsJson(String jsonFilePath) {
    try {
        JSONObject obj = new JSONObject(readLocalJson(jsonFilePath));
        JSONArray jsonArray = obj.getJSONArray("results");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String image = jsonObject.getString("image");
            JSONArray jsonArrayFoot = jsonObject.getJSONArray("foots");
            for (int j = 0; j < jsonArrayFoot.length(); j++) {
                JSONObject jsonObjectFoot = jsonArrayFoot.getJSONObject(j);
                String foodName = jsonObjectFoot.getString("name");
                String fooddetails = jsonObjectFoot.getString("fooddetails");
                String price = jsonObjectFoot.getString("price");
                allfoods.add(new Food(foodName, price, fooddetails, image));
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:1)

基本上,您刚刚初始化了onStart上的数据,该数据将在您的活动/片段恢复时最终调用,因此,您已更改的所有数据都将覆盖为初始数据。

onStart初始化移动到onViewCreated

@Override
public void onViewCreated() {
    super.onViewCreated();
    String jsonFilePath = "foods.json";
    recyclerview = getActivity().findViewById(R.id.lstitems);
    RecyclerViewMenuFragmentAdapter adapter = new RecyclerViewMenuFragmentAdapter(allfoods, getContext());
    recyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    recyclerview.setHasFixedSize(true);
    recyclerview.setAdapter(adapter);
    parsJson(jsonFilePath);
}

对于滚动来说,这是正常现象,因为RecyclerView会从上面的列表中回收视图,但是数据不是,所以您要做的是存储列表源中的值。

@Override
public void onBindViewHolder(final RecyclerViewMenuFragmentAdapter.SingleItemInMenuFragment holder, int position) {
    Food food = foodList.get(position);
    holder.foodName.setText(food.getName());
    holder.foodDetails.setText(food.getDetails());
    holder.foodPrice.setText(food.getPrice() + " تومان ");
    holder.foodOrderNumber.setVisibility(View.INVISIBLE);
    holder.foodMinusButton.setVisibility(View.INVISIBLE);
    holder.foodOrderNumber.setText(food.getFoodOrderNumber());
    holder.foodImage.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), mContext.getResources().getIdentifier(food.getImage(),
            "drawable", mContext.getPackageName()), 50, 50));
    handleClick(holder, position);
}

private void handleClick(final SingleItemInMenuFragment holder, final int position) {
    holder.foodPlusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int orderNumber = Integer.parseInt(holder.foodOrderNumber.getText().toString());
            int newOrderNumber = orderNumber + 1;
            Food food = foodList.get(position);
            food.setFoodOrderNumber(newOrderNumber);
            holder.foodOrderNumber.setText(newOrderNumber + "");
            holder.foodOrderNumber.setVisibility(View.VISIBLE);
            holder.foodMinusButton.setVisibility(View.VISIBLE);

        }
    });
    holder.foodMinusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Food food = foodList.get(position);

            int orderNumber = food.getFoodOrderNumber();
            if (orderNumber > 1) {
                int newOrderNumber = orderNumber - 1;
                food.setFoodOrderNumber(newOrderNumber);
                holder.foodOrderNumber.setText(newOrderNumber + "");
                holder.foodOrderNumber.setVisibility(View.VISIBLE);

            }
            if (orderNumber == 1) {
                int newOrderNumber = orderNumber - 1;
                food.setFoodOrderNumber(newOrderNumber);
                holder.foodOrderNumber.setText(newOrderNumber + "");
                holder.foodOrderNumber.setVisibility(View.INVISIBLE);
                holder.foodMinusButton.setVisibility(View.INVISIBLE);

            }
        }
    });
}

然后在您的Food对象上添加以下字段和函数:

public class Food {
    int foodOrderNumber;

    public int getFoodOrderNumber() {
        return foodOrderNumber;
    }

    public void setFoodOrderNumber(int foodOrderNumber) {
        this.foodOrderNumber = foodOrderNumber;
    }
}

答案 1 :(得分:0)

将此行添加到您的onBindViewHolder方法中,然后再次检查问题是否仍然存在:

 holder.setIsRecyclable(false);