第一行代码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. 布局代码 ``` <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/news_list_fragment" android:name="com.example.myapplication.NewsListFragment" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/news_list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:dividerHeight="0dp"/> </fragment> ``` 2. 主要代码 ``` public class NewsListFragment extends Fragment { private ListView mListView; private List<News> mNewsList; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_news_list, container, false); mListView = view.findViewById(R.id.news_list_view); mNewsList = getNewsListFromDatabase(); //从数据库中获取新闻列表 NewsListAdapter adapter = new NewsListAdapter(getActivity(), mNewsList); mListView.setAdapter(adapter); return view; } private List<News> getNewsListFromDatabase() { List<News> newsList = new ArrayList<>(); //从数据库中获取新闻列表的代码 return newsList; } } ``` 在以上代码中,我们使用了ListView控件来展示新闻列表,并且通过NewsListAdapter适配器将新闻列表与ListView绑定。在NewsListFragment的onCreateView方法中,我们调用getNewsListFromDatabase方法从数据库中获取新闻列表,并将其传递给NewsListAdapter适配器。在getNewsListFromDatabase方法中,我们可以使用SQLite数据库或其他类型的数据库来获取新闻列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值