《第一行代码》——碎片的最佳实践:一个简易版的新闻应用

《第一行代码》——碎片的最佳实践:一个简易版的新闻应用

  最近在学习碎片,碎片的目的实际上是为了让手机和平板共享一份的代码,碎片可以保证做出兼容手机和平板的应用程序。这个文章就是用来介绍郭霖《第一行代码》中简易版本的新闻应用的代码的。

一、具体介绍

1、界面

  手机采用的是单页模式,如下所示:
  

单页模式的新闻列表界面
  
单页模式的新闻内容界面

  而ipad采用的是双页模式,如下所示:

  

单页模式的新闻内容界面

2、大体介绍

   ∙ \bullet  对单页模式和双页模式来讲,最大的不同在于单页模式中的新闻列表和新闻内容是存在于两个界面中的,而双页模式的新闻标题和内容界面是存在于一个界面中的,因此在双页模式中必须采用碎片的方式才可以完成。
   ∙ \bullet  单页模式和双页模式所运行的主界面XML也不同,因此在活动运行的过程中,需要使用最小宽度限定符来解决单页双页的判断问题。

二、XML代码详解

1.News类

  首先需要准备好一个新闻的实体类,类当中保存了新闻的标题和内容:

public class News {//准备一个新闻实体类
    private String title;//新闻标题
    private String content;//新闻内容

    public String getTitle() {
        return title;
    }
    public void setTitle(String title){
        this.title=title;
    }
    public String getContent(){
        return content;
    }
    public void setContent(String content){
        this.content=content;
    }
}

2.单页/双页模式的XML布局文件 ——news_content_frag.xml

  我们需要构建一个用于 ipad的双页模式的XML碎片布局文件news_content_frag.xml,用于双页模式的新闻列表和新闻内容及标题的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/visibility_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="invisible">

        <TextView
            android:id="@+id/news_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:textSize="20sp"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black"/>
        <TextView
            android:id="@+id/news_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:padding="15dp"
            android:textSize="18sp"/>
    </LinearLayout>
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:background="@color/black"/>

</RelativeLayout>

  布局当中用了一个相对布局,最下面的VIew作用是将新闻列表与新闻(新闻标题和新闻内容)进行分隔开,用一个靠左的竖线分隔开,当然,这里面并未对新闻列表进行布局,单纯的只是对新闻标题和新闻内容进行一个布局,然后新闻标题和新闻内容用一个横线隔开。

3.双页模式和单页模式的布局加载类——NewsContentFragment

  碎片xml布局建立好以后,我们还需要为其建立一个布局加载类,以便将刚才定义的news_content_frag动态加载进来,这样在后续的XML布局文件中可以将此布局作为碎片布局引进去,因此我们建立了一个NewsContentFragment类:

public class NewsContentFragment extends Fragment {
    private View view;
    //重写Fragment的onCreateView()方法,在这个方法中通过inflate()将定义的新闻内容布局动态加载进来
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        view=inflater.inflate(R.layout.news_content_frag,container,false);
        return view;
    }

    //将新闻的标题和内容显示在界面上
    public void refresh(String newsTitle,String newsContent){
        //找到新闻内容线性布局id
        View visibilityLayout=view.findViewById(R.id.visibility_layout);
        //设置为布局可见
        visibilityLayout.setVisibility(View.VISIBLE);
        //获取到新闻标题和内容的控件
        TextView newsTitleText=view.findViewById(R.id.news_title);
        TextView newsContentText=view.findViewById(R.id.news_content);
        //将传递进来的标题和内容添加到控件当中去
        newsTitleText.setText(newsTitle);//刷新新闻的标题
        newsContentText.setText(newsContent);//刷新新闻的内容
    }
}

  在这个类当中,重写了继承的Fragment类中的onCreateView()方法,目的就是为了把新闻的布局动态的加载进来,同时还创建了一个refresh()方法,这样就可以获取到创建的新闻布局中的新闻标题和新闻内容的实例了,同时也可以对不同新闻列表中的新闻进行新闻内容和标题的加载与更新。

