滚动时,多列ListView中的行未正确排序:Android

时间:2017-06-06 16:30:37

标签: android listview firebase firebase-realtime-database

我已将数据填充到firebase数据库的多列 ListView中。

我已经扩展BaseAdapter课程以获得此视图,如下图所示。

为什么滚动时项目位置会发生变化?

滚动时,行会上下对齐。这是场景:

  

enter image description here

向下滚动......(看起来很好!)

  

enter image description here

滚动...

(记录未按顺序显示,因为您可以看到每次滚动列表时它们都会更改。)

  

enter image description here

这可能是什么原因?谁能解释一下?

Adapter类的

代码

    public class ReportAdapter extends BaseAdapter {

    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    TextView childOneNameColumn, childTwoNameColumn, childThreeNameColumn;
    TextView checkInColumn;
    TextView parentCheckInColumn;
    TextView checkOutColumn;
    TextView parentCheckOutColumn;
    TextView familyNameColumn;


    public ReportAdapter(Activity activity, ArrayList<HashMap<String, String>> list) {
        super();
        this.activity = activity;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        LayoutInflater inflater = activity.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.column_row, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> map = list.get(position);
        familyNameColumn.setText(map.get(FAMILY_NAME_COLUMN));
        childOneNameColumn.setText(map.get(CHILD_ONE_NAME_COLUMN));
        childTwoNameColumn.setText(map.get(CHILD_TWO_NAME_COLUMN));
        childThreeNameColumn.setText(map.get(CHILD_THREE_NAME_COLUMN));
        checkInColumn.setText(map.get(CHECKIN_COLUMN));
        parentCheckInColumn.setText(map.get(PARENT_CHECKIN_COLUMN));
        checkOutColumn.setText(map.get(CHECKOUT_COLUMN));
        parentCheckOutColumn.setText(map.get(PARENT_CHECKOUT_COLUMN));

        return convertView;
    }

    private class ViewHolder {

        public ViewHolder(View view) {
            childOneNameColumn = (TextView) view.findViewById(R.id.child1NameColumn);
            childTwoNameColumn = (TextView) view.findViewById(R.id.child2NameColumn);
            childThreeNameColumn = (TextView) view.findViewById(R.id.child3NameColumn);
            checkInColumn = (TextView) view.findViewById(R.id.checkInColumn);
            parentCheckInColumn = (TextView) view.findViewById(R.id.parentCheckInColumn);
            checkOutColumn = (TextView) view.findViewById(R.id.checkOutColumn);
            parentCheckOutColumn = (TextView) view.findViewById(R.id.parentCheckOutColumn);
            familyNameColumn = (TextView) view.findViewById(R.id.familyNameColumn);
        }
    }
}

布局 column_row.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">


    <LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/familyNameColumn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:textColor="@color/colorBlack" />

        <TextView
            android:id="@+id/child1NameColumn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" />

        <TextView
            android:id="@+id/child2NameColumn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" />

        <TextView
            android:id="@+id/child3NameColumn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" />

        <TextView
            android:id="@+id/checkInColumn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="@color/colorGreen" />

        <TextView
            android:id="@+id/parentCheckInColumn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7" />

        <TextView
            android:id="@+id/checkOutColumn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="@color/colorGreen" />

        <TextView
            android:id="@+id/parentCheckOutColumn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7" />

    </LinearLayout>
</RelativeLayout>

ListView 属性:

<ListView
    android:id="@+id/reportListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="40dp"
    android:transcriptMode="alwaysScroll" />

2 个答案:

答案 0 :(得分:1)

最好将ViewHolder用于长列表。

无论如何,你错误地设置了观点。在ListView中,您应该管理两个分支,无论是否convertView == null。当convertView!= null时,您还必须设置视图。

因此,如果您不想创建ViewHolder,请写:

if (convertView == null) {
    convertView = inflater.inflate(R.layout.column_row, parent, false);
}
childOneNameColumn = (TextView) convertView.findViewById(R.id.child1NameColumn);
childTwoNameColumn = (TextView) convertView.findViewById(R.id.child2NameColumn);
...

还尝试使用小写命名变量。

答案 1 :(得分:1)

搞乱行的主要原因是您没有在正确的TextView对象上设置文本。

每行有8 TextView个。这意味着,对于屏幕上显示的所有行,它们每个都有8个TextView个对象。

例如,如果屏幕上有10行,那么您将拥有10个TextView的10个单独副本。

您可以使用ViewHolder。 View Holder Pattern的主要目的是消除每次调用findViewById的需要,这样可以提供更好的性能。