在片段内设置网格布局的回收者视图/卡片视图

时间:2018-10-08 02:40:36

标签: android android-fragments gridview android-recyclerview cardview

我尝试采用一个程序,该程序使用Recyclerview和card视图在单个片段中显示网格布局(在这种情况下为书籍库)。这是原始方法。它在MainActivity方法中:

public class MainActivity extends AppCompatActivity {

    List<Book> lstBook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lstBook = new ArrayList<>();
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
        lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
        lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
        lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));

        RecyclerView myrv = (RecyclerView) findViewById(R.id.recyclerview_id);
        RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,lstBook);
        myrv.setLayoutManager(new GridLayoutManager(this,3));
        myrv.setAdapter(myAdapter);
    }
}

这就是我试图做的。我正在尝试模仿片段中的同一件事:

public class LibraryFragment extends Fragment {

    List<Book> lstBook;
    RecyclerView myrv;
    private RecyclerView.Adapter myAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.item_library, null);

        lstBook = new ArrayList<>();
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
        lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
        lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
        lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));

    **myrv = (RecyclerView) myrv.findViewById(R.id.my_recycler_view);
        myAdapter = new myAdapter(lstBook);
        myrv.setLayoutManager(new GridLayoutManager(this,3));
        myrv.setAdapter(myAdapter);**
    }
}

我在最后四行代码中遇到了一些麻烦,在这些代码中我无法解析myAdapter和GridLayoutManager不能正确应用。有没有人能解决我的问题?还是有人可以指出我类似的问题?

1 个答案:

答案 0 :(得分:0)

返回之后的任何代码都将无法访问,因此返回应为最后一行。同样在片段中,是不正确的,请将其更改为 getContext() getActivity()。尝试以下代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.item_library, null);

    lstBook = new ArrayList<>();
    lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
    lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
    lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
    lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
    lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));
    lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
    lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
    lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
    lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
    lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));
    lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.thevigitarian));
    lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.thewildrobot));
    lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.mariasemples));
    lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.themartian));
    lstBook.add(new Book("He Died with...","Categorie Book","Description book",R.drawable.hediedwith));

    myrv = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
    myAdapter = new myAdapter(getContext(), lstBook);
    myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
    myrv.setAdapter(myAdapter);

    return rootView;
}

希望有帮助!