【发布时间】:2019-03-12 16:18:10
【问题描述】:
我尝试采用一个程序,该程序使用 Recyclerview 和卡片视图在单个片段中显示网格布局(在本例中为图书库)。这是原始方法。它在 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 没有正确应用。有没有人有任何指针来解决我的问题?或者有人可以指出我类似的问题吗?
【问题讨论】:
标签: android android-fragments gridview android-recyclerview cardview