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

时间:2018-11-23 10:50:28

标签: android xml android-fragments grid-layout

当我将LinearLayoutManagar更改为GridLayoutManager时,我面临此问题。 我希望列表位于网格视图而不是列表视图中。 错误来自getActivity()

错误:

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)

这是我的代码。

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


}

我的适配器

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) {

        }
    }
}

书签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>

网格项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>

5 个答案:

答案 0 :(得分:2)

用下面的代码替换您的代码。您的代码失败,因为Constructor需要两个参数,Context-> 活动上下文int-列数

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

答案 1 :(得分:1)

GridLayoutManager需要多个参数,请尝试使用其构造函数之一:

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

答案 2 :(得分:1)

更改此行代码:

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

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

答案 3 :(得分:0)

尝试这样

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

答案 4 :(得分:0)

在bookmark.xml文件中添加布局管理器

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

从活动中删除setLlayoutManager