我正在使用Firebase作为其后端制作Android Chat应用程序。我在布局中使用了RecyclerView,显示了目前正在使用聊天应用程序的用户。为了填充RecyclerView,我正在使用FirebaseRecyclerAdapter对象。
我想要做的是,在RecyclerView中显示附近的用户。所以我得到他们的位置并计算到我当前位置的距离。
我想停止将远离我所在位置的用户添加到RecyclerView。虽然仍然使用recyclerView显示空白空间,但我已经设置了setVisibility(View.GONE)。 我该如何解决这个问题
代码:
private void friendsLoad() {
friendsAdapter = new FirebaseRecyclerAdapter<UserDetailsClass, FriendsViewHolder>(
UserDetailsClass.class,
R.layout.friend_online,
FriendsViewHolder.class,
tempUserRef
) {
@Override
protected void populateViewHolder(final FriendsViewHolder viewHolder, UserDetailsClass model, int position) {
if (model.getUserID().equals(UserDetails.getString("UniqueID", "ID"))) {
sender = model.getName();
viewHolder.mView.setVisibility(View.GONE);
viewHolder.mView.setY(0);
viewHolder.mView.setWillNotDraw(false);
}
if (!model.getUserID().equals(UserDetails.getString("UniqueID", "ID"))) {
// Calculate Distance
distanceCalculator = new DistanceCalculator(latitude, longitude);
distanceCalculator.calculate(model.getLatitude(), model.getLongitude());
double distance = distanceCalculator.getDistanceInMeters();
if (distance >= 8000) {
viewHolder.mView.setVisibility(View.GONE);
}
if (distance >= 4000 && distance < 8000) {
Log.i("Distance", "More than 4 Km");
viewHolder.friendName.setText(model.getName());
viewHolder.stateName.setText("");
viewHolder.distance.setText("Not Near By");
viewHolder.distance.setTextColor(Color.RED);
}
if (2000 <= distance && distance < 4000) {
Log.i("Distance", "More less 4 Km and more than 2Km");
viewHolder.friendName.setText(model.getName());
viewHolder.stateName.setText(model.getStateName());
viewHolder.distance.setText("Approachable");
viewHolder.distance.setTextColor(getResources().getColor(R.color.skyBlue));
}
if (distance < 2000) {
Log.i("Distance", "less than 1 Km");
viewHolder.friendName.setText(model.getName());
viewHolder.stateName.setText(model.getStateName()); // Setting up the City name
viewHolder.distance.setText("Near By");
viewHolder.distance.setTextColor(getResources().getColor(R.color.commandoGreen));
}
//End of calculating distance
if (model.getOnline_offline_status() == 1) {
viewHolder.online.setText("Online");
viewHolder.online.setTextColor(getResources().getColor(R.color.darkGreen));
}
if (model.getOnline_offline_status() == 0) {
viewHolder.online.setText("");
}
}
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Log.i("TAG", "Clicked");
Intent intent = new Intent(ChatHomeActivity.this, NewMessageList.class);
intent.putExtra("Sender", sender);
intent.putExtra("Receiver", viewHolder.friendName.getText().toString());
startActivity(intent);
}
});
}
};
recyclerView.setLayoutManager(mLinearLayoutManager);
recyclerView.setAdapter(friendsAdapter);
}