Android PagerAdapter Demo

package com.example.sijiewang.testviewpager;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;

public class MainActivity extends Activity {
    private ViewPager mViewPager;
    private ArrayList<View> mViewList;
    private LayoutInflater mLayoutInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLayoutInflater = getLayoutInflater();

        mViewList = new ArrayList<View>(4);
        mViewList.add(mLayoutInflater.inflate(R.layout.page1, null));
        mViewList.add(mLayoutInflater.inflate(R.layout.page2, null));
        mViewList.add(mLayoutInflater.inflate(R.layout.page3, null));
        mViewList.add(mLayoutInflater.inflate(R.layout.page4, null));

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new PagerAdapterDemo());
    }

    class PagerAdapterDemo extends PagerAdapter {

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(mViewList.get(position), 0);
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager)container).removeView(mViewList.get(position));
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }
    }

}

activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

一些特性:PagerAdapter始终会保持当前和下一个Page可用,销毁前一个Page 。

Android 绘制透明矩形

    Paint paint;
    Rect  spaceRect;

    spaceRect = new Rect();
    spaceRect.set(left, top, right, bottom);

    paint.setStyle(Paint.Style.FILL);
    paint.setAlpha(0);

    c.drawRect(spaceRect, paint);

NSString+URLEncoding 分类

#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>

@implementation NSString (URLEncoding)
-(NSString*)URLEncodedString {
    CFStringRef StringRef = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("+$,#[] "), kCFStringEncodingUTF8);
    NSString *result = (NSString*)CFBridgingRelease(StringRef);
    return result;
}

-(NSString*)URLDecodedString {
    CFStringRef stringRef = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8);
    NSString *result = (NSString*)CFBridgingRelease(stringRef);
    return  result;
}
@end

Android 的坑:Intent setData and setType

public Intent setData (Uri data)

Added in API level 1
Set the data this intent is operating on. This method automatically clears any type that was previously set by setType(String) or setTypeAndNormalize(String).

Note: scheme matching in the Android framework is case-sensitive, unlike the formal RFC. As a result, you should always write your Uri with a lower case scheme, or use normalizeScheme() or setDataAndNormalize(Uri) to ensure that the scheme is converted to lower case.

Parameters
data	The Uri of the data this intent is now targeting.
Returns
Returns the same Intent object, for chaining multiple calls into a single statement.

public Intent setType (String type)

Added in API level 1
Set an explicit MIME data type.

This is used to create intents that only specify a type and not data, for example to indicate the type of data to return.

This method automatically clears any data that was previously set (for example by setData(Uri)).

Note: MIME type matching in the Android framework is case-sensitive, unlike formal RFC MIME types. As a result, you should always write your MIME types with lower case letters, or use normalizeMimeType(String) or setTypeAndNormalize(String) to ensure that it is converted to lower case.

Parameters
type	The MIME type of the data being handled by this intent.
Returns
Returns the same Intent object, for chaining multiple calls into a single statement.

public Intent setDataAndType (Uri data, String type)

Added in API level 1
(Usually optional) Set the data for the intent along with an explicit MIME data type. This method should very rarely be used -- it allows you to override the MIME type that would ordinarily be inferred from the data with your own type given here.

Note: MIME type and Uri scheme matching in the Android framework is case-sensitive, unlike the formal RFC definitions. As a result, you should always write these elements with lower case letters, or use normalizeMimeType(String) or normalizeScheme() or setDataAndTypeAndNormalize(Uri, String) to ensure that they are converted to lower case.

Parameters
data	The Uri of the data this intent is now targeting.
type	The MIME type of the data being handled by this intent.
Returns
Returns the same Intent object, for chaining multiple calls into a single statement.

看下源码:

public Intent setData(Uri data) {
    mData = data;
    mType = null;
    return this;
}

public Intent setType(String type) {
    mData = null;
    mType = type;
    return this;
}

public Intent setDataAndType(Uri data, String type) {
    mData = data;
    mType = type;
    return this;
}

真是坑啊。

小黑屋

Alt none