滚动列表视图和自定义适配器的问题

Problem with scrolling listview and custom adapter

我尝试用一​​个按钮创建一个可滚动的 listview

这是我的 lis_issue.xml 文件。

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

    <TextView
        android:id="@+id/txtName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="14dp"
        android:layout_weight="2"
        android:text="Nazwa"
        android:textColor="@color/colorFontBlack"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/txtAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal|fill"
        android:paddingLeft="10dp"
        android:text="Ilość"
        android:textColor="@color/colorFontBlack"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/txtUnit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal|fill"
        android:paddingLeft="10dp"
        android:text="Jednostka"
        android:textColor="@color/colorFontBlack"
        android:textSize="20sp" />

    <ImageButton
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_weight="0.3"
        android:background="@color/colorBackground"
        android:src="@drawable/ic_close_red" />
    </LinearLayout>

这里是 activity 这个列表视图

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    tools:context=".ProductsIssueActivity">

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingStart="16dp"
        android:paddingEnd="16dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/txtViewBarcode"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:text="Kod"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/edtBarcode"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:inputType="number"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/ivBarcode"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_gravity="center_horizontal|center"
                app:srcCompat="@drawable/ic_barcode" />


        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/txtViewName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:text="Nazwa"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/edtName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:maxLines="1"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />

            <Space
                android:layout_width="40dp"
                android:layout_height="wrap_content" />


        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/txtViewAmount"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:text="Ilość"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/edtAmount"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:inputType="number"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />

            <Space
                android:layout_width="40dp"
                android:layout_height="wrap_content" />


        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/txtViewUnit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:text="Jednostka"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />

            <Spinner
                android:id="@+id/spinnerUnit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp"></Spinner>

            <Space
                android:layout_width="40dp"
                android:layout_height="wrap_content" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/txtViewStorehouse"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:text="Magazyn"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />


            <Spinner

                android:id="@+id/spinnerStorehouse"

                android:layout_width="0dp"
                android:layout_height="wrap_content"

                android:layout_weight="0.5"
                android:textSize="20sp"></Spinner>

            <Space
                android:layout_width="40dp"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/txtViewEmployee"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:text="Pobierający"
                android:textColor="@color/colorFontBlack"
                android:textSize="20sp" />


            <Spinner

                android:id="@+id/spinnerEmployee"

                android:layout_width="0dp"
                android:layout_height="wrap_content"

                android:layout_weight="0.5"
                android:textSize="20sp"></Spinner>

            <Space
                android:layout_width="40dp"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <Space
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <LinearLayout
            android:id="@+id/linearLayoutConfirm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible">

            <Button
                android:id="@+id/btnConfirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/button_blue_small_selector"
                android:drawableLeft="@drawable/ic_pdf_black_24dp"
                android:paddingLeft="10dp"
                android:text="Raport"
                android:textColor="@color/colorFont"
                android:textSize="20sp"
                android:visibility="visible" />


            <Space
                android:id="@+id/space"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <Button
                android:id="@+id/btnAddIssue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/button_blue_small_selector"
                android:drawableLeft="@drawable/ic_add"
                android:paddingLeft="10dp"
                android:text="Dodaj "
                android:textColor="@color/colorFont"
                android:textSize="20sp"
                android:visibility="visible" />

        </LinearLayout>

    </LinearLayout>

    <ListView
        android:id="@+id/listViewIssue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"

        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ll" />


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorBackground"
        app:itemIconTint="@color/nav_item"
        app:itemTextColor="@color/nav_item"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

我为我的 listview 创建自定义 适配器 class

public class IssueProductAdapter extends BaseAdapter {

        public ArrayList<IssueProduct> productslist;
        public Context context;

        private IssueProductAdapter(Context context, ArrayList<IssueProduct> products) {
            this.productslist = products;
            this.context = context;
        }

        @Override
        public int getCount() {
            return productslist.size();
        }

        @Override
        public Object getItem(int i) {
            return productslist.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

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

            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).
                        inflate(R.layout.list_issue, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }


            IssueProduct currentProduct = (IssueProduct) getItem(position);


            viewHolder.txtName.setText(currentProduct.getName());
            viewHolder.txtAmount.setText(String.valueOf(currentProduct.getAmount()));
            viewHolder.txtUnit.setText(currentProduct.getUnit());


            return convertView;
        }


        private class ViewHolder {
            TextView txtName;
            TextView txtAmount;
            TextView txtUnit;
            ImageButton btnDelete;

            public ViewHolder(View view) {
                txtName = (TextView) view.findViewById(R.id.txtName);
                txtAmount = (TextView) view.findViewById(R.id.txtAmount);
                txtUnit = (TextView) view.findViewById(R.id.txtUnit);
                btnDelete = (ImageButton) view.findViewById(R.id.btnDelete);
            }
        }

    }

在 activity 的 onCreate 方法中,我写了

 listViewResult = (ListView) findViewById(R.id.listViewIssue);adapterIssueProdut = new IssueProductAdapter(this, addedProductsArrayList);
        listViewResult.setAdapter(adapterIssueProdut);
adapterIssueProdut.registerDataSetObserver(observerIssueProduct);

我写了一个 观察者 用于 listview 变化

DataSetObserver observerIssueProduct = new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            //mDbAdapter.open();

            addedProductsArrayList.clear();
            addedProductsArrayList = mDbAdapter.fetchAllIssuesinArrayList();
            adapterIssueProdut = new IssueProductAdapter(ProductsIssueActivity.this, addedProductsArrayList);

            listViewResult.setAdapter(adapterIssueProdut);
            adapterIssueProdut.registerDataSetObserver(observerIssueProduct);

        }
};

当我在没有按钮的情况下使用 listview 时,listview 正在滚动。但是当我在 listview 行添加一个按钮时,这个 listview 不会滚动。

我该怎么做才能解决我的问题?

你的 Activity 一团糟。

您正在使用 ConstraintLayout,因此您必须确保对 ConstraintLayout 的每个 ChildView 都设置了约束。

For more Information About ConstraintLayout check link below enter link description here

在您的 activity.xml 情况下,您有 ConstraintLayout 作为包含三个直接 ChildView 的 parentGroupView:

  • 线性布局
  • 列表视图
  • 底部导航视图

这里是您的 acticity.xml 代码:

    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        ... your Code 
    </LinearLayout>

    <ListView
        android:id="@+id/listViewIssue"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ll" />


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@color/colorBackground"
        app:itemIconTint="@color/nav_item"
        app:itemTextColor="@color/nav_item"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/listViewIssue"
        app:layout_constraintVertical_bias="1.0"
        app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>