使用Fragment实现主页面,在该页面中使用RecyclerView显示书名列表
时间: 2023-06-19 17:08:54 浏览: 150
Fragmen与RecyclerView使用
好的,下面是实现的步骤:
1. 创建一个Fragment类,命名为BookListFragment,并在其中创建RecyclerView。
```
public class BookListFragment extends Fragment {
private RecyclerView mRecyclerView;
private BookListAdapter mAdapter;
public BookListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_book_list, container, false);
// 初始化RecyclerView
mRecyclerView = view.findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new BookListAdapter(getActivity(), getBookList());
mRecyclerView.setAdapter(mAdapter);
return view;
}
// 模拟数据,返回书名列表
private List<String> getBookList() {
List<String> list = new ArrayList<>();
list.add("Java编程思想");
list.add("Effective Java");
list.add("Android编程权威指南");
list.add("Kotlin实战");
return list;
}
}
```
2. 创建一个RecyclerView的布局文件,命名为fragment_book_list.xml。
```
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
3. 创建一个BookListAdapter类,继承自RecyclerView.Adapter,并实现ViewHolder。
```
public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.ViewHolder> {
private Context mContext;
private List<String> mBookList;
public BookListAdapter(Context context, List<String> bookList) {
mContext = context;
mBookList = bookList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_book, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String bookName = mBookList.get(position);
holder.mTextView.setText(bookName);
}
@Override
public int getItemCount() {
return mBookList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.tv_book_name);
}
}
}
```
4. 创建RecyclerView的item布局文件,命名为item_book.xml。
```
<TextView
android:id="@+id/tv_book_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp" />
```
5. 在MainActivity中,使用FragmentManager将BookListFragment添加到主页面中。
```
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 将BookListFragment添加到主页面中
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
BookListFragment fragment = new BookListFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
}
}
```
6. 最后,在主页面的布局文件中添加一个FrameLayout,用于显示BookListFragment。
```
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
至此,我们就完成了使用Fragment在主页面中显示RecyclerView的书名列表。
阅读全文