У меня есть recyclerview
, что я могу изменить положение его элементов, перетаскивая все. Работает нормально.
Но после того, как я выключаю приложение, элементы возвращаются к тому, что было до того, как я могу сохранить новую позицию. Я думаю, SharedPreferences
будет хорошим решением, но я не знаю, как его использовать.
Как я могу это сделать.
Мой код здесь ..
public class MyItemTouchHelper extends ItemTouchHelper.Callback {
private final ItemTouchHelpAdapter mAdapter;
public MyItemTouchHelper(ItemTouchHelpAdapter mAdapter) {
this.mAdapter = mAdapter;
}
@Override
public boolean isLongPressDragEnabled() {
return false;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setBackgroundColor(
ContextCompat.getColor(viewHolder.itemView.getContext(),R.color.cardview_shadow_end_color)
);
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState==ItemTouchHelper.ACTION_STATE_DRAG){
viewHolder.itemView.setBackgroundColor(
Color.LTGRAY
);
}
}
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags,swipeFlags);
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemSwipe(viewHolder.getAdapterPosition());
}
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements
ItemTouchHelpAdapter{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mgamename = new ArrayList<>();
private static ItemTouchHelper mTouchHelper;
private Context mcontext;
public RecyclerViewAdapter(ArrayList<String> mgamename, Context mcontext) {
this.mgamename = mgamename;
this.mcontext = mcontext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
Log.d(TAG,"onBindViewHolder: called.");
holder.gamename.setText(mgamename.get(position));
holder.gamename.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
if (position==0){
Intent intent1 = new Intent(view.getContext(),cc.class);
mcontext.startActivity(intent1);}
if (position==1){
Intent intent1 = new Intent(view.getContext(),tc.class);
mcontext.startActivity(intent1);
}
if (position==2){
Intent intent1 = new Intent(view.getContext(),k.class);
mcontext.startActivity(intent1);
}
if (position==3){
Intent intent1 = new Intent(view.getContext(),ccp.class);
mcontext.startActivity(intent1);
}
if (position==4){
Intent intent1 = new Intent(view.getContext(),tcp.class);
mcontext.startActivity(intent1);
}
if (position==5){
Intent intent1 = new Intent(view.getContext(),kp.class);
mcontext.startActivity(intent1);
}
if (position==6){
Intent intent1 = new Intent(view.getContext(),balot.class);
mcontext.startActivity(intent1);
}
if (position==7){
Intent intent1 = new Intent(view.getContext(),tarneeb31.class);
mcontext.startActivity(intent1);
}
if (position==8){
Intent intent1 = new Intent(view.getContext(),tarneeb61.class);
mcontext.startActivity(intent1);
}
if (position==9){
Intent intent1 = new Intent(view.getContext(),tarneeb41.class);
mcontext.startActivity(intent1);
}
if (position==10){
Intent intent1 = new Intent(view.getContext(),tarneeb41e.class);
mcontext.startActivity(intent1);
}
if (position==11){
Intent intent1 = new Intent(view.getContext(),tarneeb400.class);
mcontext.startActivity(intent1);
}
}
});
}
@Override
public int getItemCount() {
return mgamename.size();
}
@Override
public void onItemMove(int fromPosition, int toPostion) {
String fromNote = mgamename.get(fromPosition);
mgamename.remove(fromNote);
mgamename.add(toPostion,fromNote);
notifyItemMoved(fromPosition,toPostion);
}
@Override
public void onItemSwipe(int postion) {
mgamename.remove(postion);
notifyItemRemoved(postion);
}
public void setmTouchHelper(ItemTouchHelper touchHelper){
this.mTouchHelper = touchHelper;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements
View.OnTouchListener, GestureDetector.OnGestureListener {
TextView gamename;
RelativeLayout parentLayout;
TextView number;
CardView cardView;
GestureDetector mGestureDetector;
public ViewHolder(@NonNull View itemView) {
super(itemView);
gamename = (TextView) itemView.findViewById(R.id.gamename);
number = (TextView) itemView.findViewById(R.id.number);
parentLayout = (RelativeLayout) itemView.findViewById(R.id.parent_layout);
cardView = (CardView)itemView.findViewById(R.id.cardv);
mGestureDetector = new GestureDetector(itemView.getContext(),this);
itemView.setOnTouchListener(this);
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
cardView.callOnClick();
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
mTouchHelper.startDrag(this);
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mGestureDetector.onTouchEvent(motionEvent);
return true;
}
}
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ArrayList<String> mgamename = new ArrayList<>();
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
public static final String LIST_OF_SORTED_DATA_ID = "json_list_sorted_data_id";
public final static String PREFERENCE_FILE = "preference_file";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSharedPreferences = this.getApplicationContext().getSharedPreferences(PREFERENCE_FILE,Context.MODE_PRIVATE);
mEditor = mSharedPreferences.edit();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Log.d(TAG,"onCreate: started.");
mgamename.add("Complex Complex");
mgamename.add("Trix Complex");
mgamename.add("Kingdoms");
mgamename.add("Complex Complex Partner");
mgamename.add("Trix Complex Partner");
mgamename.add("Kingdoms Partner");
mgamename.add("Balot");
mgamename.add("Tarneeb 31");
mgamename.add("Tarneeb 61");
mgamename.add("Tarneeb 41(Syrian)");
mgamename.add("Tarneeb 41 (Egyptian)");
mgamename.add("Tarneeb 400");
initRecyclerView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.item1:
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
String shareBody = "An App to Calculate All Your Beloved Games Such As Balot Tarneeb Trix etc";
String shareSub = "Cards Game Calculator";
myIntent.putExtra(Intent.EXTRA_SUBJECT,shareSub);
myIntent.putExtra(Intent.EXTRA_TEXT,shareBody);
startActivity(Intent.createChooser(myIntent,"Share Using"));}
switch (item.getItemId()){
case R.id.item2:
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("About")
.setMessage("Calculation Note For Your Beloved Games Such As Balot Taneeb Trix. Enjoy!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
alertDialog.setCanceledOnTouchOutside(false);
}
return super.onOptionsItemSelected(item);
}
private void initRecyclerView(){
Log.d(TAG,"initRecyclerView: init recyclerview");
RecyclerView recyclerview = findViewById(R.id.recycler_view);
final RecyclerViewAdapter adapter = new RecyclerViewAdapter(mgamename,this);
recyclerview.setLayoutManager(new LinearLayoutManager(this));
ItemTouchHelper.Callback callback = new MyItemTouchHelper(adapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
adapter.setmTouchHelper(itemTouchHelper);
itemTouchHelper.attachToRecyclerView(recyclerview);
recyclerview.setAdapter(adapter);
}
}
Пожалуйста, помогите мне, спасибо заранее.