学习Android Studio开发工具之Activity2(&Fragment)

开篇先介绍几个放在眼前却经常忽视的快捷键如图:

展现出android Studio超强的搜索能力,提高大工程的开发维护效率。

双击Shift按键效果

Ctrl+Shift+N 根据文件名打开文件

Ctrl+E 打开最近文件

Alt+Home 定位导航栏

本篇是介绍Google推荐使用的fragment以及其与activity的通信。

新建一个BlankFragment,

其中自动生成的代码如下:

package com.hitsz.xiaokai.fragmentcom;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BlankFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 * 这里是说实现子Fragment需要继承Fragment,这里是Google推荐的模板,在fragment里定义一个接口,然后它所依附的activity必须实现这个接口来完成相应的交互事件。
 * 对于Fragment本身是使用工厂方法来创建实例的。
 */
public class BlankFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    //此处声明Fragment对应变量,一般是布局控件
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    //接口引用
    private OnFragmentInteractionListener mListener;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     * 使用相应的参数返回工厂方法创建的Fragment
     */
    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        //使用Bundle传递数据
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //加载特定布局文件到布局容器中
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    //更新参数到ui
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //判断activity是否实现了内部接口,如果没有实现抛出异常
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }
   //解除依附关系,同时将接口对象置空,方便垃圾回收
    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     * 此接口必须被activity实现
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}


这里拿官网给的demo作为演示:

布局文件layout/article_view.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/article"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:textSize="18sp" />

layout/news_articles.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

layout_large/news_articles.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

显示数据Ipsum.java

package com.hitsz.xiaokai.fragmentcom;

public class Ipsum {

    static String[] Headlines = {
        "Article One",
        "Article Two"
    };

    static String[] Articles = {
        "Article One\n\nExcepteur pour-over occaecat squid biodiesel umami gastropub, nulla laborum salvia dreamcatcher fanny pack. Ullamco culpa retro ea, trust fund excepteur eiusmod direct trade banksy nisi lo-fi cray messenger bag. Nesciunt esse carles selvage put a bird on it gluten-free, wes anderson ut trust fund twee occupy viral. Laboris small batch scenester pork belly, leggings ut farm-to-table aliquip yr nostrud iphone viral next level. Craft beer dreamcatcher pinterest truffaut ethnic, authentic brunch. Esse single-origin coffee banksy do next level tempor. Velit synth dreamcatcher, magna shoreditch in american apparel messenger bag narwhal PBR ennui farm-to-table.",
        "Article Two\n\nVinyl williamsburg non velit, master cleanse four loko banh mi. Enim kogi keytar trust fund pop-up portland gentrify. Non ea typewriter dolore deserunt Austin. Ad magna ethical kogi mixtape next level. Aliqua pork belly thundercats, ut pop-up tattooed dreamcatcher kogi accusamus photo booth irony portland. Semiotics brunch ut locavore irure, enim etsy laborum stumptown carles gentrify post-ironic cray. Butcher 3 wolf moon blog synth, vegan carles odd future."
    };
}

导航Fragment,继承自ListFragment,自带listview。

HeadlinesFragment.java

package com.hitsz.xiaokai.fragmentcom;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HeadlinesFragment extends ListFragment {
    //定义内部接口引用
    OnHeadlineSelectedListener mCallback;

    // The container Activity must implement this interface so the frag can deliver messages
    public interface OnHeadlineSelectedListener {
        /** Called by HeadlinesFragment when a list item is selected,当点击list item时回调,携带点击位置信息
        */
        public void onArticleSelected(int position);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

        // Create an array adapter for the list view, using the Ipsum headlines array 创建一个适配器ArrayAdapter<String>显示listview内容
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
    }

    @Override
    public void onStart() {
        super.onStart();

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        //根据id获取Fragment,需要在布局文件中使用Fragment标签加载Fragment
        if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.确保依附的activity实现接口
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item
        //通知activity选中的item
        mCallback.onArticleSelected(position);

        // Set the item as checked to be highlighted when in two-pane layout,设为高亮
        getListView().setItemChecked(position, true);
    }
}

显示详细内容的ArticleFragment.java

package com.hitsz.xiaokai.fragmentcom;

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

public class ArticleFragment extends Fragment {
    //Fragment通信参数
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.考虑的activity非正常退出,还原状态
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.article_view, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already been
        // applied to the fragment at this point so we can safely call the method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
            // Set article based on argument passed in
            updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            // Set article based on saved instance state defined during onCreateView
            updateArticleView(mCurrentPosition);
        }
    }

    public void updateArticleView(int position) {
        TextView article = (TextView) getActivity().findViewById(R.id.article);
        article.setText(Ipsum.Articles[position]);
        mCurrentPosition = position;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

主activity,MainActivity.java

package com.hitsz.xiaokai.fragmentcom;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import com.hitsz.xiaokai.fragmentcom.ArticleFragment;
import com.hitsz.xiaokai.fragmentcom.R;
//实现需要通信Fragment的内部接口
public class MainActivity extends FragmentActivity
        implements HeadlinesFragment.OnHeadlineSelectedListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment如果不是xml加载需要使用Fragment事务加载Fragment
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we‘re being restored from a previous state,
            // then we don‘t need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            HeadlinesFragment firstFragment = new HeadlinesFragment();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent‘s extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the ‘fragment_container‘ FrameLayout添加Fragment到activity的主要方法
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }
    }

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment

        // Capture the article fragment from the activity layout
        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we‘re in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);

        } else {
            // If the frag is not available, we‘re in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            //通过bundle携带数据,传递数据给articleFragment,Fragment之间的通信方式
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back 添加返回栈
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction提交事务
            transaction.commit();
        }
    }
}