3.单页模式的XML布局——news_content.xml

  上述的一切都是建立在双页模式当中的,如果我们要为手机进行一个单页模式的新闻(包含新闻标题和新闻内容)的布局设置,则需要创建一个新的布局news_content.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/news_content_fragment"
        android:name="com.shuting.news_fragment.Fragment.NewsContentFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

  这里面重点是我们通过一个 < < <fragment > > >标签在单页模式的新闻布局中添加碎片,并且通过name属性来显式指明要添加的碎片类名NewsContentFragment,这其实就是说明新闻布局是在单页模式和双页模式共享的。

4.单页模式的活动类——NewsContentActivity

  我们单页模式的新闻布局界面已经做好了,因此需要在主活动中来进行调用:

public class NewsContentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_content);
        String newsTitle=getIntent().getStringExtra("news_title");//获取传入的新闻标题
        String newsContent=getIntent().getStringExtra("news_content");//获取传入的新闻内容
        //在活动中调用碎片的方法,能够在活动中得到相应碎片的实例,然后就可以调用碎片里面的方法
        //这里是获得了碎片单页模式碎片的实例
        //getFragmentManager()所得到的是所在fragment 的父容器的管理器
        NewsContentFragment newsContentFragment=(NewsContentFragment)
                getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
        //刷新NewsContentFragment界面
        newsContentFragment.refresh(newsTitle,newsContent);
    }

    public static void actionStart(Context context,String newsTitle,String newsContent){
        Intent intent=new Intent(context,NewsContentActivity.class);
        intent.putExtra("news_title",newsTitle);
        intent.putExtra("news_content",newsContent);
        context.startActivity(intent);
    }
}

  代码中的onCreate()方法中我们首先采用了一个actionStart()方法进行启动活动。这个方法能够完成Intent的构建,并且NewsContentActivity中需要的数据都是通过actionStart()方法的参数传递过来的,并且存储到了intent中,最后调用startActivity()方法来启动NewsContentActivity类同时还简化了启动活动的代码,用如下代码就可以进行启动:

NewsContentActivity。actionStart(getActivity(),"getTitle()","getContent()");

  然后在onCreate()方法中加载了news_content布局(新闻布局),同时还通过intent获取到传入的新闻标题和新闻内容,然后调用FragmentManager的findFragmentById()方法得到了NewsContentFragment的实例,其实种方法就是在活动中得到相应碎片的实例,然后用来调用碎片里面的方法,而在碎片布局加载类NewsContentFragment中有一个刷新界面的方法,这样我们就可以将新闻的标题和内容展现在布局上面了。

4.单页和双页的新闻列表布局——news_title_frag.xml

  我们将新闻(包括新闻标题和新闻内容)的布局做好之后,我们还需要加入新闻列表布局,因此我们新建一个news_title_frag.xml,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/news_title_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

  在这里面由于新闻标题会出现多个,因此会出现不同标题对应布局复用的现象,因此我们需要采用RecyclerView来显示新闻列表,同时还需要创建一个列表中标题的布局(列表中有多个子项标题),则新建一个news_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/news_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="end"
    android:textSize="18sp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"/>

  在这里面android:singleLine="true"的目的是为了让TextView进行单行显示,android:ellipsize="end"用于设定当文本内容超出控件快递时文本的缩略方式,这里指定成end表示在尾部进行缩略。这样就为子项标题创建好了布局方式,由于新闻列表和子项标题的布局都创建好了,其实也都是碎片的形式,因此我们需要创建一个布局类用于展示新闻列表,但是在创建之前,我们需要知道,因为列表布局时对应着双页和单页模式的,因此我们需要判断当前对应的是哪种模式,因此我们需要采用限定符操作来实现单双页判断。

5.单页和双页判断——限定符布局activity_main.xml

  我们要为单页模式和双页模式创建不同的activity_main.xml,单页模式的则采用原始的activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/news_title_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/news_title_fragment"
        android:name="com.shuting.news_fragment.Fragment.NewsTitleFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

  而双页模式则需要新建一个layout-sw600dp文件夹,在此文件夹下面新建一个activity_main.xml文件,代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/news_title_fragment"
        android:name="com.shuting.news_fragment.Fragment.NewsTitleFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/news_content_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">

        <fragment
            android:id="@+id/news_content_fragment"
            android:name="com.shuting.news_fragment.Fragment.NewsContentFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

