利用java实现《第一行代码》中Fragment一个简易版的新闻应用android
时间: 2024-05-02 08:17:10 浏览: 133
java_android_Fragment管理类的标准代码
好的,我们可以按照以下步骤实现一个简易版的新闻应用:
1. 创建一个新的 Android 项目,并在 MainActivity 中添加用于显示 Fragment 的布局:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
```
```xml
<!-- activity_main.xml -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 创建 NewsFragment 类,并在其中添加用于显示新闻列表的 RecyclerView:
```java
public class NewsFragment extends Fragment {
private RecyclerView mRecyclerView;
private NewsAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new NewsAdapter();
mRecyclerView.setAdapter(mAdapter);
return view;
}
private class NewsHolder extends RecyclerView.ViewHolder {
private TextView mTitleTextView;
private TextView mDateTextView;
public NewsHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_news, parent, false));
mTitleTextView = itemView.findViewById(R.id.news_title);
mDateTextView = itemView.findViewById(R.id.news_date);
}
public void bind(News news) {
mTitleTextView.setText(news.getTitle());
mDateTextView.setText(news.getDate());
}
}
private class NewsAdapter extends RecyclerView.Adapter<NewsHolder> {
private List<News> mNewsList = new ArrayList<>();
public void setNewsList(List<News> newsList) {
mNewsList = newsList;
}
@Override
public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new NewsHolder(layoutInflater, parent);
}
@Override
public void onBindViewHolder(NewsHolder holder, int position) {
News news = mNewsList.get(position);
holder.bind(news);
}
@Override
public int getItemCount() {
return mNewsList.size();
}
}
}
```
```xml
<!-- fragment_news.xml -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
```xml
<!-- list_item_news.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:id="@+id/news_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceSmall" />
</LinearLayout>
```
3. 创建 News 类,用于保存新闻的标题和日期:
```java
public class News {
private String mTitle;
private String mDate;
public News(String title, String date) {
mTitle = title;
mDate = date;
}
public String getTitle() {
return mTitle;
}
public String getDate() {
return mDate;
}
}
```
4. 在 MainActivity 中添加用于显示 NewsFragment 的代码:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.container);
if (fragment == null) {
fragment = new NewsFragment();
fm.beginTransaction()
.add(R.id.container, fragment)
.commit();
}
}
}
```
5. 在 NewsFragment 中添加一个用于生成假数据的方法,并在 onCreateView 方法中调用它:
```java
public class NewsFragment extends Fragment {
// ...
private void generateNewsList() {
List<News> newsList = new ArrayList<>();
newsList.add(new News("Title 1", "2021-01-01"));
newsList.add(new News("Title 2", "2021-01-02"));
newsList.add(new News("Title 3", "2021-01-03"));
newsList.add(new News("Title 4", "2021-01-04"));
newsList.add(new News("Title 5", "2021-01-05"));
mAdapter.setNewsList(newsList);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new NewsAdapter();
mRecyclerView.setAdapter(mAdapter);
generateNewsList();
return view;
}
// ...
}
```
现在,我们可以运行应用程序,看到一个简单的新闻列表。这只是一个很简单的例子,但它展示了如何使用 Fragment 和 RecyclerView 构建一个 Android 应用程序。
阅读全文