返回到活动时不要刷新RecyclerView

时间:2018-08-06 14:25:19

标签: android android-recyclerview

我已经实现了一个应用程序,当我单击项目时,我正在使用RecyclerView,当我单击返回recyclerView时,它会转到另一个活动以显示该项目的详细信息。我要使后退按钮回到recylerView中的相同位置 请帮助我进行了很多搜索,但是我没有找到任何东西 我的代码是:

public class Optimism extends AppCompatActivity {
    RecyclerView recyclerView;
    FirebaseDatabase mFirebaseDatabase;
    DatabaseReference mdatabaseReference;
    LinearLayoutManager linearLayoutManager;
    Parcelable mListState;
    ArrayList<items>ar;

    ImageView saveImage;

    public static Optimism getOpt() {
        return opt;
    }

    static Optimism opt;

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

        opt = this;
        final ProgressBar progressBar = findViewById(R.id.progressBar);
        Drawable d = getResources().getDrawable(R.drawable.progress);
        progressBar.setProgressDrawable(d);
        //back botton
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        //reycyclerView
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true);
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        //get type
        String type = getIntent().getStringExtra("Type");
        //ActionBar
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(type);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);


        //firebase
        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mdatabaseReference = mFirebaseDatabase.getReference(type);
        mdatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    progressBar.setVisibility(View.GONE);
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(Optimism.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();

            }
        });
        mdatabaseReference.keepSynced(true);


    }

    @Override
    protected void onStart() {
        super.onStart();
        final FirebaseRecyclerAdapter<items, RecycleViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<items, RecycleViewHolder>(
                items.class,
                R.layout.card_view_design,
                RecycleViewHolder.class
                , mdatabaseReference) {
            @Override
            protected void populateViewHolder(RecycleViewHolder viewHolder, items model, int position) {

                viewHolder.setDetails(getApplicationContext(), model.getImage(), model.getTitle());

            }

            @Override
            public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                final RecycleViewHolder recycleViewHolder = super.onCreateViewHolder(parent, viewType);

                recycleViewHolder.setOnClickListener(new RecycleViewHolder.ClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        ImageView imageView = view.findViewById(R.id.img);
                        TextView title = view.findViewById(R.id.title);

                        Drawable drawable = imageView.getDrawable();
                        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                        Intent intent = new Intent(Optimism.this, PostDetails.class);
                        BitmapHelper.getInstance().setBitmap(bitmap);
                        intent.putExtra("title", title.getText().toString());
                        startActivity(intent);
                    }


                });
                return recycleViewHolder;
            }
        };

        recyclerView.setAdapter(firebaseRecyclerAdapter);


    }

    //back botton
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) ;
        finish();
        return super.onOptionsItemSelected(item);

    }

}

2 个答案:

答案 0 :(得分:1)

当您从Activity1转到Activity2时,将所选位置存储在一个变量中,而当您从Activity2返回到Activity 1时,常见的调用方法是start()

在该方法中保留此代码

recyclerView.getLayoutManager()。scrollToPosition(position)

答案 1 :(得分:0)

正如您在“活动生命周期”所附图片中所见,重新进入活动时,将调用onStart

Android Activity Lifecycle

因此,您应该将所有代码移至onStart onCreate,在您的用例中被调用一次。

您那里有一个代码,可以刷新RecyclerView,例如recyclerView.setAdapter

参考:https://developer.android.com/guide/components/activities/activity-lifecycle