</LinearLayout>

  我们可以看到,在单页模式中只引入了放置新闻列表的碎片,而在双页模式中引入了两个碎片,分别对应着新闻列表和新闻(新闻内容和标题)的布局,因此在程序运行的开始,如果是单页模式,则只显示新闻列表,否则则会将新闻列表和新闻的布局同时加载出来,可以看到双页模式是一个水平布局,则就可以有第一部分的那种界面显示了,这个时候我们添加了一个FrameLayout布局,这个布局的id是news_content_fragment,用来判断是单页还是双页,能够找到此id就是就是双页模式。

6.单页和双页新闻列表布局加载类——NewsTitleFragment

  首先我们采用onCreateView()方法将新闻列表布局动态的加载进来:

public class NewsTitleFragment extends Fragment {
	private boolean isTwoPane;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.news_title_frag,container,false);
        RecyclerView recyclerView=(RecyclerView) view.findViewById(R.id.news_title_recycler_view);
        return view;
    }
    public void onActivityCreated(Bundle savedINstanceState){
        super.onActivityCreated(savedINstanceState);
        //获得和碎片相关联的活动的实例
        if(getActivity().findViewById(R.id.news_content_layout)!=null){
            isTwoPane=true;//可以找到news_content_layout布局时,为双页模式
        }else{
            isTwoPane=false;//找不到此布局时为单页模式
        }
    }

  我们重写onActivityCreated()方法,这个方法是通过在活动中能否找到id为news_content_layout来判断当前是双页模式还是单页模式,一开始运行的就是活动。首先用getActivity()从碎片当中调用活动里的方法,然后通过findViewById获取到R.id.news_content_layout,这个id号是在layout-sw600dp/activity_main.xml中的FrameLayout对应的,并且在此布局当中放置了新闻碎片,因此如果能够找到这个布局id证明就是双页模式。
  除此之外,我们之前创建为新闻列表创建了RecyclerView控件,则需要存放多个新闻标题,这个时候我们需要创建一个LIst来存放多个标题,并且还需要为标题建立适配器:

public class NewsTitleFragment extends Fragment {
    private List<News> newsList=new ArrayList<>();
    class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{
        private List<News> mNewsList;
        class ViewHolder extends RecyclerView.ViewHolder{
            TextView newsTitleText;

            public ViewHolder(View view){
                super(view);
                newsTitleText=(TextView)view.findViewById(R.id.news_title);
            }
        }

  在类内创建一个类中类ViewHolder,主要是用来加载新闻标题的实例。

        public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
            View view=LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.news_item,parent,false);
            final ViewHolder holder=new ViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    News news=mNewsList.get(holder.getAdapterPosition());
                    if(isTwoPane){
                        //如果是双页模式,则刷新NewsContentFragment中的内容
                        NewsContentFragment newsContentFragment=
                                (NewsContentFragment)getFragmentManager()
                                .findFragmentById(R.id.news_content_fragment);
                        newsContentFragment.refresh(news.getTitle(),news.getContent());
                    }else{
                        //如果是单页模式,则直接启动NesContentActivity
                        //采用getActivity()来得到和当前碎片相关联的活动实例
                        NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
                    }
                }
            });
            return holder;
        }

  接着我们重写onCreateViewHolder()方法,首先通过LayoutInflater.from()将RecyclerView子项获取到,即对应的新闻列表里面新闻标题的布局动态的加载进来,然后通过view.setOnClickListener()来获取到点击项的News实例,再通过isTwoPane来判断当前是单页模式还是双页模式,如果是双页模式,我们则获取到NewsContentFragment实例,目的是用来调用碎片里面的refresh方法——将新闻标题和新闻内容进行刷新;如果是单页模式,则直接采用actionStart跳转到新的新闻界面,即点击了新闻列表中的某个新闻标题,则跳转到这个新闻标题所对应的新闻内容中去。
  接着我们需要向RecyclerView中填充数据:

    public void initNews(){
        for(int i=1;i<=5;i++){
            News news=new News();
            news.setTitle("This is news title"+i);
            news.setContent(getRandomLengthContent("This is news content"+i+"."));
            newsList.add(news);
        }
    }
    private String getRandomLengthContent(String content){
        Random random=new Random();
        int length=random.nextInt(20)+1;//长度为1-20
        StringBuilder builder=new StringBuilder();
        for(int i=0;i<length;i++){
            builder.append(content);
        }
        return builder.toString();
    }

  我们将创建的新闻实例添加标题和内容,并且将新闻实例添加到newsList列表当中去,采用getRandomLengthContent()方法来随机初始化新闻内容的长度,接着需要在碎片当中加载RecyclerView布局,并且创建线性布局并将适配器加载进去:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.news_title_frag,container,false);
        RecyclerView recyclerView=(RecyclerView) view.findViewById(R.id.news_title_recycler_view);
        initNews();
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        NewsAdapter newsAdapter=new NewsAdapter(newsList);
        recyclerView.setAdapter(newsAdapter);
        return view;
    }

  因此NewsTitlerFragment的类的整体代码如下所示:

