के साथ डिवाइस को घूर्णन करने के बाद रीफ्रेशिंग नहीं देखें मेरे पास RecyclerView
AppCompatActivity
के अंदर है। डिवाइस घुमाए जाने के बाद आइटम सम्मिलन और परिवर्तन दिखाए जाते हैं और एनिमेटेड होते हैं।पुनर्चक्रण डिवाइस को एक खुले डायलॉगफ्रैगमेंट
समस्या होता है जब आप: RecyclerView
में किसी आइटम पर
- टैप करें।
- ए
DialogFragment
अगर आप आइटम को मिटाना चाहते हैं तो संकेत देते हैं। - डिवाइस को घुमाएं।
- संवाद में हटाने की पुष्टि करें।
- सरणी सूची की जांच करें। आइटम हटा दिया गया है।
RecyclerView
अभी भी आइटम दिखाता है।
notifyItemRemoved
के बजाय notifyDataSetChanged
उपयोग करने की कोशिश लेकिन या तो काम नहीं किया क्योंकि आइटम अभी भी RecyclerView
में दिखाया जा रहा है।
यह एंड्रॉइड के किसी भी संस्करण के साथ हो रहा है।
public class MyAppCompatActivity extends AppCompatActivity {
int positionOfDeletedItem;
MyObjectRecyclerViewAdapter adapter;
ArrayList<MyObject> someTestData;
MyItemDeletionHandler deletionHandlerRemover;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
positionOfDeletedItem = 1;
deletionHandlerRemover = new MyItemDeletionHandler(this);
someTestData = new ArrayList<MyObject>(3);
someTestData.add(new MyObject("A"));
someTestData.add(new MyObject("B"));
someTestData.add(new MyObject("C"));
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyObjectRecyclerViewAdapter(new MyAdapterOnClickEvent.OnItemClick() {
@Override
public void onClick(int posicion, int idViaje, View view) {
String tag = "Some tag value";
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag(tag);
if(prev != null)
ft.remove(prev);
ft.addToBackStack(null);
DialogFragment newFragment = MyDeletionConfirmationDialog.newInstance(deletionHandlerRemover);
newFragment.show(ft, tag);
}
}, someTestData);
recyclerView.setAdapter(adapter);
}
private final static class MyItemDeletionHandler extends Handler {
private final WeakReference<MyAppCompatActivity> theActivity;
private MyItemDeletionHandler(MyAppCompatActivity act) {
theActivity = new WeakReference<MyAppCompatActivity>(act);
}
@Override
public void handleMessage(Message msg) {
MyAppCompatActivity activity = theActivity.get();
if(activity != null) {
if(msg.what == 1) {
activity.deleteTheItem();
}
}
}
}
public void deleteTheItem() {
someTestData.remove(positionOfDeletedItem);
adapter.notifyItemRemoved(positionOfDeletedItem);
}
}
public class MyDeletionConfirmationDialog extends DialogFragment {
private Message handlerMessage;
public static MyDeletionConfirmationDialog newInstance(Handler callbackHandler) {
MyDeletionConfirmationDialog myDialog = new MyDeletionConfirmationDialog();
Bundle args = new Bundle();
args.putParcelable("handlerMessage", callbackHandler.obtainMessage(1, true));
myDialog.setArguments(args);
return myDialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handlerMessage = getArguments().getParcelable("handlerMessage");
}
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setMessage("Some message");
alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final Message toSend = Message.obtain(handlerMessage);
toSend.sendToTarget();
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Dialog dialog = alertDialogBuilder.create();
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
}
मैं कैसे RecyclerView
सही ढंग से काम करने के लिए प्राप्त कर सकते हैं:
कैसे प्रक्रिया संभाला जा रहा है की कोड सरलीकृत?
संपादित करें 1:
मैं अन्य RecyclerView
रों जिसमें यह ठीक से काम करता है। केवल अंतर यह है कि एस के अंदर AppCompatActivity
के बजाय हैं। मुझे संदेह है कि RecyclerView
के onDetachedFromWindow
और onAttachedToWindow
घटनाओं के साथ इसका कुछ संबंध है।
संपादित करें 2: संवाद (4 कदम) बंद कर दिया और खोला है
तो फिर यह उम्मीद के रूप में काम करता है।
संपादित करें 3:
RecyclerView
एक Fragment
के रूप में निकाला जाता है तो समस्या गायब हो जाता है और अपेक्षित तरीके से काम। Fragment
के बजाय AppCompatActivity
के संयोजन के साथ सही ढंग से काम करने के ऊपर वर्णित उपयोग केस होना असंभव है।
जब आप संवाद पर क्लिक करते हैं तो यह सूची में आइटम गायब हो जाना चाहिए और हटा देना चाहिए। तो जब आप अभिविन्यास बदलते हैं और यह डेटा हटा देता है तो संवाद कैसे रहता है? –
@ReazMurshed उपयोग केस निम्न है: आप इसे हटाने के लिए किसी आइटम पर क्लिक करते हैं। यह विकल्प ('रद्द करें' और 'हटाएं') के साथ एक पुष्टिकरण संवाद दिखाता है। आप किसी भी विकल्प पर क्लिक नहीं करते हैं। आप डिवाइस घुमाते हैं। संवाद अभी भी खुला है। फिर आप आइटम को हटाने के लिए 'हटाएं' विकल्प का चयन करें। संवाद बंद हो जाता है। आइटम डेटाबेस से हटा दिया गया है। 'RecyclerView' तदनुसार अपडेट नहीं किया गया है, यह अभी भी हटाए गए आइटम को दिखाता है। – OneEyeQuestion
यह एक उचित समाधान नहीं है, लेकिन वैसे भी, यह हैक आप जिस चाल को चाहते हैं वह कर सकता है। आप 'कॉन्फ़िगरेशन चेंज' में आसानी से अभिविन्यास परिवर्तन का पता लगा सकते हैं और स्क्रीन में दिखाए जा रहे संवाद को खारिज कर सकते हैं। स्क्रीन में दिखाई गई बातचीत गतिविधि/खंड जीवनशैली से संबंधित नहीं है और यही कारण है कि यह स्क्रीन पर बनी हुई है। –