android筆記4——Fragment的使用

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

說明第一下:按照前面的方式我們建立了項目,如果使用的是最新的ADT,Minimum Android SDK選的是android2.*或1.*,此時會預設建立一個相容的項目,——appcompat_v7,這個項目還是不能刪除的,刪除會報錯。。

說明第二下:項目建立好了之後,發現layout檔案夾(布局)中會有預設兩個檔案:adtivity和fragment檔案:


1、Fragment概述:

Fragment意思為片段,片段,說白了就是模組,說道模組,就不用我多說了。。

特徵:

Fragment總是“嵌入”activity中使用的,作為activity中的介面組成部分;

一個activity可以包含多個Fragment,一個Fragment也可以被多個activity複用;

Fragment可以響應自己的事件,也擁有自己的生命週期。

首先我們先看一下我們要達到什麼要求:

  

 
2、fragment_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.javen.xresource.activity.fragment.MainFragment" >    <TextView        android:id="@+id/ftxv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/fragment" />    <Button         android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/change_fragment"/>    <Button         android:id="@+id/btn2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/change_activity"/></LinearLayout>
在fragment中定義了兩個按鈕和一個文本顯示,以及在activity中也定義了一個文本顯示,點擊fragment不同按鈕控制文本顯示。

activity_main.xml內容如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.javen.xresource.activity.MainActivity"    tools:ignore="MergeRootFrame">    <TextView         android:id="@+id/atxv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/activity"/>        <!-- xml方式在activity中嵌入fragment -->    <fragment         android:name="com.javen.xresource.activity.fragment.MainFragment"        android:id="@+id/mft"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    </LinearLayout>

3、建立MainFragment.java類:

package com.javen.xresource.activity.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;import com.javen.xresource.activity.R;public class MainFragment extends Fragment {private TextView fragmentTextView;private Button button;public MainFragment() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_main, container,false);//使用fragment_main.xml初始化Fragmentbutton = (Button) rootView.findViewById(R.id.btn);//擷取Fragment中的ButtonfragmentTextView = (TextView) rootView.findViewById(R.id.ftxv);//擷取Fragment中的TextViewbutton.setOnClickListener(new OnClickListener() {//設定按鈕的監聽事件@Overridepublic void onClick(View v) {fragmentTextView.setText("我被fragment點擊了...");}});return rootView;}}

此時Fragment已經搞定了。。。

4、MainActivity.java

代碼如下:

package com.javen.xresource.activity;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private Button button;private TextView activityTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*if (savedInstanceState == null) {getFragmentManager().beginTransaction().add(R.id.container, new MainFragment()).commit();//java代碼方式在activity中嵌入Fragment,與前面xml方式一樣}*/activityTextView = (TextView) findViewById(R.id.atxv);//擷取activity中的TextViewFragment fragment = getFragmentManager().findFragmentById(R.id.mft);//擷取activity中的fragmentbutton = (Button) fragment.getView().findViewById(R.id.btn2);//擷取fragment中的Button2button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {activityTextView.setText("我被activity點擊了...");}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}

將項目運行即可。。。

6、問題來了:Button2 改變activity中TextView的操作能不能放在Fragment?--> 延伸出:Fragment能不能與Activity通訊?

衍生問題後面再說,通訊肯定是可以的。

至於對TextView的操作都放在Fragment中。。。本人嘗試過,使用:

getActivity().findViewById(R.id.atxv);

是不能擷取到Activity中的View元素的,此語句返回的是null。所以Button2的操作還是得放在activity中。

至於原因?歡迎大家發表評論來討論這個問題。。。





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.