Listview android

时间:2017-11-22 04:32:36

标签: android listview android-viewholder

我正在实现一个ListView,并且在大型列表中快速滚动时会出现这个故障。我正在使用ViewHolder Pattern

[![ListView中的UI故障] [1]] [1]

[1]:

getView:

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

    if (convertView == null) {
        convertView = LayoutInflater.from(this.getContext())
                .inflate(mResource, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.titleView = (TextView) convertView.findViewById(R.id.sheet_name_text_view);
        viewHolder.rowsCount = (TextView) convertView.findViewById(R.id.rows_count_text_view);
        viewHolder.selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);

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

    final ImportedTable item = getItem(position);
    if (item != null) {
        viewHolder.titleView.setText(item.getTableName());
        int columnsCount = 0;
        for (boolean isSelected : item.getSelectedColumns()) {
            if (isSelected) {
                columnsCount++;
            }
        }
        viewHolder.rowsCount.setText(context.getString(R.string.label_rowscounttext, (item.getRecordsCount() - item.getHeaderRow()), columnsCount));
    }

    final CheckBox isSelectedForImport = (CheckBox) convertView.findViewById(R.id.import_table_list_toggle_button);
    RelativeLayout checkboxLayout = (RelativeLayout) convertView.findViewById(R.id.import_table_list_toggle_layout);
    checkboxLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isSelectedForImport.performClick();
        }
    });
    final TextView selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
    isSelectedForImport.setChecked(item.isSelected());
    if (!isSelectedForImport.isChecked()) {
        convertView.setClickable(true);
        viewHolder.titleView.setTextColor(Color.parseColor("#8A000000"));
    } else {
        convertView.setClickable(false);  viewHolder.titleView.setTextColor(Color.parseColor("#DE000000"));
    }
    isSelectedForImport.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId()) && !isSelectedForImport.isChecked()) {

                isSelectedForImport.setChecked(true);
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                final AlertDialog dialog;

                builder.setMessage(context.getString(R.string.importapplication_appscreen_label_lookupwarningmessage));
                builder.setPositiveButton(context.getString(R.string.ui_label_continue), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (int i = 0; i < item.getColumnType().size(); i++) {
                            removeLookups(item.getSheetId(), item.getTableId(), i);

                        }
                        isSelectedForImport.setChecked(false);
                        item.setSelected(false);
                        dialog.dismiss();
                    }
                });

                builder.setNegativeButton(context.getString(R.string.ui_label_dontcontinue), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog = builder.show();

                notifyDataSetChanged();
            } else if (!isSelectedForImport.isChecked()) {
                item.setSelected(isSelectedForImport.isChecked());
                notifyDataSetChanged();
            } else {
                if (isSelectedForImport.isChecked()) {
                    for (int i = 0; i < item.getColumnType().size(); i++) {
                        if (item.getSelectedColumns().get(i) && item.getColumnType().get(i).equals("SINGLE_LOOKUP")) {
                            enableParents(item.getSheetId(), item.getTableId(), i);
                        }
                    }
                    notifyDataSetChanged();
                }
                item.setSelected(isSelectedForImport.isChecked());
            }
            mCallback.onTableSelectionToggle();
        }
    });

    if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId())) {
        Set<String> children = ((ImportActivity) context).getNewApplication().getChildTableNames(item.getSheetId(), item.getTableId());
        Iterator<String> childIterator = children.iterator();
        String childrenString = childIterator.next();
        while (childIterator.hasNext()) {
            childrenString += ", ";
            childrenString += childIterator.next();
        }
        selectMessage.setText(context.getString(R.string.label_importlookupchildrentext) +" "+ childrenString);
        selectMessage.setVisibility(View.VISIBLE);

    } else {
        selectMessage.setVisibility(View.GONE);
    }

    return convertView;
}

这不会一直发生,小名单也不会发生。我没有其他帖子中描述的其他常见问题(只要没有出现这个故障,所有视图都能正常工作,并且处于正确的顺序)。

XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:paddingLeft="16dp">

    <TextView
        android:id="@+id/sheet_name_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="4dp"
        android:textColor="#DE000000"
        android:textSize="16sp"
        tools:text="Sheet Name" />

    <TextView
        android:id="@+id/rows_count_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#8A000000"
        android:textSize="14sp"
        tools:text="Rows : 3" />
    <TextView
        android:id="@+id/row_info_text"
        android:textSize="12sp"
        android:layout_width="match_parent"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:visibility="gone"
        tools:visibility="gone"
        tools:text="This form is looked up by "/>

</LinearLayout>

<RelativeLayout
    android:id="@+id/import_table_list_toggle_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="19dp"
    android:paddingRight="19dp"
    android:layout_alignParentRight="true">
    <CheckBox
        android:id="@+id/import_table_list_toggle_button"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:paddingBottom="24dp"
        android:paddingTop="24dp" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

正如我提到的问题是RelativeLayout布局。

试试这个......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/sheet_row_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|center_vertical"
            android:descendantFocusability="blocksDescendants"
            android:orientation="horizontal"
            android:weightSum="10">

<LinearLayout
    android:layout_width="0dp"
    android:layout_weight="8"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:paddingLeft="16dp">

    <TextView
        android:id="@+id/sheet_name_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="4dp"
        android:textColor="#DE000000"
        android:textSize="16sp"
        tools:text="Sheet Name"/>

    <TextView
        android:id="@+id/rows_count_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#8A000000"
        android:textSize="14sp"
        tools:text="Rows : 3"/>

    <TextView
        android:id="@+id/row_info_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="12sp"
        android:visibility="gone"
        tools:text="This form is looked up by "
        tools:visibility="gone"/>

</LinearLayout>

<RelativeLayout
    android:id="@+id/import_table_list_toggle_layout"
    android:layout_width="0dp"
    android:layout_weight="2"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:paddingLeft="19dp"
    android:paddingRight="19dp">

    <CheckBox
        android:id="@+id/import_table_list_toggle_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:paddingBottom="24dp"
        android:paddingTop="24dp"/>
</RelativeLayout>