FragmentTest的使用

标题

package com.gao.fragment;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


/**
 * 在代码里面加入一个标记,判断是否为 双面板显示
 */
public class NewsTitleFragment extends Fragment {


    public NewsTitleFragment() {
        // Required empty public constructor
    }

    private ListView newsTitleListView;
    private List<News> mNewsList;
    private NewsAdapter adapter;
    private boolean isTwoPane; //是否为双页加载的 标记

    @Override
    public void onAttach(Activity context) {
        super.onAttach(context);
        mNewsList = getNews(); //初始化数据源

        adapter = new NewsAdapter(context, R.layout.news_item, mNewsList);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.news_title_frag, container, false);

        newsTitleListView = (ListView) view.findViewById(R.id.news_title_list_view);
        newsTitleListView.setAdapter(adapter);

        newsTitleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = mNewsList.get(position);

                if (isTwoPane) {
                    // 如果是双页模式,则刷新NewsContentFragment中的内容
                    NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager()
                            .findFragmentById(R.id.news_content_fragment);
                    newsContentFragment.refresh(news.getTitle(), news.getDesc());
                } else {
                    //如果是单页模式 就直接启动 NewsContentActivity
                    NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getDesc());

                }
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getActivity().findViewById(R.id.news_content_fragment) != null) {
            isTwoPane = true;
        } else {
            isTwoPane = false;
        }
    }

    private List<News> getNews() {

        List<News> newsList = new ArrayList<>();

        News news1 = new News();

        news1.setTitle("这个类的代码有点长");
        news1.setDesc("我来重点解释一下。根据碎片的生命周期,我们知道,onAttach()\n" +
                "方法会首先执行,因此在这里做了一些数据初始化的操作,比如调用 getNews()方法获取几条模拟的新闻数据,以及完成 NewsAdapter 的创建");
        newsList.add(news1);

        News news2 = new News();
        news2.setTitle("完成 NewsAdapter 的创建");
        news2.setDesc("然后在 onCreateView()方法中加载了news_title_frag布局, 并给新闻列表的 ListView注册了点击事件然后在 onCreateView()方法中加载了news_title_frag布局, 并给新闻列表的 ListView注册了点击事件然后在 onCreateView()方法中加载了news_title_frag布局, 并给新闻列表的 ListView注册了点击事件然后在 onCreateView()方法中加载了news_title_frag布局, 并给新闻列表的 ListView注册了点击事件");
        newsList.add(news2);
  News news3 = new News();
        news3.setTitle("完成 NewsAdapter 的创建");
        news3.setDesc("然后在 onCreateView()方法中加载了news_title_frag布局, 并给新闻列表的 ListView注册了点击事件然后在 onCreateView()方法中加载了news_title_frag布局, 并给新闻列表的 ListView注册了点击事件然后在 onCreateView()方法中加载了news_title_frag布局, 并给新闻列表的 ListView注册了点击事件然后在 onCreateView()方法中加载了news_title_frag布局, 并给新闻列表的 ListView注册了点击事件");
        newsList.add(news3);
        return newsList;
    }
}

新闻内容 Fragment


package com.gao.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * 显示 新闻内容的fragment
 */
public class NewsContentFragment extends Fragment {


    public NewsContentFragment() {
        // Required empty public constructor
    }

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return view = inflater.inflate(R.layout.news_content_frag, container, false);
    }

    //用于将新闻的标题和内容显示在界面上
    public void refresh(String newsTitle, String newsContent) {
        View visibleLayout = view.findViewById(R.id.visible_layout);
        visibleLayout.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);
    }

}

//显示新闻内容的 activity

package com.gao.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
//显示新闻内容de activity
public class NewsContentActivity extends AppCompatActivity {

    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);
        setContentView(R.layout.activity_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界面

    }
}
package com.gao.fragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * 创建新闻列表的适配器
 */
public class NewsAdapter extends ArrayAdapter<News> {
    private int resourceId;

    public NewsAdapter(Context context, int textViewResourceId, List<News> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        News news = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId,null);
        }else{
            view = convertView;
        }

        TextView newsTitleText = (TextView) view.findViewById(R.id.tv_title);
        newsTitleText.setText(news.getTitle());
        return view;
    }
}

新闻的实体类


package com.gao.fragment;

/**
 * 新闻的实体类
 */
public class News {
    public String title; //新闻标题
    public String desc; //新闻内容

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

主界面


package com.gao.fragment;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

/**
 * 动态的加载布局
 * 在运行时判断程序应该是使用双页模式还是单页模式
 *
 * layout-sw600dp
 * 当程序运行在屏幕宽度大于 600dp 的设备上时,会加载 layout-sw600dp/activity_main 布局,
 * 当程序运行在屏幕宽度小于 600dp 的设备上时,则仍然加载默认的layout/activity_main 布局
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gao.fragment.MainActivity">

    <fragment
        android:id="@+id/news_title_fragment"
        android:name="com.gao.fragment.NewsTitleFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

activity_main.xml(large)

<?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="horizontal">

    <fragment
        android:id="@+id/news_title_fragment"
        android:name="com.gao.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:background="#f00"
        android:layout_weight="3">

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

activity_news_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gao.fragment.NewsContentActivity">

    <fragment
        android:id="@+id/news_content_fragment"
        android:name="com.gao.fragment.NewsContentFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <!--直接在布局中引入了 NewsContentFragment,这样
也就相当于把 news_content_frag 布局的内容自动加了进来-->
</LinearLayout>

news_content_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              >
    <!-- 新闻内容的布局文件-->

    <LinearLayout
        android:id="@+id/visible_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="1dp"
            android:textSize="20sp" />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:scaleType="fitXY"
            android:src="@drawable/spilt_line" />
        <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>
    <ImageView
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:scaleType="fitXY"
        android:src="@drawable/spilt_line_vertical" />
</RelativeLayout>

news_item.xml

<?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="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="标题"
            android:textColor="#000"
            android:textSize="18sp"/>

    </LinearLayout>

</LinearLayout>

news_title_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ListView
        android:id="@+id/news_title_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值