RecyclerView和动画中的notifyItemInserted() [英] notifyItemInserted() in RecyclerView and animation
问题描述
当我添加新项目时,我在调用方法
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);
}
,希望看到添加的项目具有动画效果,但只有recyclerView中的上一个项目可以动画。我尝试更改(position + 1)或(position-1)等位置,但这无济于事。
活动的代码:
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.
推荐答案
您没有在 addItem()
方法中添加项目。
您为适配器分配了一个完整的新列表。
You are not adding an item in your addItem()
method.
You assign a complete new list to your adapter.
将addItem方法更改为以下内容:
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);
也许您应该看看 SortedList类
,它可以使您的生活更轻松。
Maybe you should look at the SortedList class
, it could make your life easier.
在此处查看文档:
https://developer.android.com/reference/android/support/v7/util/SortedList.html
这篇关于RecyclerView和动画中的notifyItemInserted()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!