刷新之前,回收者视图不会显示第一项

时间:2018-09-26 16:55:58

标签: android android-recyclerview

我有一个应用程序,它可以在回收站视图中保存一个数字及其记录的日期,但是当数组为空然后尝试保存我的号码时,RecyclerView不会显示该项目。该项目已保存,但未在视图中显示。我知道它可以保存,因为当我返回视图时,该项目在那里。我在网上寻找答案的年龄已久,却没有人遇到同样的问题。预先感谢您的帮助!

活动2:

package com.pinet.count;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Activity2 extends AppCompatActivity {
    private ExampleAdapter mAdapter;
    private static final Date currentTime = Calendar.getInstance().getTime();
    private ArrayList<ExampleItem> mexampleList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        Intent intent = getIntent();
        String dateString= DateFormat.format("dd/MM/yyyy",currentTime).toString();
        int count = intent.getIntExtra(MainActivity.EXTRA_NUMBER,0);

        loadData();
        buildRecyclerView();



        if (count!=0){
            if (mexampleList == null){
                mexampleList = new ArrayList<ExampleItem>();
            }
            mexampleList.add(0, new ExampleItem(String.valueOf(count), dateString));
            mAdapter.notifyItemInserted(0);
            mAdapter.notifyItemRangeChanged(0,mexampleList.size());

        }


        saveList();
    }

    private void buildRecyclerView(){
        RecyclerView mRecyclerView = findViewById(R.id.recyclerview);
        mRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        mAdapter= new ExampleAdapter(mexampleList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
            @Override
            public void onDeleteClick(int position) {
                removeItem(position);



            }
        });
    }
    private void removeItem(int position){
        mexampleList.remove(position);
        mAdapter.notifyItemRemoved(position);
        mAdapter.notifyItemRangeChanged(position, mexampleList.size());
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences",0);
        sharedPreferences.edit().remove("count list").apply();
    }package com.pinet.count;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Activity2 extends AppCompatActivity {
    private ExampleAdapter mAdapter;
    private static final Date currentTime = Calendar.getInstance().getTime();
    private ArrayList<ExampleItem> mexampleList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        Intent intent = getIntent();
        String dateString= DateFormat.format("dd/MM/yyyy",currentTime).toString();
        int count = intent.getIntExtra(MainActivity.EXTRA_NUMBER,0);

        loadData();
        buildRecyclerView();



        if (count!=0){
            if (mexampleList == null){
                mexampleList = new ArrayList<ExampleItem>();
            }
            mexampleList.add(0, new ExampleItem(String.valueOf(count), dateString));
            mAdapter.notifyItemInserted(0);
            mAdapter.notifyItemRangeChanged(0,mexampleList.size());

        }


        saveList();
    }

    private void buildRecyclerView(){
        RecyclerView mRecyclerView = findViewById(R.id.recyclerview);
        mRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        mAdapter= new ExampleAdapter(mexampleList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
            @Override
            public void onDeleteClick(int position) {
                removeItem(position);



            }
        });
    }
    private void removeItem(int position){
        mexampleList.remove(position);
        mAdapter.notifyItemRemoved(position);
        mAdapter.notifyItemRangeChanged(position, mexampleList.size());
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences",0);
        sharedPreferences.edit().remove("count list").apply();
    }
    private void saveList(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(mexampleList);
        editor.putString("count list", json);
        editor.apply();
    }
    private void loadData(){


        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("count list", null);
        Type type = new TypeToken<ArrayList<ExampleItem>>() {}.getType();
        mexampleList = gson.fromJson(json, type);

    }

}
`
    private void saveList(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(mexampleList);
        editor.putString("count list", json);
        editor.apply();
    }
    private void loadData(){


        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("count list", null);
        Type type = new TypeToken<ArrayList<ExampleItem>>() {}.getType();
        mexampleList = gson.fromJson(json, type);

    }

}

主要活动:

package com.pinet.count;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private int count = 0;

    public static final String EXTRA_NUMBER ="com.pinet.count.EXTRA_NUMBER";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView= findViewById(R.id.textView);
        Button buttonPos = findViewById(R.id.button);
        Button buttonNeg = findViewById(R.id.button2);
        Button buttonReset = findViewById(R.id.button3);
        Button buttonSave = findViewById(R.id.button4);
        Button butonHistory = findViewById(R.id.button5);

        buttonPos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                count +=1;
                textView.setText(String.valueOf(count));
            }
        });
        buttonNeg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                count-=1;
                textView.setText(String.valueOf(count));
            }
        });
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                count = 0;
                textView.setText(String.valueOf(count));
            }
        });
        butonHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OpenActivity2();
            }
        });
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openActivity2();
            }
        });
    }
    private void openActivity2(){
        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra(EXTRA_NUMBER, count);
        startActivity(intent);
    }
    private void OpenActivity2(){
        Intent intent = new Intent(this, Activity2.class);
        startActivity(intent);
    }
}

ExampleAdapter:

package com.pinet.count;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.ArrayList;

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {

    private final ArrayList<ExampleItem> mExampleList;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onDeleteClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }
    public static class ExampleViewHolder extends RecyclerView.ViewHolder {

        final TextView mTextView1;
        final TextView mTextView2;
        final ImageView mDeleteImage;

        ExampleViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
            super(itemView);
            mTextView1=itemView.findViewById(R.id.textView);
            mTextView2=itemView.findViewById(R.id.textView2);
            mDeleteImage=itemView.findViewById(R.id.image_delete);
            mDeleteImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener  !=null){
                        int position = getAdapterPosition();
                        if (position!=RecyclerView.NO_POSITION){
                            listener.onDeleteClick(position);

                        }
                    }
                }
            });
        }

    }

    public ExampleAdapter(ArrayList<ExampleItem> exampleList){
        mExampleList = exampleList;
    }

    @NonNull
    @Override
    public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
        return new ExampleViewHolder(v, mListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
        ExampleItem currentItem = mExampleList.get(position);
        holder.mTextView1.setText(currentItem.getmText1());
        holder.mTextView2.setText(currentItem.getmText2());
    }


    @Override
    public int getItemCount() {
        if (mExampleList !=null) {
            return mExampleList.size();
        }
        else {
            return 0;
        }
    }

}

ExampleItem:

package com.pinet.count;

class ExampleItem {
    private final String mText1;
    private final String mText2;
    public ExampleItem(String text1, String text2){
        mText1= text1;
        mText2= text2;
    }
    public String getmText1(){
        return mText1;
    }
    public String getmText2(){
        return mText2;
    }
}

如果您还可以帮助我弄清楚动画制作回收者的观点,将不胜感激!

PiNet

1 个答案:

答案 0 :(得分:0)

在“ Activity2” onCreate()中更改您的Activity方法,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);

    String dateString = DateFormat.format("dd/MM/yyyy",currentTime).toString();

    Intent intent = getIntent();
    int count = intent.getIntExtra(MainActivity.EXTRA_NUMBER,0);

    loadData();
    if (count!=0){
        if (mexampleList == null){
            mexampleList = new ArrayList<ExampleItem>();
        }
        String value = String.valueOf(count);
        ExampleItem exampleItem = new ExampleItem(value, dateString);
        mexampleList.add(0, exampleItem);
    }
    buildRecyclerView();
    saveList();
}

我更改了将新数据添加到ArrayList的顺序。这样,在设置适配器之前,“新项目”将被添加到列表中。如果代码分成不同的代码行,我也将其推断出来。我建议这样做,因为它更容易调试。


一些其他评论:

方法removeItem(int position)似乎是从SharedPreferences中删除整个列表,而不是我假设您只想删除其中一项的方法。

在“ MainActivity”中,您还有两种方法:

openActivity2()OpenActivity2()

首先,Java命名约定规定方法名称以小写字母开头。其次,这是一种非常糟糕的编程风格,无法为方法赋予难以区分的方法名称。