简体   繁体   English

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

[英]Set specific item always on top recyclerview

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. 您需要使用自定义排序器对leaderboardArrayList进行排序。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM