Fragment
一、什麼是Fragment?
1、Fragment是activity的界面中的一部分;多個Fragment們組合到一個activity中;多個activity中可重用一個Fragment。即Fragment相當於模塊化的一段activity;
2、具有自己的生命週期,接收自己的事件;
3、在activity運行時被添加或刪除。
二、爲什麼要使用Fragment?
1、支持更動態靈活的界面設計;2、activity的layout分成Fragment。
三、如何使用Fragment?
1、Create Fragment類 — onCreate() onCreateView() onPause()
2、Add Fragment
(1) Layout
<?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">
<fragment
android:id="@+id/fragment_test"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.chenjinhua.redcircle.FragmentTest"
></fragment>
</LinearLayout>
activity_fragment.xml佈局文件裏添加如上代碼後,運行,報如下錯誤:非法狀態異常,Fragment沒有創建View。
解決方法:
首先新建一個view佈局文件如之前第二週寫的item_listview.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:layout_marginTop="5dp">
<ImageView
android:id="@+id/avatar_imageview"
android:layout_width="50dp"
android:layout_height="40dp"
/>
<TextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name"
android:layout_toRightOf="@+id/avatar_imageview"/>
<TextView
android:id="@+id/item_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="age"
android:layout_below="@+id/item_name"
android:layout_toRightOf="@+id/avatar_imageview"/>
</RelativeLayout>
然後在FragmentTest類的onCreateView()方法裏獲取view:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_listview,container);
return view;
}
(2)Java Code
FragmentManager;
FragmentTransaction;
Add/Remove
FragmentTestActivity類
package com.example.chenjinhua.redcircle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
/**
* Created by chenjinhua on 16/3/19.
*/
public class FragmentTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentTest fragmentTest = new FragmentTest();
fragmentTransaction.add(R.id.fragment_test_view,fragmentTest).commit();
}
}
activity_fragment.xml佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/red"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.chenjinhua.redcircle.FragmentTest"
></fragment>
<RelativeLayout
android:layout_marginTop="100dp"
android:id="@+id/fragment_test_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
FragmentTest類 同上。
運行報如下錯誤:
FragmentTestActivity類裏fragment_test_view是父view;FragmentTest類裏item_listview是子view。
解決辦法:修改FragmentTest類裏的onCreateView()方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//解析出來的xml視圖放到container裏,不綁定
View view = inflater.inflate(R.layout.item_listview,container,false);
return view;
}
程序運行結果:
第一個Fragment是通過佈局文件生成的,第二個Fragment是通過java code生成的。
刪除fragment:修改FragmentTestActivity類代碼爲d
fragmentTransaction.add(R.id.fragment_test_view,fragmentTest);
fragmentTransaction.remove(fragmentTest).commit();
四、如何管理Fragment?
1、查找Fragment — findFragmentById() 和 findFragmentByTag()
Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_test);
if (fragment instanceof FragmentTest){
// TODO: Do your Action.
}else{
throw new IllegalStateException("iis not fragmentTest");
}
java裏面的二元運算符,判斷左邊的對象是否是右邊類的實例。假如是的話,返回true;假如不是的話,返回false
2、Fragment的後退 — Fragment Stack; popBackStack(); addOnBackStackChangedListener()
五、Fragment 生命週期
參考博客:
http://blog.csdn.net/forever_crying/article/details/8238863/
Handler
多線程與異步
Handler用來做什麼?
1、定時執行Message和MessageQueue;
2、在不同線程中執行Runnable。
Handler怎麼使用?
obtainMessage() //取得消息
sendMessage() //發送消息
handlerMessage() //處理消息
Message、 MessageQueue、Looper
Message 有2個整型數值 和 1個Object;
MessageQueue是Message的隊列
Looper消息泵
Looper的原理
如何使用:
1、是MessageQueue的管理者;
2、Looper.prepare()
3、每一個Looper對象和一個線程關聯
4、Looper.myLooper()可以獲得當前線程的Looper對象
Looper從MessageQueue中取出Message,交由Handler的handlerMessage進行處理;調用Message.recycle()將其放入Message Pool中。
自定義控件、自定義屬性、畫視圖
一、提取佈局屬性 theme & style
把視圖控件中大部分相同的屬性抽取出來
1、Theme是針對窗體級別的,改變窗體樣式;Style 是針對窗體元素級別的,改變指定控件或者Layout的樣式;
2、抽象view的共同屬性;
3、可繼承
使用步驟:
1、創建屬性; 2、佈局中設置。
1、創建屬性
比如有3個TextView,他們有一些相同的屬性;
在res - values - styles.xml裏創建屬性。
2、佈局中設置
style.xml裏子控件屬性可以繼承父控件屬性,子控件的屬性會覆蓋父控件的屬性。
同理,Theme也是,在styles.xml裏創建theme;在Manifest裏設置。
如下圖設置了整個application的theme,也可以對每個activity設置theme。
三、View是如何工作的? – 自定義控件
1、構造器 —> 初始化
2、onMesure() 定大小
3、onLayout() 定位置
4、onDraw() 繪製
5、invalidate() 刷新
自定義控件的三種主要創建形式
1、繼承已有的控件來實現自定義控件,如 Button、 CheckedTextView;
2、通過繼承一個佈局文件實現自定義控件,如RelativeLayout;
3、通過繼承view類來實現自定義控件。
栗子:做一個簡單的自定義控件
1、做一個圓形的紅色按鈕;
2、中間有一個白色的數字;
3、數字起始爲20;
4、每點擊一次減少1。
1、代碼裏onDraw() - 自定義控件;
2、視圖xml文件裏引用自定義控件。
DrawRedCircleView類
package com.example.chenjinhua.redcircle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
public class DrawRedCircleView extends View implements View.OnClickListener{
private Paint mPaint;
private Rect mRect;
private int mNumber = 20;
public DrawRedCircleView(Context context) {
this(context, null);
}
public DrawRedCircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DrawRedCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint();
mRect = new Rect();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.RED);
canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, mPaint);
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(100);
String textNumber = String.valueOf(mNumber);
mPaint.getTextBounds(textNumber,0,textNumber.length(),mRect);
int textWidth = mRect.width();
int textHight = mRect.height();
canvas.drawText(textNumber,getWidth()/2 - textWidth/2,getWidth()/2 + textHight/2,mPaint);
View view = findViewById(R.id.redCircle_view);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if ( mNumber > 0 ){
mNumber --;
}else{
mNumber = 20;
}
invalidate();
}
}
RedCircleActivity類
package com.example.chenjinhua.redcircle;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by chenjinhua on 16/3/15.
*/
public class RedCircleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_redcircle);
}
}
MainActivity類
package com.example.chenjinhua.redcircle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
/**
* Created by chenjinhua on 16/3/15.
*/
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView_mainActivity = (TextView) findViewById(R.id.textView_mainActivity);
textView_mainActivity.setOnClickListener(this);
}
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, RedCircleActivity.class));
}
}
activity_redcircle.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">
<com.example.chenjinhua.redcircle.DrawRedCircleView
android:id="@+id/redCircle_view"
android:layout_width="200dp"
android:layout_height="200dp"/>
</LinearLayout>
如何自定義屬性
對於自定義屬性,遵循以下幾步,就可以實現:
1、自定義一個CustomView(extends View )類
2、編寫values/attrs.xml,在其中編寫styleable和item等標籤元素
3、在佈局文件中CustomView使用自定義的屬性(注意namespace)
4、在CustomView的構造方法中通過TypedArray獲取
在哪裏 創建屬性 ?
res - values 下新建xml文件,如attrs.xml。
attrs.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
//類
<declare-styleable name="ViewTest">
//每一個attr都是一個屬性,每個屬性都有一個format
<attr name="backgroundColor" format="color"/>
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
</declare-styleable>
</resources>
如何使用這些屬性:
代碼中引用
private int mBackgroundColor;
//根據名字讀取attr.xml裏的屬性(是一個數組)
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.ViewTest);
//從讀取出來的數組,獲取BackgroundColor的屬性,沒有的話默認值爲RED給變量mBackgroundColor。
mBackgroundColor = typedArray.getColor(R.styleable.ViewTest_backgroundColor,Color.BLUE);
mPaint.setColor(mBackgroundColor);
佈局中設置 - activity_draw.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
//添加自定義命名空間
xmlns:jinhua="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--引用自定義的控件-->
<com.example.chenjinhua.thirdweek.ViewTest
android:layout_width="300dp"
android:layout_height="300dp"
jinhua:backgroundColor="@color/colorAccent"
jinhua:textSize="18dp"/>
</LinearLayout>
小技巧
1、參考系統源碼的實現
2、多瞭解原理
Tips
1、什麼時候需要使用init()方法
init : 單詞翻譯過來是 初始化的意思, 不是一個官方提供的api函數,程序員習慣上 把 要用的控件 都在 oncreate中 統一實例化, 但是把代碼放在oncreate中很難看, 所以再創一個init( ) 函數 ……之類的,把初始化代碼(new出來的對象等)放裏面,方便整改。
2、利用畫布繪製文字
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(100);
//要寫的數字
String text = String.valueOf(mNumber);
//獲取文字四周矩形的邊距,放到mRect對象裏。
mPaint.getTextBounds(text,0,text.length(),mRect);
int textWidth = mRect.width();
int textHight = mRect.height();
canvas.drawText(text,getWidth()/2 - textWidth/2,getHeight()/2 + textHight/2,mPaint);
3、dp、sp 和 px
是什麼?
px:像素點
dp:與像素密度密切相關 (100px 在不同手機上大小顯示不一致,dp就解決了這個問題)
sp:相當於dp(用來修飾文字)
dip:= dp
怎麼用?
文字的尺寸一律用sp單位;
非文字的尺寸一律用dp單位;
偶爾需要使用px單位:如需要在屏幕上畫一條細的分割線 1px。