public class NewsTitleFragment extends Fragment {
    private List<News> newsList=new ArrayList<>();
    private boolean isTwoPane;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.news_title_frag,container,false);
        RecyclerView recyclerView=(RecyclerView) view.findViewById(R.id.news_title_recycler_view);
        initNews();
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        NewsAdapter newsAdapter=new NewsAdapter(newsList);
        recyclerView.setAdapter(newsAdapter);
        return view;
    }
    public void initNews(){
        for(int i=1;i<=5;i++){
            News news=new News();
            news.setTitle("This is news title"+i);
            news.setContent(getRandomLengthContent("This is news content"+i+"."));
            newsList.add(news);
        }
    }
    private String getRandomLengthContent(String content){
        Random random=new Random();
        int length=random.nextInt(20)+1;//长度为1-20
        StringBuilder builder=new StringBuilder();
        for(int i=0;i<length;i++){
            builder.append(content);
        }
        return builder.toString();
    }

    public void onActivityCreated(Bundle savedINstanceState){
        super.onActivityCreated(savedINstanceState);
        //获得和碎片相关联的活动的实例
        if(getActivity().findViewById(R.id.news_content_layout)!=null){
            isTwoPane=true;//可以找到news_content_layout布局时,为双页模式
        }else{
            isTwoPane=false;//找不到此布局时为单页模式
        }
    }

    class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{
        private List<News> mNewsList;
        class ViewHolder extends RecyclerView.ViewHolder{
            TextView newsTitleText;

            public ViewHolder(View view){
                super(view);
                newsTitleText=(TextView)view.findViewById(R.id.news_title);
            }
        }
        public NewsAdapter(List<News> newsList){
            mNewsList=newsList;
        }
        public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
            View view=LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.news_item,parent,false);
            final ViewHolder holder=new ViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    News news=mNewsList.get(holder.getAdapterPosition());
                    if(isTwoPane){
                        //如果是双页模式,则刷新NewsContentFragment中的内容
                        NewsContentFragment newsContentFragment=
                                (NewsContentFragment)getFragmentManager()
                                .findFragmentById(R.id.news_content_fragment);
                        newsContentFragment.refresh(news.getTitle(),news.getContent());
                    }else{
                        //如果是单页模式,则直接启动NesContentActivity
                        //采用getActivity()来得到和当前碎片相关联的活动实例
                        NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
                    }
                }
            });
            return holder;
        }
        public void onBindViewHolder(ViewHolder holder,int position){
            News news=mNewsList.get(position);
            holder.newsTitleText.setText(news.getTitle());
        }
        public  int getItemCount(){
            return mNewsList.size();
        }
    }
}

7.代码梳理

  最终我们可以对代码进行一个如下的梳理,代码会进行一个缩略,只要能表示出意思即可。代码结构如下所示:

  首先我们会调用MainActivity类进行活动的加载,当程序运行在屏幕宽度大于600dp的设备上面时,则是ipad的双页模式,小于则是默认加载layout/activity_main布局:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
