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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章