【问题标题】:notifyItemInserted() in RecyclerView and animationRecyclerView 和动画中的 notifyItemInserted()
【发布时间】:2015-11-30 16:11:34
【问题描述】:

当我添加一个新项目时,我正在调用方法

public void addItem(ArrayList<ArrayList<String>> arrayList, int position){
    this.arrayList=arrayList;
    notifyItemInserted(position);
}

并期望看到添加的项目动画,但只有 recyclerView 中的前一个项目可以动画。我试图改变位置,如 (position+1) 或 (position-1),但它没有帮助。 活动代码:

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);
}

请给我小费,聪明人。

【问题讨论】:

    标签: android animation android-recyclerview custom-adapter


    【解决方案1】:

    您没有在 addItem() 方法中添加项目。 您将一个完整的新列表分配给您的适配器。

    把你的 addItem 方法改成这样:

    public void addItem(String item, int position) {
        this.arrayList.add(position, item);
        notifyItemInserted(position);
    }
    

    要将项目添加到您的列表视图调用:

    adapter.addItem("test", 0);
    

    也许你应该看看SortedList class,它可以让你的生活更轻松。

    在此处查看文档: https://developer.android.com/reference/android/support/v7/util/SortedList.html

    【讨论】:

    • 很遗憾,它对我没有帮助。
    • 您的点击方法有几个错误,您的代码也不完整。也许你应该看看 recyclerview 教程。
    【解决方案2】:

    为时已晚。但是我发现在所有“for”函数中,您将第一个参数设置为 1 而不是 0。否则,您必须将第二个条件设置为 '=' 而不是 '

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多