Android Serial 12 - Perfect news app content area

Posted by muthuraj_mr on Wed, 20 May 2020 19:19:36 +0200

1. Write a class here to start an activity by first loading the news_we just created in the onCreateView() methodContent_The frag layout, which is unexplained, goes on to provide a refresh() method for displaying news titles and content on the interface.As you can see, here you get the title and content control of the news by the findViewById() method, respectively, and set the parameters passed in by the method.

 

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);//Refresh News Headlines

    newsContentText.setText(newsContent);//Refresh News Content

  }

}

 

2. Here we make the most of the code's reusability by introducing NewsContentFragment directly into the layout, which is equivalent to news_Content_The frag layout is automatically added.

 

<?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. Then create a new class for displaying news content. You can see that in the onCreate() method we get the incoming news headlines and content through Intent, then call the findFragmentById() method of FragmentManager to get an instance of NewsContentFragment, then call its refresh() method, and pass in the headlines and content of the news to get these numbersAs it appears, notice that here we also provide an action Start() method.

 

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");//Get incoming news headlines

    String newsContent = getIntent().getStringExtra("news_content");//Get incoming news

    NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);

    newsContentFragment.refresh(newsTitle,newsContent);//Refresh NewsContentFragment Interface

  }

​

}

 

4. Next, create a layout to display the news list

 

<?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. Source code:

1. Project Address

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

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

3. Blog Park:https://www.cnblogs.com/ruigege0000/

4. Welcome to the WeChat Public Number: Fourier Transform, Personal Public Number, only for learning and communication, Background Reply "Gift Pack" to get big data learning materials

 

Topics: Android Fragment xml encoding