问题
When I'm adding a new item, I'm calling method
public void addItem(ArrayList<ArrayList<String>> arrayList, int position){
this.arrayList=arrayList;
notifyItemInserted(position);
}
and expect to see added item animated, but only previous item in recyclerView can be animated. I tried to change position like (position+1) or (position-1), but it doesn't help. Activity's code:
private void showDialog(final String table_name) {
...
final Cursor c = db.rawQuery("SELECT * FROM '"+table_name+"'",null);
final String mas[]=c.getColumnNames();
c.close();
final List<EditText> list = new ArrayList<>();
for (int m = 1; m < mas.length; m++) {
list.add(new EditText(this));
list.get(m-1).setHint(mas[m]);
linearLayout.addView(list.get(m-1));
}
alert.setView(linearLayout);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ArrayList<String> arrayList=new ArrayList<String>();
for (int i=0;i<list.size();i++)
arrayList.add(list.get(i).getText().toString());
try {
arrayList=AESEncryption.encrypt2(arrayList);
ContentValues contentValues=new ContentValues();
for (int i=1;i<mas.length;i++){
contentValues.put(mas[i],arrayList.get(i-1));
}
db.insert(table_name, null, contentValues);
adapter.addItem(startDecrypt(),adapter.getItemCount());
} catch (BadPaddingException e) ...
});
...
alert.show();
}
...
private void fillListView() throws GeneralSecurityException, IOException, ClassNotFoundException {
dbHelper = new DBHelper(this);
dbHelper.setFILENAME(getIntent().getExtras().getString("categoryName"));
db = dbHelper.getWritableDatabase();
dbHelper.createTable(db);
Cursor c = db.rawQuery("SELECT * FROM '"+FILENAME+"'",null);
String mas[]=c.getColumnNames();
String[] mas2 = Arrays.copyOfRange(mas, 1, mas.length);
c.close();
adapter=new RVAdapter(startDecrypt(),mas2,PasswordManagerActivity.this,FILENAME, AESEncryption);
rv.setAdapter(adapter);
rv.setHasFixedSize(true);
}
Please, give me a tip, wise men.
回答1:
You are not adding an item in your addItem()
method.
You assign a complete new list to your adapter.
Change your addItem method to something like this:
public void addItem(String item, int position) {
this.arrayList.add(position, item);
notifyItemInserted(position);
}
To add an item to your listview call:
adapter.addItem("test", 0);
Maybe you should look at the SortedList class
, it could make your life easier.
See the docs here: https://developer.android.com/reference/android/support/v7/util/SortedList.html
回答2:
It is too late. But what I found that in all your 'for' functions, you are setting first argument to 1 instead 0. Otherwise, you have to set second condition to '=' instead of '<'. Hope this set animation correct.
回答3:
public void addItem(String item, int position){
yourList.add(position, item);
notifyDataSetChanged();
}
use this
来源:https://stackoverflow.com/questions/32399115/notifyiteminserted-in-recyclerview-and-animation