始终将特定项目设置在recyclerview顶部

时间:2019-05-17 13:03:22

标签: java android android-recyclerview

我知道如何将新添加的项目放在顶部,但是在这里,我想将特定项目放在顶部,而新项目已经添加了很多次。

我在这里上传了一个picture 您可以在这里找到我想要的。

在该图中,您可以看到新加入的团队始终排在最前面,而许多团队则通过其他设备加入。换句话说,在该图片中,用户可以看到自己的团队始终排在最前面。而其他用户已添加团队。

这是我的代码

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

    }

1 个答案:

答案 0 :(得分:0)

您需要使用自定义分类器对leaderboardArrayList进行分类。这样,您可以将顶部元素放在顶部(我猜只有一个),而其他元素则根据您想要的任何属性(例如日期,最大数量...)进行排序

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