I know how to set newly added item on top but here I want to stay specific item on top while new item has been added many time.
Here I upload one picture You can find here What I want.
In that picture you can see new joined team stay always on top while many team joined by other devices. In other words in that picture user can see own team always on top. while other user has been added teams.
Here is my code
public LeaderBoardFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_leader_board, container, false);
recyclerView = view.findViewById(R.id.leaderboard_recycler);
wp10ProgressBar = view.findViewById(R.id.wp10progressBar);
wp10ProgressBar.showProgressBar();
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
// recyclerView.setItemAnimator(itemAnimator);
//// recyclerView.addItemDecoration(dividerItemDecoration);
return view;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// itemAnimator = new DefaultItemAnimator();
// itemAnimator.setAddDuration(5000);
// itemAnimator.setRemoveDuration(5000);
getLeaderBoardData();
}
private void getLeaderBoardData() {
Sharedpref sharedpref = Sharedpref.getInstance(getActivity());
queue = Volley.newRequestQueue(Objects.requireNonNull(getActivity()));
intent = Objects.requireNonNull(getActivity()).getIntent();
matchId = sharedpref.getString("matchId");
leaderboardArrayList = new ArrayList<>();
HashMap<String, String> params = new HashMap<>();
params.put("match", matchId);
Log.d(TAG + "112", String.valueOf(params));
String Url = Constants.Base_URL + "leaderboard/";
JsonObjectRequest request = new JsonObjectRequest(Url, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, String.valueOf(response));
try {
String statusObject = response.getString("status");
String msgObject = response.getString("msg");
if (statusObject.equals("200")) {
JSONArray jsonArray = response.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject projResponse = jsonArray.getJSONObject(i);
String rankObject = projResponse.getString("rank");
String killsObject = projResponse.getString("kills");
String myteamObject = projResponse.getString("myteam");
Log.d("response", rankObject);
Log.d("response", killsObject);
Log.d("response", myteamObject);
// Collections.reverse(leaderboardArrayList);
leaderboardArrayList.add(new Leaderboard(rankObject, killsObject, myteamObject));
}
leaderBoardAdapter = new LeaderBoardAdapter(getContext(), leaderboardArrayList);
recyclerView.setAdapter(leaderBoardAdapter);
leaderBoardAdapter.notifyItemInserted(0);
leaderBoardAdapter.notifyDataSetChanged();
// prizeAdapter.setOnItemClickListner(WingsActivity.this);
wp10ProgressBar.hideProgressBar();
}else {
wp10ProgressBar.hideProgressBar();
// shimmerLayout.stopShimmerAnimation();
Toast.makeText(getContext(), msgObject, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(request);
}
You need to sort your leaderboardArrayList
with a custom sorter. That way you put top elements on top (I guessed there was only one), and other elements are sorted on any properties you want (like a date, a max amount ...)
Collections.sort(leaderboardArrayList, (a, b) -> {
//mustBeOnTop is a custom method that return true if this object is the one you want on top of your list
if(1 == a.user_id) return -1;
if(1 == b.user_id) return 1;
return a.joinedDate.compareTo(b.joinedDate); //compare their "join" date
});
leaderBoardAdapter = new LeaderBoardAdapter(getContext(), leaderboardArrayList);
recyclerView.setAdapter(leaderBoardAdapter); //don't need to call any notification method of the adapter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.