第一行代码Android 第四章(碎片Fragment,动态加载布局,碎片实践:一个简易版的新闻应用)

第四章:手机平板要兼顾--探究碎片
4.1 碎片是什么
        碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间,因而在平板上应用的非常广泛。
        它和活动很像,同样都能包含布局,同样都有自己的生命周期。
        平板上:新闻标题界面和新闻详细内容界面分别放在两个碎片中,然后在同一个活动中引入这两个碎片。
4.2 碎片的使用方式
        碎片通常都是在平板上使用的,首先应该创建一个平板模拟器。
4.2.1 碎片的简单用法

//在一个活动中添加两个碎片 
//左侧碎片布局 left_fragment.xml
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"
        />
</LinearLayout>
//这个布局,只放置了一个按钮,并让她水平居中显示。
//新建右侧碎片布局
//right_fragment.xml
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="00ff00"  //绿色
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is right fragment"
        />
</LinearLayout>

注意:Fragment有两种
        1)android.app.Fragment 系统内置的
        2)android.support.v4.app.Fragment   support-v4库中的  (建议使用)(这里不需要添加依赖就可以使用,因为build.gradle已经添加了appcompat-v7库的依赖,这个库中包含support-v4) 

//新建一个LeftFragment类,并让他继承自Fragment
public class LeftFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;
    }
}
//同样再新建一个RightFragment
public class RightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.right_fragment,container,false);
        return view;
    }
}

注意:可以通过android:name来制定碎片的类名,一定要把包名也加上 

//接下来修改activity_main中的代码(活动)
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <fragment
        android:id="@+id/right_fragment"
        android:name="com.example.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

4.2.2 动态添加碎片
        碎片的真正强大之处在于它可以在程序运行时动态的添加到活动中

//在以上代码中继续完善,新建another_right_fragment.xml
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="ffff00"  
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is another right fragment"
        />
</LinearLayout>
//新建一个AnotherRightFragment类作为另一个右侧碎片
public class LeftFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.another_right_fragment,container,false);
        return view;
    }
}
//接下来修改activity_main中的代码(活动)
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我们可以按照以下步骤实现一个简易版新闻应用: 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 应用程序。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值