alkincakiralar
alkincakiralar

Reputation: 33

Android gridview in fragment can't scrolling

I have one page called products. When page is loaded it shows productsfragment. This fragment has one RecyclerView which doesn't have any problem. On selecting a product a new fragment opens up and shows the list of product prices fragment. This fragment has one RecyclerView and the scrolling here doesn't works.

I tried gridview and cardview but they too are not scrolling. I tried gridview in scroll view or nested scroll view, they are also not scrolling.

here is my fragment with RecyclerView which is not scrolling

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sorderfragment_step1_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Activities.OrderSelect.Fragments.ProductSingleOrder.SOrderFragment_Step1">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/gvProductPrices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

and its my RecyclerView item view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:gravity="center"
    android:maxHeight="200dp"
    android:minHeight="200dp"
    android:background="@layout/border2"
    android:padding="0dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtPriceName"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="Kucuk Boy Whopper"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout4"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="0dp"
        android:background="#000000"
        app:layout_constraintBottom_toTopOf="@+id/txtPrice"
        app:layout_constraintEnd_toEndOf="parent">

    </android.support.constraint.ConstraintLayout>

    <TextView
        android:id="@+id/txtPrice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:gravity="center"
        android:text="14.99"
        android:textColor="#000000"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</LinearLayout>

and my adapter

class PriceAdapter(val priceList : List<ProductPrice>,
                   val context: Context) : RecyclerView.Adapter<ViewHolder>() {
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.gv_product_prices, p0, false))
    }

    override fun getItemCount(): Int {
        return priceList.size
    }

    override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
        val price = priceList[p1]
        p0.txtPriceName.text = price.getDescription()
        p0.txtPriceName.isAllCaps = true
        p0.txtPrice.text = BaseHelper.getNumericStr(price.getPrice())
    }

}

class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
    val txtPriceName = view.txtPriceName!!
    val txtPrice = view.txtPrice!!
}

and my fragment code

    class SOrderFragment_Step1 : Fragment() {
    private var _priceRepository = ProductPriceRepository(OrderRepository.orderSelect!!)
    private var priceAdapter: PriceAdapter? = null
    private var prices: List<ProductPrice>? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_sorder_step1, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        configurePrices()
    }

    private fun configurePrices() {
        prices = _priceRepository.getPriceByProductId(OrderRepository.currentProduct!!.getId())
        gvProductPrices.layoutManager = GridLayoutManager(OrderRepository.orderSelect!!, 2)
        gvProductPrices.adapter = PriceAdapter(prices!!, this, OrderRepository.orderSelect!!)
    }

    fun priceSelect(price: ProductPrice) {
        Toast.makeText(OrderRepository.orderSelect!!, price.getDescription(), Toast.LENGTH_LONG).show()
    }

}

and also fragment change like this

val manager = this.fragmentManager!!.beginTransaction()
            manager.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
            manager.replace(R.id.fragmentProductSteps, SOrderFragment_Step1(), "detailFragment").commit()

and fragment base activity xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/order_select_layout"
    tools:context=".OrderSelect">

    <GridView
        android:id="@+id/gvProductGroups"
        android:layout_width="165dp"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:scrollbars="none"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="225dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="7">


            <include
                layout="@layout/activity_advertise_partition"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </android.support.constraint.ConstraintLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/orderProductLayout"
        android:layout_width="0dp"
        android:layout_height="83dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@layout/gv_order_products_border"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnCancelCurrentOrder2"
        app:layout_constraintStart_toEndOf="@+id/gvProductGroups">


        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="5dp"
            android:id="@+id/paymentView"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/txtTotalL"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TOPLAM :"
                android:textColor="#bc3030"
                android:textSize="24sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/txtPriceL"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="-1dp"
                android:layout_weight="1"
                android:gravity="left|center"
                android:text="₺ 0.00"
                android:textColor="#000000"
                android:textSize="24sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/imgOrderButton"
                app:layout_constraintStart_toEndOf="@+id/txtTotalL"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0" />

            <Button
                android:id="@+id/imgOrderButton"
                android:layout_width="175dp"
                android:layout_height="0dp"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:background="#15912e"
                android:padding="15sp"
                android:text="Sepeti Goster"
                android:textColor="#FFFFFF"
                android:textSize="18sp"
                android:textStyle="bold"
                android:visibility="invisible"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>

    </LinearLayout>

    <fragment
        android:id="@+id/fragmentProductSteps"
        android:name="com.example.alknakralar.kios.Helpers.BlankFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/orderProductLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/gvProductGroups"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <Button
        android:id="@+id/btnCancelCurrentOrder2"
        android:layout_width="125dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="#ff0000"
        android:text="SEPETI BOSALT"
        android:textColor="#FFFFFF"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imgBackFromProducts"
        app:layout_constraintTop_toBottomOf="@+id/fragmentProductSteps" />

    <ImageView
        android:id="@+id/imgBackFromProducts"
        android:layout_width="70dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragmentProductSteps"
        app:srcCompat="@drawable/back" />

</android.support.constraint.ConstraintLayout>

How can i fix this issue ?

Upvotes: 0

Views: 545

Answers (1)

Jaykishan Sewak
Jaykishan Sewak

Reputation: 822

Sugession: Why are you using grid view and setting adapter to it where you have another approach and commonly used approach.

Which is to use recycler view and set its layout manager as GridLayout manager

Below single line will make your job done

recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));

Upvotes: 3

Related Questions