运行效果:

时间: 2024-11-03 22:16:42

学习Android Studio开发工具之Activity2(&Fragment)的相关文章

学习Android Studio开发工具之Activity3(框架3)

接上文学习Android Studio开发工具之Activity3(框架2) 本篇介绍Android Studio提供的用户偏好设置,新建一个Module命名为Prefs,选择Settings Activity,如图: 运行的效果如图: SettingsActivity继承自AppCompatPreferenceActivity间接继承自PreferenceActivity,覆写方法onBuildHeaders(List<Header> target),在其方法内部加载视图资源文件 loadH

学习Android Studio开发工具之Activity3(框架2)

接上文学习Android Studio开发工具之Activity3(框架1) 本篇介绍Android Studio提供的用户登录框架,新建一个Module命名为Login,选择Login Activity,如图: 等待Android Studio编译完成安装到虚拟机后的效果如图: 可以看到使用ScrollView布局使得登录编辑框更加人性化. 先来看布局文件 activity_login.xml <LinearLayout xmlns:android="http://schemas.and

学习Android Studio开发工具之Activity1

Android Studio与EclipseADT存在着诸多不同之处,这里列举一些Android Studio相对Eclipse 比较棒的特性. 颜色.图片在布局和代码中可以实时预览 string可以实时预览 多屏预览.截图带有设备框,可随时录制模拟器视频 可以直接打开文件所在位置 跨工程移动.搜索.跳转 自动保存,无需一直Ctrl + S 即使文件关闭依然可以回退N个历史 智能重构.智能预测报错 每一行文件编辑历史,可追溯到人 各种插件例如ADB..gitignore.sql.markdown

android studio开发工具的android library打包文件(.aar)本地引用

by 蔡建良 2014-5-13 关键点: 利用Gradle发布本地maven库支持android library 打包文件(*.aar) 的本地引用 开发环境: windows7 64位操作系统 android studio0.5.8 (1) 安装maven1.在安装maven之前,先确保已经安装JDK1.6及以上版本,并且配置好环境变量.2.下载maven3,最新版本是Maven3.2.1 ,下载地址:http://maven.apache.org/download.html 下载apach

[Android 新特性] 谷歌发布Android Studio开发工具1.0正式版(组图) 2014-12-09 09:35:40

Android Studio是谷歌于13年I/O大会推出的Android开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的Android开发工具用于开发和调试. 今日谷歌发布了Android studio的1.0正式版,增加了包括智能代码编辑,用户界面设计工具,性能分析工具等新功能,支持Android 5.0平台的开发. 谷歌也将推荐开发环境从Eclipse IDE调整为Android studio,还为Eclipse开发者提供了

Android Studio 开发工具基本设置

由于时间关系本文只简单说明作用: 1.Android Studio 背景颜色设置,个人喜欢这背景颜色.还有设置字体大小. 2.文件字符编码设置 3.快捷键使用 之前习惯使用 Eclipse 所以设置 4.自动导包设置 5.真机调试设置 首先真机设置开发者选项

Android studio开发工具设置默认编码

这段是用的是Google的开发工具,和Eclipse设置编码格式的时候有点区别,这里我分享给大家 第一步 File -->Settings打开设置面板 第二步在编码面板设置编码 应用一下就ok了

Android Studio 开发技巧详解

Android Studio开发工具是每一个 Android 程序猿每天都要使用的工具,但是即使你是一个经验丰富的开发人员,你也可能已经错过了许多可以节约生命的技巧,这篇文章也许就可以帮助你掌握它们其中的一部分.我不会一字一句地翻译,而是以最简洁易懂的方式介绍给你,同时提供必要的注解和延伸,让你可以在一遍快速阅读之后迅速掌握. 1.当你想不起来某个功能怎么用的时候 如果你是 Windows/Linux 用户, 那么请按Ctrl + Shift + A, 如果你是 Mac 用户,那么请按Comma

windows平台下Android studio开发环境搭建教程

最近,Google 已宣布,为了简化 Android 的开发力度,以重点建设 Android Studio 工具,到今年年底将停止支持Eclipse等其他集成开发环境 .而随着Android studio正式版的推出和完善,Android开发者们转向Android studio开发平台也将是大势所趋! 小弟Vike原先学习Android也是一直用的eclipse,虽然时间不长,而且用起来慢点,卡点,但是毕竟熟悉起来了,猛地要转到一个新平台,还真是相当不习惯.且不说快捷键有变化,就连Android