Java 根据距离添加和删除recyclerview项

Java 根据距离添加和删除recyclerview项,java,android,arraylist,android-recyclerview,Java,Android,Arraylist,Android Recyclerview,我正在开发一个应用程序,其中消息列表(message_received)应仅在一定距离(50、100、200、300或400米)内可见 为此,我有一个消息的ArrayList(MESSAGE_received)、一个带有自定义适配器的RecyclerView和一个包含RecyclerView的片段,并将其作为我的ArrayList 我现在采用的方法如下: 要删除条目(在我的适配器中),请执行以下操作: 要添加条目(在我的适配器中),请执行以下操作: 最后,我的代码确定项目是否太远: ArrayL

我正在开发一个应用程序,其中消息列表(message_received)应仅在一定距离(50、100、200、300或400米)内可见

为此,我有一个消息的ArrayList(MESSAGE_received)、一个带有自定义适配器的RecyclerView和一个包含RecyclerView的片段,并将其作为我的ArrayList

我现在采用的方法如下:

要删除条目(在我的适配器中),请执行以下操作:

要添加条目(在我的适配器中),请执行以下操作:

最后,我的代码确定项目是否太远:

ArrayList<String[]> operation = new ArrayList<>();


for (int i = 0; i < MESSAGES_RECEIVED.size(); i++){

    if(dist <= distMax){
        if(!MESSAGES_RECEIVED.get(i).isDisplayed()){
            operation.add(new String[]{"add", String.valueOf(i)});
            MESSAGES_RECEIVED.get(i).setDisplayed(true);
        }
    } else {
        operation.add(new String[]{"remove", String.valueOf(i)});
    }

}

for (String[] values : operation){
    Log.i(TAG, "recalculateDistance: " + values[0] + " " + values[1]);
    if(values[0].equals("add")){
        int pos = Integer.valueOf(values[1]);
        mRecyclerViewAdapter.addAt(pos, MESSAGES_RECEIVED.get(pos));
    } else if(values[0].equals("remove")){
        int pos = Integer.valueOf(values[1]);
        mRecyclerViewAdapter.removeAt(pos);
    }
}
ArrayList操作=新建ArrayList();
对于(int i=0;i如果(distOk,我终于找到了一些方法使它工作。我创建了另一个ArrayList并将其放置在一个静态类中(这样我就可以从应用程序中的多个位置更新显示)

这是我创建的方法

公共静态void updateDisplaydMessages(){ 显示消息。清除()

getDistance(接收到的消息);
对于(消息m:已接收消息){
浮动距离=m.getDistance();
int distMax=Integer.valueOf(值优先半径地理围栏);
if(距离<距离最大值){
显示的消息。添加(m);
}
}
}
一旦调用了它,我就从片段中调用
notifyDataSetChanged
,就是这样!可能不是实现它的最有效的方法,但它是有效的

public void addAt(int pos, Message m){
    mMessagesList.add(pos, m);
    notifyItemInserted(pos);
}
ArrayList<String[]> operation = new ArrayList<>();


for (int i = 0; i < MESSAGES_RECEIVED.size(); i++){

    if(dist <= distMax){
        if(!MESSAGES_RECEIVED.get(i).isDisplayed()){
            operation.add(new String[]{"add", String.valueOf(i)});
            MESSAGES_RECEIVED.get(i).setDisplayed(true);
        }
    } else {
        operation.add(new String[]{"remove", String.valueOf(i)});
    }

}

for (String[] values : operation){
    Log.i(TAG, "recalculateDistance: " + values[0] + " " + values[1]);
    if(values[0].equals("add")){
        int pos = Integer.valueOf(values[1]);
        mRecyclerViewAdapter.addAt(pos, MESSAGES_RECEIVED.get(pos));
    } else if(values[0].equals("remove")){
        int pos = Integer.valueOf(values[1]);
        mRecyclerViewAdapter.removeAt(pos);
    }
}
    getDistance(MESSAGES_RECEIVED);
    for(Message m : MESSAGES_RECEIVED){

        float dist = m.getDistance();
        int distMax = Integer.valueOf(VALUE_PREF_RADIUS_GEO_FENCING);

        if(dist < distMax){
            MESSAGES_DISPLAYED.add(m);
        }
    }

}