RecyclerView在11行后重复标题

问题描述 投票:1回答:2

我正在使用包含2个参数的真正基本数据库。我正在使用recyclerview在我的活动中显示数据。这是问题所在。在一定数量的行(在这种情况下为11)之后,它只是重复标题,一切正常。我只是想摆脱那种重复,我需要你的帮助才能这样做。我会尝试添加一些图片来告诉你发生了什么

first picture

这里有重复

second picture

这是回收者视图的代码

public class RecyclerAdapter_ZOZNAM extends RecyclerView.Adapter<RecyclerAdapter_ZOZNAM.RecyclerViewHolder_ZOZNAM> {
ArrayList<ZOZNAM_ZAMESTNANCOV> arrayList = new ArrayList<>();
RecyclerAdapter_ZOZNAM(ArrayList<ZOZNAM_ZAMESTNANCOV> arrayList)
{
    this.arrayList = arrayList;
}

@Override
public RecyclerViewHolder_ZOZNAM onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout_workers,parent,false);
    RecyclerViewHolder_ZOZNAM recyclerViewHolder = new RecyclerViewHolder_ZOZNAM(view);
    return recyclerViewHolder;
}

@Override
public void onBindViewHolder(RecyclerAdapter_ZOZNAM.RecyclerViewHolder_ZOZNAM holder, int position) {

    ZOZNAM_ZAMESTNANCOV zamestnanci = arrayList.get(position);
    if (position == 0){
        holder.headerTopLine.setVisibility(View.VISIBLE);
        holder.header.setVisibility(View.VISIBLE);
        holder.headerBottomLine.setVisibility(View.VISIBLE);
    }
    holder.Name.setText(zamestnanci.getMeno());
    holder.Number.setText(zamestnanci.getCislo());
}

@Override
public int getItemCount() {
    return arrayList.size();
}

public static  class RecyclerViewHolder_ZOZNAM extends  RecyclerView.ViewHolder
{
    TextView Name,Number;
    LinearLayout header;
    ImageView headerTopLine, headerBottomLine;
    RecyclerViewHolder_ZOZNAM(View view)
    {
        super(view);
        headerTopLine = (ImageView) view.findViewById(R.id.headerTopLine);
        header = (LinearLayout) view.findViewById(R.id.header);
        headerBottomLine = (ImageView) view.findViewById(R.id.headerBottomLine);
        Name = (TextView)view.findViewById(R.id.workName);
        Number = (TextView)view.findViewById(R.id.workNumber);

    }
}
}

和XML文件

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/headerTopLine"
        android:visibility="gone"
        tools:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"/>

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="gone"
        tools:visibility="visible">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="CELÉ MENO"
            android:gravity="center_horizontal"
            android:textSize="20dp"
            android:textStyle="bold"
            />
        <ImageView
            android:paddingTop="10dp"
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="#000"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="TELEFÓN"
            android:gravity="center_horizontal"
            android:textSize="20dp"
            android:textStyle="bold"
            />
        <ImageView
            android:paddingTop="10dp"
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="#000"/>

    </LinearLayout>

    <ImageView
        android:id="@+id/headerBottomLine"
        android:visibility="gone"
        tools:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Celé meno"
            android:gravity="center_horizontal"
            android:textSize="15dp"
            android:id="@+id/workName"
            android:textStyle="bold"
            />
        <ImageView
            android:paddingTop="10dp"
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="#000"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text=""
            android:gravity="center_horizontal"
            android:textSize="15dp"
            android:id="@+id/workNumber"
            android:textStyle="bold"
            />
        <ImageView
            android:paddingTop="10dp"
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="#000"/>

    </LinearLayout>

    <ImageView
        android:paddingTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#000"
        android:id="@+id/footer"/>
</LinearLayout>

这就是我在活动中使用它的方式

recyclerView = (RecyclerView) findViewById(R.id.dbViewworkers);
                arrayList.clear();
                layoutManager = new LinearLayoutManager(workersActivity.this);


                recyclerView.setLayoutManager(new GridLayoutManager(workersActivity.this, 1, GridLayoutManager.VERTICAL, false));
                recyclerView.setHasFixedSize(true);
                cursor.moveToFirst();
                do {

                    ZOZNAM_ZAMESTNANCOV zamestnanci = new ZOZNAM_ZAMESTNANCOV(cursor.getString(1), cursor.getString(2));
                    arrayList.add(zamestnanci);

                } while (cursor.moveToNext());




                workDb.close();

                adapter = new RecyclerAdapter_ZOZNAM(arrayList);
                recyclerView.setAdapter(adapter);
android android-recyclerview duplicates recycler-adapter repeat
2个回答
1
投票

你的onBindViewHolder无效,因为它没有正确地重新绑定在位置0中使用的视图(当它在不同的位置重新使用时显示标题)。简单的其他情况应该解决您的问题:

@Override
public void onBindViewHolder(RecyclerAdapter_ZOZNAM.RecyclerViewHolder_ZOZNAM holder, int position) {

    ZOZNAM_ZAMESTNANCOV zamestnanci = arrayList.get(position);
    if (position == 0){
        holder.headerTopLine.setVisibility(View.VISIBLE);
        holder.header.setVisibility(View.VISIBLE);
        holder.headerBottomLine.setVisibility(View.VISIBLE);
    } else {
        holder.headerTopLine.setVisibility(View.GONE);
        holder.header.setVisibility(View.GONE);
        holder.headerBottomLine.setVisibility(View.GONE);
    }
    holder.Name.setText(zamestnanci.getMeno());
    holder.Number.setText(zamestnanci.getCislo());
}

0
投票

在适配器中使用此功能。

@Override
public int getItemViewType(int position) {
    return position
}
© www.soinside.com 2019 - 2024. All rights reserved.