zoukankan      html  css  js  c++  java
  • Android连载12-完善新闻app内容区域

    1.这里编写一个类用于开启活动,首先在onCreateView()方法中加载了我们刚刚创建的news_content_frag布局,这个没什么好解释的,接下来又提供了一个refresh()方法,这个方法就是用于将新闻的标题和内容显示在界面上的。可以看到,这里通过findViewById()方法分别获取到新闻的标题和内容控件,然后将方法传递进来的参数​设置进去。

    package com.example.fragmentbestpractice;
    
    ​
    
    import android.app.Fragment;
    
    import android.os.Bundle;
    
    import android.view.LayoutInflater;
    
    import android.view.View;
    
    import android.view.ViewGroup;
    
    import android.widget.TextView;
    
    ​
    
    public class NewsContentFragment extends Fragment{
    
      private View view;
    
     
    
      @Override
    
      public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    
        view = inflater.inflate(R.layout.news_content_frag, container,false);
    
        return view;
    
      }
    
     
    
      public void refresh(String newsTitle,String newsContent) {
    
        View visibilityLayout = view.findViewById(R.id.visibility_layout);
    
        visibilityLayout.setVisibility(View.VISIBLE);
    
        TextView newsTitleText = (TextView) view.findViewById(R.id.news_title);
    
        TextView newsContentText = (TextView) view.findViewById(R.id.news_content);
    
        newsTitleText.setText(newsTitle);//刷新新闻标题
    
        newsContentText.setText(newsContent);//刷新新闻内容
    
      }
    
    }

    2.这里我们充分发挥了代码的复用性,直接在布局中引入了NewsContentFragment,这样也就相当于把news_content_frag​布局中内容自动加了进来。

    <?xml version="1.0" encoding="utf-8"?>
    
    <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.example.fragmentbestpractice.NewsContentFragment"
    
            android:layout_width="match_parent"
    
            android:layout_height="match_parent"
    
            /></LinearLayout>

    3.然后新建一个类用于显示新闻内容的活动,可以看到在onCreate()方法中我们通过Intent获取到了传入的新闻标题和内容,然后调用FragmentManager的findFragmentById()方法地得到了NewsContentFragment的实例,接着调用它的refresh()方法,并将新闻的标题和内容传入,就可以把这些数据显示出来了,注意这里我们还提供了一个action​Start()方法。

     
    
    package com.example.fragmentbestpractice;
    
    ​
    
    import android.app.Activity;
    
    import android.content.Context;
    
    import android.content.Intent;
    
    import android.os.Bundle;
    
    import android.view.Window;
    
    ​
    
    public class NewsContentActivity extends Activity{
    
      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);
    
      }
    
     
    
      @Override
    
      protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    
        setContentView(R.layout.news_content);
    
        String newsTitle = getIntent().getStringExtra("news_title");//获取传入的新闻标题
    
        String newsContent = getIntent().getStringExtra("news_content");//获取传入的新闻内容
    
        NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);
    
        newsContentFragment.refresh(newsTitle,newsContent);//刷新NewsContentFragment界面
    
      }
    
    ​
    
    }

    4.接下来创建一个用于显示新闻列表的布局

     
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="match_parent"
    
        android:layout_height="match_parent"
    
        android:orientation="vertical" >
    
       
    
        <ListView
    
            android:id="@+id/news_title_list_view"
    
            android:layout_width="match_parent"
    
            android:layout_height="match_parent" >
    
           
    
        </ListView></LinearLayout>

    ​三、源码:

    1.项目地址

    https://github.com/ruigege66/Android/tree/master/FragmentBestPractise

    2.CSDN:https://blog.csdn.net/weixin_44630050

    3.博客园:https://www.cnblogs.com/ruigege0000/

    4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

     

  • 相关阅读:
    Powershell增加ADB命令
    电脑通过ADB截图脚本
    常用Website List
    护眼豆绿色背景RGB
    xshell方便设置
    解决 Xshell6|Xftp6 强制升级
    beyond compare 4.2.9桌面右键集成的问题修复
    Ubuntu中Samba的安装配置和使用
    excel设置保护工作区域
    【转载】学习 Qt 编程的好书推荐
  • 原文地址:https://www.cnblogs.com/ruigege0000/p/12927431.html
Copyright © 2011-2022 走看看