简体   繁体   English

无法解析构造函数'GridLayoutManager(android.support.v4.app.FragmentActivity)

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

I am facing this problem when I change the LinearLayoutManagar to GridLayoutManager. 当我将LinearLayoutManagar更改为GridLayoutManager时,我遇到了这个问题。 I want the list to be in the grid view not list view. 我希望列表位于网格视图而不是列表视图中。 The error comes in the getActivity() 错误来自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

<?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 网格项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 . 您的代码失败,因为Constructor需要两个参数,即Context > Activity上下文int - No columns

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

GridLayoutManager expects more than one argument, try one of its constructors: GridLayoutManager需要多个参数,请尝试使用其构造函数之一:

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 在bookmark.xml文件中添加布局管理器

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

Remove setLlayoutManager from activity 从活动中删除setLlayoutManager

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在ViewModelProviders中解析android.support.v4.app.FragmentActivity的方法 - Cannot resolve method of android.support.v4.app.FragmentActivity in ViewModelProviders 无法将ContraceptiveFragment中的ContraceptiveFragment()应用于(android.support.v4.app.FragmentActivity) - ContraceptiveFragment( ) in ContraceptiveFragment cannot be applied to (android.support.v4.app.FragmentActivity) 无法将clcik上的android.app.Application强制转换为android.support.v4.app.FragmentActivity - android.app.Application cannot be cast to android.support.v4.app.FragmentActivity on clcik android.support.v4.app.FragmentActivity扩展了什么? - What does android.support.v4.app.FragmentActivity extend? android.support.v4.app.FragmentActivity无法解析 - android.support.v4.app.FragmentActivity can not be resolved Android错误-不可转换类型; 无法转换&#39;android.support.v4.app.FragmentActivity&#39; - Android Error - Inconvertible types; cannot cast 'android.support.v4.app.FragmentActivity' android.support.v4.app.FragmentActivity缺少操作栏 - android.support.v4.app.FragmentActivity missing action bar 迁移到androidx时无法访问类android.support.v4.app.FragmentActivity - cannot access class android.support.v4.app.FragmentActivity when i migrate to androidx “TAG”在“android.support.v4.app.FragmentActivity”中有私有访问权限 - 'TAG' has private access in 'android.support.v4.app.FragmentActivity' “无法找到类'android.support.v4.app.FragmentActivity'”错误 - “Could not find class 'android.support.v4.app.FragmentActivity'” Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM