如何基于onBindHolder

时间:2017-09-23 13:17:05

标签: android json android-recyclerview

您好我正在使用json填充回收站视图。我有一个textview,它是类别名称,在json数组中重复。我只需要显示一次。

Example :
Row 1
**My Category Name 1**
My list name 1

Row 2
My list name 2

Row 3
**My Category Name 2**
My list name 1

所以在这里我需要根据重复的值隐藏**我的类别名称**。 一切都运作完美,但滚动RecyclerView时它变得混乱。

我的适配器类

    @Override
    public void onBindViewHolder(final RecyclerViewQuest.MyViewHolder holder, final int position) {

        final QuestModel questModel=questModelArrayList.get(position);

        String masterTitle = questModel.getSurvycat();
        if(temptitle.equalsIgnoreCase(masterTitle))
        { holder.categoryName.setVisibility(View.GONE);
        }
        else
        {
            temptitle = masterTitle;
            holder.categoryName.setVisibility(View.VISIBLE);
            holder.categoryName.setText(masterTitle);
        }

        holder.categoryName.setText(questModel.getSurvycat());
        if(questModel.getQformat().equals("Y/N")){
            holder.radioGroupQuest.setTag(questModelArrayList.get(position));
            holder.questRadio.setText(questModel.getQlist());
            holder.layoutRadio.setVisibility(View.VISIBLE);
            holder.layoutEditText.setVisibility(View.GONE);

        }else {
            holder.questEditText.setText(questModel.getQlist());
            holder.layoutRadio.setVisibility(View.GONE);
            holder.layoutEditText.setVisibility(View.VISIBLE);
        }



}

我的JSON数据

"messageText": [
        {
            "survycatid": 1,
            "survycat": "Category 1",
            "qsetid": 1,
            "qlistid": 1,
            "qlist": "Are you aware of this",
            "qformat": "obj"
        },
        {
            "survycatid": 1,
            "survycat": "Category 1",
            "qsetid": 1,
            "qlistid": 2,
            "qlist": "If yes, are you aware o.",
            "qformat": "Y/N"
        },
        {
            "survycatid": 1,
            "survycat": "Category 1",
            "qsetid": 1,
            "qlistid": 3,
            "qlist": "If NO, are you   aware of anyone who may be benefitted from the scheme, if included",
            "qformat": "Y/N"
        },
        {
            "survycatid": 2,
            "survycat": "Category 2",
            "qsetid": 1,
            "qlistid": 2,
            "qlist": "If yes, can you please provide the  of those to be included.",
            "qformat": "Y/N"
        },
        {
            "survycatid": 2,
            "survycat": "Category 2",
            "qsetid": 1,
            "qlistid": 5,
            "qlist": "Additional Comments",
            "qformat": "TextLine"
        },
        {
            "survycatid": 2,
            "survycat": "Category 2",
            "qsetid": 1,
            "qlistid": 1,
            "qlist": "Are you aware of the scheme",
            "qformat": "Y/N"
        },

我试图隐藏标题文本字段,它在第一次尝试时工作,但在进一步滚动时它变得混乱。请给我一个解决方案。

提前致谢。如果它是一个小技巧,那么对不起我的无知。

1 个答案:

答案 0 :(得分:0)

“temptitle”的值不应存储为类的成员。您应该检查questModelArrayList.get(position-1).getSurvycat()是否等于questModelArrayList.get(position).getSurvycat()。如果是,你应该在这个ViewHolder中隐藏标题,记住位置为0时的特殊情况。