FragmentTestの使用

29050 ワード

見出し
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 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 getNews() {

        List newsList = new ArrayList<>();

        News news1 = new News();

        news1.setTitle("         ");
        news1.setDesc("        。         ,    ,onAttach()
"
+ " , , 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 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

<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)

<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

<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"
        />
    
LinearLayout>

news_content_frag.xml

<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

<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

<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>