Android连载12-完善新闻app内容区域

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

 

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​布局中内容自动加了进来。git

 

<?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()方法。github

 

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>

 

​3、源码:app

1.项目地址ide

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

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

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

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

 

相关文章
相关标签/搜索