首页 > 其他 > 详细

Fragment在Activity中跳转,实现类似新闻标题跳转新闻内容功能

时间:2015-09-09 11:28:39      阅读:231      评论:0      收藏:0      [点我收藏+]

1.准备的工作,新闻数据类,新闻数据适配器,适配器的布局:

News.java

package com.example.zps.fourfragmentbestpractice;

/**
 * Created by zps on 2015/9/1.
 */
public class News {
    private String title;
    private String content;

    public String getTitle() {
        return title;
    }

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

NewsAdapter.java

package com.example.zps.fourfragmentbestpractice;

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;

/**
 * Created by zps on 2015/9/1.
 */
public class NewsAdapter extends ArrayAdapter<News>{
    private  int resourceId;
    public NewsAdapter(Context context, int resource, List<News> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @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获取到news的数据;
        TextView newsTitleText = (TextView)view.findViewById(R.id.news_item_news_title);
        newsTitleText.setText(news.getTitle());
        return view;
    }
}

news_adapter.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="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/news_item_news_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:paddingBottom="15dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp"
        android:singleLine="true"
        android:textSize="18sp"
        />
</LinearLayout>

2 . 实现新闻标题页面:

首先里面子布局用fragment实现,frament用自定义Fragment类来实现fragment布局的功能,比如跳转到另一个fragment;

然后,在总体布局的页面通过自定义的Fragment类加载fragment布局;

最后,用一个活动类来实现总布局页面;

news_title_fragment.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="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>

NewsTitleFragment.java

package com.example.zps.fourfragmentbestpractice;

import android.app.Activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
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;

/**
 * Created by zps on 2015/9/1.
 */
public class NewsTitleFragment extends Fragment implements AdapterView.OnItemClickListener {
    private List<News> listNews;
    private ListView newsTitleListView;
    private NewsAdapter adapter;
    private boolean isTwoPane;
//首先执行onAttach()对数据初始化
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //获取模拟新闻数据
        listNews = getNews();
        //设置适配器的数据,适配器加载news_item.xml布局
        adapter = new NewsAdapter(activity, R.layout.news_adapter, listNews);

    }

//再执行onCreateView()对布局界面初始化
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.news_title_fragment, container, false);
        newsTitleListView = (ListView) view.findViewById(R.id.news_title_list_view);
        //取得适配器的数据,为news_title_frag.xml布局下的ListView视图的子视图设置适配器;
        newsTitleListView.setAdapter(adapter);
        newsTitleListView.setOnItemClickListener(this);
        return view;
    }

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

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       News news = listNews.get(position);
        if(isTwoPane){
            NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager()
                    .findFragmentById(R.id.news_content_fragment);
            newsContentFragment.refresh(news.getTitle(),news.getContent());
        }else {
         NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
        }
    }

    private List<News> getNews() {
        listNews = new ArrayList<>();
        News news1 = new News();
        news1.setTitle("会见连战等出席抗战胜利纪念活动的台湾各界代表人士");
        news1.setContent("新华网快讯:中共中央总书记1日上午在人民大会堂会见出席抗战胜利70周年纪念活动的连战等台湾各界代表人士。");
        listNews.add(news1);
        News news2 = new News();
        news2.setTitle("9月2日-4日256条公交绕行或停驶");
        news2.setContent("北京市交通委表示,北京市公共交通运行将严格执行通告措施,按时尽快恢复公共交通运行,尽量减少对市民出行的影响。同时,在北京火车站等重点地区加大地面公交和出租保障措施,自9月2日20:00起至阅兵活动结束,公交、出租启动专项保障方案。\n" +
                "\n" +
                "地面公交方面,日班线路20路、25路、29路、39路、52路等5条日班线路,配车147部,每条线路增加5部机动车辆。可接驳地铁4号线、5号线、7号线和10号线四条地铁线路,确保抵京客流的及时疏散。\n" +
                "\n" +
                "夜班线夜17路、夜19路等2条夜班线路,配车8部,每条线路增加5部机动车辆,共配车18部。可接驳北京站东、北京焦化厂方向");
        listNews.add(news2);
        return listNews;
    }
}

news_title.xml

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/news_title_fragment"
        android:name="com.example.zps.fourfragmentbestpractice.NewsTitleFragment"/>

</LinearLayout>

NewsTitleActivity.java

(注意:

因为所有的Fragment类 是继承import android.support.v4.app.Fragment;(也可以继承import android.support.app.Fragment,我没精力去弄了)

所以FragmentActivity类 要继承FragmentActivity 类)

package com.example.zps.fourfragmentbestpractice;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;

public class NewsTitleActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.news_title);
    }

}

3.实现新闻内容页面:

基本思路和实现新闻标题的一样,不同点:

NewsContentActivity活动类通过NewsTitleActivity中的命令:NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent())来得到数据,而不是通过news类直接获得;

news_content_fragment.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"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/visibility_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="invisible">

        <TextView
            android:id="@+id/news_content_news_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:scaleType="fitXY"
            android:src="@drawable/spilt_line_line" />
        <TextView
            android:id="@+id/news_content_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:scaleType="fitXY"
            android:src="@drawable/split_line_vertical"/>
</RelativeLayout>

NewsContentFragment.java

package com.example.zps.fourfragmentbestpractice;


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

/**
 * Created by zps on 2015/9/1.
 */
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_fragment, 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_content_news_title);
        TextView newsContentText = (TextView) view.findViewById(R.id.
                news_content_news_content);
        newsTitleText.setText(newsTitle);
        newsContentText.setText(newsContent);
    }
}

news_content.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="match_parent"
    android:orientation="vertical">
<fragment
    android:id="@+id/news_content_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.zps.fourfragmentbestpractice.NewsContentFragment"/>
</LinearLayout>

NewsContentActivity.java

package com.example.zps.fourfragmentbestpractice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;

/**
 * Created by zps on 2015/9/1.
 */
public class NewsContentActivity extends FragmentActivity {

    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) getSupportFragmentManager()
                .findFragmentById(R.id.news_content_fragment);

        newsContentFragment.refresh(newsTitle,newsContent);


    }
}

Fragment在Activity中跳转,实现类似新闻标题跳转新闻内容功能

原文:http://www.cnblogs.com/520-1314/p/4793957.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!