如何在不使用动画的情况下重新启动活动,同时保持回收器视图适配器内的滚动位置。 我的主要活动java code`
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
ArrayList<MyData> data= new ArrayList<MyData>();
for (int i=0;i<10;i++){
data.add(new MyData(
"Title"+i,
"description"+i,
R.drawable.ic_favorite_black_24dp
));
}
// specify an adapter (see also next example)
mAdapter = new MyAdapter(data);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
myData代码:
public class MyData {
String title;
String description;
int image;
public MyData(String title, String description, int image) {
this.title = title;
this.description = description;
this.image = image;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public int getImage() {
return image;
}
}
我的适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<MyData> data;
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public View view;
public ViewHolder(View v) {
super(v);
view = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<MyData> data) {
this.data = data;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_row, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
TextView title = (TextView) holder.view.findViewById(R.id.mainText);
TextView description = (TextView) holder.view.findViewById(R.id.descriptionText);
ImageView image= (ImageView) holder.view.findViewById(R.id.icon);
title.setText(data.get(position).getTitle());
description.setText(data.get(position).getDescription());
image.setImageResource(data.get(position).getImage());
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return data.size();
}
}