具有多种视图类型的Recylerview

时间:2019-10-21 15:40:32

标签: android android-recyclerview

我正在尝试使用四种视图类型制作recyclerview,每个视图类型将代表某些数据,我的应用程序用于英超联赛(足球),因此我对目标,黄牌和红牌具有视图类型, 每个字符串都有进球的时间,或得到黄牌和红牌的时间,我正在尝试运用一些逻辑,但对我来说没用

       //Splitting Strings
       String[] homeg = hometiming.split(";");
       String[] homeyello = homeyellow.split(";");
       String[] homere = homered.split(";");

       for (String h : homeg) {
           if (!h.equals("")) {

               int subgoals = Integer.parseInt(h.substring(0, 1));

               for (String y : homeyello) {
                   if (!y.equals("")) {

                       int subYellow = Integer.parseInt(y.substring(0, 1));

                       for (String n : homere) {
                           if (!n.equals("")) {
                               int subRed = Integer.parseInt(n.substring(0, 1));

                               /// if goals time is smaller than time of yellow and rec card , show 
                               /// goals
                               if (subgoals < subYellow && subgoals < subRed) {

                                   GoalsItem goalsItem = new GoalsItem();
                                   goalsItem.setHomegoals("goals :" + h + "\n");
                                   consolidatedlist.add(goalsItem);
                               }
                               /// if yellow cards time is smaller than time of goals and red cards , 
                               /// show yellow cards
                               else if (subYellow < subgoals && subYellow < subRed) {

                                   YellowItems yellowItems = new YellowItems();
                                   yellowItems.setHomeyellow("yellow :" + y + "\n");
                                   consolidatedlist.add(yellowItems);
                                }
                               /// if red card time is smaller than time of goals and yellow cards , 
                               /// show red cards
                               else if (subRed < subgoals && subRed < subYellow) {
                                   RedItems redItems = new RedItems();
                                   redItems.setHomered("red : " + n + "\n");
                                   consolidatedlist.add(redItems);

                           } } } } } } }
  • 第三名:这是我在bindviewholder上的应用程序
 @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {

            case TypeItem.TYPE_PHOTO:
                PhotoItem photoItem = (PhotoItem) list.get(position);
                ViewPhoto viewphoto = (ViewPhoto) holder;
                viewphoto.date.setText("" + photoItem.getDate());
                Picasso.with(context).load(photoItem.getPhoto()).fit().into(((ViewPhoto) holder).photo);
                break;

            case TypeItem.TYPE_Goals:
                GoalsItem goalsItem = (GoalsItem) list.get(position);
                ViewGoals viewgoals = (ViewGoals) holder;
                viewgoals.homegoals.setText("" + goalsItem.getHomegoals());
                viewgoals.awaygoals.setText("" + goalsItem.getAwaygoals());

                break;

            case TypeItem.TYPE_Yellow:
                YellowItems yellowItems = (YellowItems) list.get(position);
                ViewYellow viewyellow = (ViewYellow) holder;
                viewyellow.homeyellow.setText("" + yellowItems.getHomeyellow());
                viewyellow.awayyellow.setText("" + yellowItems.getAwayyellow());

                break;
            case TypeItem.TYPE_RED:
                RedItems redItems = (RedItems) list.get(position);
                ViewRed viewred = (ViewRed) holder;
                viewred.homered.setText("" + redItems.getHomered());
                viewred.awayred.setText("" +redItems.getAwayred());
                break;

        }
    }
  • 这是我正在使用的json示例
         "strHomeGoalDetails":"",
         "strHomeRedCards":"",
         "strHomeYellowCards":"13':Luka Milivojevic;",
         "strAwayRedCards":"",
         "strAwayYellowCards":"47':Raheem Sterling;",
         "strAwayGoalDetails":"39':Gabriel Jesus;41':David Silva;",

1 个答案:

答案 0 :(得分:1)

您必须override getItemViewType(position: Int),而不是viewType中的onCreatecreate返回不同的ViewHolders(如果需要),并且比onBindViewHolder中的绑定您的类型

例如,在您的适配器类中,使用typeField: Stringkey: Int

创建映射
private val keyMap = HashMap<String, Int>().apply {
    put("FIRST_TYPE", 1)
    put("SECOND_TYPE", 2)
    put("THIRD_TYPE", 3)
}

第二步是覆盖getItemViewType(position: Int)

override fun getItemViewType(position: Int): Int {
return keyMap.get(yourItemList(position).getType())

第三步-override onCreateViewHolder

 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResourceViewHolder {
        return when (viewType) {
           keyMap.get("FIRST_TYPE") -> //create specific ViewHolder
           keyMap.get("SECOND_TYPE") -> //create specific ViewHolder
           //etc
}

最后一部分是绑定onBindViewHolder

您可以执行与onCreateViewHolder

相同的方式