根据名称和标题对 RecycerView 项目进行分组

时间:2021-06-03 11:45:48

标签: java android android-recyclerview

安卓新手! 我一直在使用带有标题的 RecyclerView
如果相同,我想按比赛名称对项目进行分组, image

JSON 数据

    (
        [0] => stdClass Object
            (
                [contest_id] => 4571
                [contest_name] => Practice Contest
                [contest_tag] => Get ready for mega winnings!
                [winners] => 25
                [prize_pool] => 0
                [total_team] => 50
                [join_team] => 0
                [entry] => 0
            )
    
        [1] => stdClass Object
            (
                [contest_id] => 4572
                [contest_name] => Practice Contest
                [contest_tag] => Get ready for mega winnings!
                [winners] => 10
                [prize_pool] => 50
                [total_team] => 20
                [join_team] => 0
                [entry] => 0
            )
      
        [2] => stdClass Object
            (
                [contest_id] => 4574
                [contest_name] => spl Contest
                [contest_tag] => Get ready for mega winnings!
                [winners] => 100
                [prize_pool] => 500
                [total_team] => 100
                [join_team] => 0
                [entry] => 50
            )
    
        [3] => stdClass Object
            (
                [contest_id] => 4575
                [contest_name] => spl Contest
                [contest_tag] => test
                [winners] => 5
                [prize_pool] => 100
                [total_team] => 10
                [join_team] => 0
                [entry] => 50
                [contest_description] => tetret
                [contest_note1] => tetet
                [contest_note2] => tetet
                [winning_note] => 
                [match_id] => 143
                [sport_type] => cricket
                [cancelled] => 0
                [type] => 
            )
            //different contests 
            .
            .
            )

RecyclerView 类的代码 我只放了一些重要的代码。如果您需要 XML 文件,请告诉我。

@Override
    public void getResult(Context mContext, String type, String message, JSONObject result) {
           if (type.equals(CONTESTLISTTYPE)){
            binding.swipeRefreshLayout.setRefreshing(false);
            binding.tvNoDataAvailable.setVisibility(View.GONE);
            binding.RvContestList.setVisibility(View.VISIBLE);
            try {
                JSONArray jsonArray = result.getJSONArray("data");
                 beanContestLists = new Gson().fromJson(jsonArray.toString(),
                        new TypeToken<List<BeanContestList>>() {
                        }.getType());
              
                adapterContestList = new AdapterContestList(beanContestLists, activity);
                binding.RvContestList.setAdapter(adapterContestList);

            } catch (Exception e) {
                e.printStackTrace();
            }

            adapterContestList.notifyDataSetChanged();

        }

}
  public class AdapterContestList extends RecyclerView.Adapter<AdapterContestList.MyViewHolder> {
        private List<BeanContestList> mListenerList;
        Context mContext;     
        public AdapterContestList(List<BeanContestList> mListenerList, Context context) {
            mContext = context;
            this.mListenerList = mListenerList;
}
  public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tv_LiveContestName,tv_LiveContestDesc,tv_TotalPrice,tv_WinnersCount,tv_EntryFees,tv_TeamLeftCount,tv_TotalTeamCount;             
           public MyViewHolder(View view) {
                super(view);
                tv_LiveContestName = view.findViewById(R.id.tv_LiveContestName);
                tv_LiveContestDesc = view.findViewById(R.id.tv_LiveContestDesc);
                tv_TotalPrice = view.findViewById(R.id.tv_TotalPrice);
                tv_WinnersCount = view.findViewById(R.id.tv_WinnersCount);
                tv_EntryFees = view.findViewById(R.id.tv_EntryFees);
                tv_TeamLeftCount = view.findViewById(R.id.tv_TeamLeftCount);
                tv_TotalTeamCount = view.findViewById(R.id.tv_TotalTeamCount);
                PB_EntryProgress = view.findViewById(R.id.PB_EntryProgress);
                tv_JoinContest = view.findViewById(R.id.tv_JoinContest);
                tv_Contest_bonus_msg=view.findViewById(R.id.tv_Contest_bonus_msg);                
            }
        }
        @Override
        public int getItemCount() {
            return mListenerList.size();
        }
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.adapter_contest_list, parent, false);
            return new MyViewHolder(itemView);
        }
       @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {

            final String contest_id = mListenerList.get(position).getContest_id();
            String contest_name= mListenerList.get(position).getContest_name();
            String contest_tag= mListenerList.get(position).getContest_tag();
            String winners= mListenerList.get(position).getWinners();
            prize_pool= mListenerList.get(position).getPrize_pool();
            String total_team= mListenerList.get(position).getTotal_team();
            String join_team= mListenerList.get(position).getJoin_team();
            final String entry= mListenerList.get(position).getEntry();

            holder.tv_LiveContestName.setText(contest_name);
            holder.tv_LiveContestDesc.setText(contest_tag);
            holder.tv_TotalPrice.setText("₹ "+prize_pool);
            holder.tv_WinnersCount.setText(winners);
            holder.tv_EntryFees.setText("₹ "+entry);
            holder.tv_TeamLeftCount.setText(remaining_team+" Spots Left");
            holder.tv_TotalTeamCount.setText(total_team+" Teams");
            holder.tv_JoinContest.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
 
            }); }}}

如果你想要其他任何东西,请告诉我。我应该改变什么? 请解释一下逻辑。 谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 按比赛名称对您的 BeanContestList 列表进行排序

  2. 在 onBindViewHolder 添加这个条件

    if (position > 0 && mListenerList.get(position).getContestName() == mListenerList.get(position - 1).getContestName()) { ///隐藏标题 } 别的 { //显示标题 } header.text = mListenerList.get(position).getContestName();

相关问题