简体   繁体   中英

Cannot resolve constructor 'GridLayoutManager(android.support.v4.app.FragmentActivity)

I am facing this problem when I change the LinearLayoutManagar to GridLayoutManager. I want the list to be in the grid view not list view. The error comes in the getActivity()

error:

no suitable constructor found for GridLayoutManager(FragmentActivity)
    constructor GridLayoutManager.GridLayoutManager(Context,AttributeSet,int,int) is not applicable
    (actual and formal argument lists differ in length)
    constructor GridLayoutManager.GridLayoutManager(Context,int) is not applicable
    (actual and formal argument lists differ in length)
    constructor GridLayoutManager.GridLayoutManager(Context,int,int,boolean) is not applicable
    (actual and formal argument lists differ in length)

This is my code.

public class FragmentBookmark extends Fragment {
    View paramView;
    MyAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        paramView = inflater.inflate(R.layout.bookmark, container, false);
        RecyclerView recyclerView = (RecyclerView) paramView.findViewById(R.id.listRecyclerView);

        MyAdapter adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        return paramView;
    }


}

My Adapter

public class MyAdapter extends RecyclerView.Adapter {


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
       View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.grid_item, viewGroup, false);
       return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((ListViewHolder) viewHolder).bindView(i);
    }

    @Override
    public int getItemCount() {
        return OurData.title.length;
    }

    private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView mItemText;
        private ImageView mItemImage;

        public ListViewHolder(View itemView) {
            super(itemView);
            mItemText = (TextView) itemView.findViewById(R.id.tv_name);
            mItemImage = (ImageView) itemView.findViewById(R.id.imageview);
            itemView.setOnClickListener(this);
        }

       public void bindView(int position) {
            mItemText.setText(OurData.title[position]);
            mItemImage.setImageResource(OurData.picture[position]);

       }

        @Override
        public void onClick(View v) {

        }
    }
}

bookmark xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/listRecyclerView"
    android:layout_width="match_parent"

    android:layout_height="wrap_content">


</android.support.v7.widget.RecyclerView>

grid item xml

<LinearLayout 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="wrap_content"
    app:cardElevation="4dp"
    android:id="@+id/recyclerView">

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:contentDescription="@null" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:maxLines="1"
        android:text="newText"
        android:singleLine="true" />

</LinearLayout>

Replace your code with below code. Your code is failing because Constructor expects two params, Context -> Activity context and int - No of columns .

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

GridLayoutManager expects more than one argument, try one of its constructors:

int num_columns = 2;
new GridLayoutManager(getActivity(), num_columns);

change this line of your code :

 RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity());
 recyclerView.setLayoutManager(layoutManager);

to

 RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),numberOfYourColumns);
 recyclerView.setLayoutManager(layoutManager);

try like this

 // Initializing recyclerview
        RecyclerView recyclerViewItems = findViewById(R.id.rv_feeds);
        //setting no of cols. orientation using GridLayoutManager
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2, LinearLayout.VERTICAL, false);
        recyclerViewItems.setLayoutManager(gridLayoutManager);

        //passing data to adapter
        FeedsRecycleViewAdapter adapter = new FeedsRecycleViewAdapter(this, myImageList, myBrands, myPrice, myActPrice);

Add layout manager in bookmark.xml file

xmlns:app="http:// schemas.android.com/apk/res-auto" app:layoutManager="android.support.v7.widget.GridLayoutManager" app:spanCount="2"`

Remove setLlayoutManager from activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM