Я пытаюсь принять программу, которая использует 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, которые не были правильно применены,У кого-нибудь есть какие-нибудь указатели для решения моей проблемы?Или кто-то может указать мне на подобную проблему?