1)双页模式

  layout-sw600dp/activity_main.xml布局文件中有两个 < < <fragment > > >标签用来在主界面中添加碎片,同时为了区分双页模式和单页模式,为双页模式右侧显示新闻(新闻标题和内容)的添加碎片的布局又套了一个FrameLayout帧布局,用其id="@+id/news_content_layout"来区分:

    <fragment
        android:id="@+id/news_title_fragment"
        android:name="com.shuting.news_fragment.Fragment.NewsTitleFragment"/>
        
    <FrameLayout
        android:id="@+id/news_content_layout">
        <fragment
            android:id="@+id/news_content_fragment"
            android:name="com.shuting.news_fragment.Fragment.NewsContentFragment"/>
    </FrameLayout>

  接着我们需要找到这两个碎片的布局,android:name属性告诉我们要添加的碎片类名,一个是NewsTitleFragment类,另一个是NewsContentFragment类,于是我们首先去看一下对应新闻列表的类——NewsTitleFragment:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.news_title_frag,container,false);
    }

  类在最开始则将news_title_frag布局动态的加载进来了,则可以知道双页模式中的新闻列表对应的碎片布局则为news_title_frag.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/news_title_recycler_view"/>
</LinearLayout>

  可以看到加入了一个RecyclerView控件,则一定会有对应的新闻列表中子项的布局——news_item.xml,并且还会为此布局添加一个适配器,适配器则添加到了NewsTitleFragment类中,同时里面则会直接进行一个布局及数据初始化,因此就可以直接显示出来新闻列表了。接着我们看一下NewsContentFragment类:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        view=inflater.inflate(R.layout.news_content_frag,container,false);
        return view;
    }

  这个类中的onCreateView()方法将新闻碎片布局给动态加载进来了,则新闻的碎片布局文件为news_content_frag.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout>
        <TextView
            android:id="@+id/news_title"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black"/>
        <TextView
            android:id="@+id/news_content"/>
    </LinearLayout>
    <View
        android:background="@color/black"/>

</RelativeLayout>

  可以看到新闻(包括新闻内容和新闻标题)的布局用上述控件给描述出来。

2)单页模式

  我们找到layout/activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/news_title_layout">
    <fragment
        android:id="@+id/news_title_fragment"
        android:name="com.shuting.news_fragment.Fragment.NewsTitleFragment"/>
</FrameLayout>

  可以看到里面包含一个碎片,碎片的类名为NewsTitleFragment类,因此我们找到这个类,和双页模式一样,这个类会进行数据的初始化和界面的加载,同时也会通过"@+id/news_content_layout"来判断是单页还是双页:

    public void onActivityCreated(Bundle savedINstanceState){
        super.onActivityCreated(savedINstanceState);
        if(getActivity().findViewById(R.id.news_content_layout)!=null){
            isTwoPane=true;//可以找到news_content_layout布局时,为双页模式
        }else{
            isTwoPane=false;//找不到此布局时为单页模式
        }
    }

  我们需要从活动中获取到此布局,因为程序刚开始在主活动中运行的时候就加载的是activity_main.xml,然后定义的isTwoPane是为后续找到当前点击的新闻列表是否跳转做准备的,因为当新闻列表出现的时候,我们会随机选择一个新闻标题进行点击,则点击以后是双页模式刷新界面还是单页模式进行跳转到下一个界面是用isTwoPane来决定的:

                    if(isTwoPane){
                        NewsContentFragment newsContentFragment=
                                (NewsContentFragment)getFragmentManager()
                                .findFragmentById(R.id.news_content_fragment);
                        newsContentFragment.refresh(news.getTitle(),news.getContent());
                    }else{
                        NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
                    }

  如果是双页模式,则需要通过R.id.news_content_fragment来获取到展示新闻的布局,并且获得到碎片的实例,来进行界面的刷新,如果是单页模式,则进行跳转到新的界面——NewsContentActivity类:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_content);
        String newsTitle=getIntent().getStringExtra("news_title");//获取传入的新闻标题
        String newsContent=getIntent().getStringExtra("news_content");//获取传入的新闻内容
        NewsContentFragment newsContentFragment=(NewsContentFragment)
                getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
        newsContentFragment.refresh(newsTitle,newsContent);
    }

  类中则采用onCreate()方法用R.layout.news_content展示目前的表示内容的布局——news_content.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <fragment
        android:id="@+id/news_content_fragment"
        android:name="com.shuting.news_fragment.Fragment.NewsContentFragment"/>
</LinearLayout>

  跟双页模式一样,新闻(新闻标题和新闻内容)布局都是一样的。

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值