滚动listview和自定义适配器的问题

时间:2018-12-07 16:06:10

标签: android scroll android-listview

我尝试用一​​个按钮创建可滚动的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>

这是此列表视图的活动

<?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创建自定义适配器类

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);
            }
        }

    }

在我编写的活动的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不会出现问题。

该如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

您的活动全部搞砸了。

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

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

在您的activity.xml中,您拥有ConstraintLayout作为parentGroupView,其中包含三个直接的ChildView:

  • LinearLayout
  • ListView
  • BottomNavigationView

您的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>