在保留现有项目的同时将新项目添加到现有索引

时间:2016-07-03 12:01:04

标签: android android-recyclerview

如何在当前包含项目的索引处向RecyclerView添加项目,并按堆叠顺序保留其下方存在的所有项目?例如,如果我有这个列表:

  

item = 0,第1项,第2项,......

我想将一个项目插入现有索引但不替换原始值:

  

item = 0,项目A ,第1项,第2项,......

到目前为止我使用的代码是:

public void addRecyclerObject(int position, Object recyclerObject) {
   recyclerObjects.add(position, recyclerObject);
   notifyItemInserted(position);
}

但是,当我使用执行上面的代码时,RecyclerView会这样呈现:

  

item = 0,项目A ,第2项,...

item 1已完全删除,并替换为其他值。如何在不替换现有值的情况下插入其他项目?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果您使用package recursiondemo; import static java.lang.Math.log; /** This class demonstrates the recursion with calculation of the value log(N!) * log(N!) = log(N*(N-1).....3*2*1) = log(N) + log (N-1) + ......log(3) + log (2) + log(1) * @author = */ public class logRecursion implements recursionInterface { //private int localCounter = 0; public logRecursion(){ } /** * * @param localCounter * @return */ //@Override public double directCalculation(int localCounter){ double result = 0.0; int loopCounter = localCounter; while ( loopCounter >=1) { result += log(loopCounter); --loopCounter; } return result; } public double calculation(int localCounter) throws Exception{ if (localCounter == 1) { return 0.0; } if (localCounter <= 0 ) { throw new Exception("Factorials are not defined for the input given"); } try { return log(localCounter) + calculation(localCounter - 1); // Recursion } catch (StackOverflowError e) { System.err.println("Caught stack Overflow error: " + e.getMessage()); } return 0.0; // This is an arbitrary return value to avoid compile time error of no return parameter. So this return value is meaning less } } package recursiondemo; /** * Driver class * @author */ public class RecursionDemo { /** * @param args the command line arguments * @throws java.lang.Exception */ public static void main(String[] args) throws Exception { // TODO code application logic here logRecursion test; test = new logRecursion(); System.out.println("The factorial of log of " + args[0] + " is " + test.calculation(Integer.parseInt(args[0]))); // Recursion System.out.println("Direct calculation " + test.directCalculation(Integer.parseInt(args[0])) ); // Direct calculation } } 初始化List中的项目,则可以轻松地将其插入现有索引中,并且移动存储在该位置的原始项目而不进行替换。例如:

RecyclerView
正常插入

list.add(1, "String 1") list.add(2, "String 2") list.add(2, "String 3") String1被插入第二个位置。 String2也会插入第二个位置,将String3推入第三个位置并更改数据集的其余部分。如果您需要更多帮助,请查看this帖子。告诉我你怎么走。

相关问题