我在互聯網上搜索了「自定義對象傳遞」問題。有幾種解決方案,但他們都使用Intent進行通信。有意圖,有方法,以方便,但方法public Parcelable View.onSaveInstanceState()
如何通過View.onSaveInstanceState()方法傳遞自定義對象的列表?
所以,我的要求是保存視圖的狀態,因爲它是一個自定義視圖,我不能保存在片段中的狀態。相反,我必須重寫方法View.onSaveInstanceState()
和View.onRestoreInstanceState()
。
這些方法適用於Parcelable
。我有一個視圖叫做,BoxDrawingView
和Box
類包含在視圖中。
還有一件事 - 我已經嘗試Parcelize Box
類。
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by Manish Sharma on 9/6/2016.
*/
public class Box implements Parcelable{
private PointF mOrigin;
private PointF mCurrent;
public Box(PointF origin) {
mOrigin = origin;
mCurrent = origin;
}
public PointF getCurrent() {
return mCurrent;
}
public void setCurrent(PointF current) {
mCurrent = current;
}
public PointF getOrigin() {
return mOrigin;
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<Box> CREATOR
= new Parcelable.Creator<Box>() {
public Box createFromParcel(Parcel in) {
return new Box(in);
}
public Box[] newArray(int size) {
return new Box[size];
}
};
private Box(Parcel in) {
mOrigin = (PointF)in.readValue(ClassLoader.getSystemClassLoader());
mCurrent = (PointF)in.readValue(ClassLoader.getSystemClassLoader());
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeValue(mOrigin);
out.writeValue(mCurrent);
}
}
現在,這裏是應該呼籲節電狀態的2種生命週期方法的自定義視圖:
package com.example.manishsharma.draganddraw;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Manish Sharma on 9/5/2016.
*/
public class BoxDrawingView extends View {
private static final String TAG = "BoxDrawingView";
private Box mCurrentBox;
private List<Box> mBoxen = new ArrayList<>();
private Paint mBoxPaint;
private Paint mBackgroundPaint;
// Used when creating the view in code
public BoxDrawingView(Context context) {
this(context, null);
}
// Used when inflating the view from XML
public BoxDrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
// Paint the boxes a nice semitransparent red (ARGB)
mBoxPaint = new Paint();
mBoxPaint.setColor(0x22ff0000);
// Paint the background off-white
mBackgroundPaint = new Paint();
mBackgroundPaint.setColor(0xfff8efe0);
}
@Override
protected void onDraw(Canvas canvas) {
// Fill the background
canvas.drawPaint(mBackgroundPaint);
for (Box box : mBoxen) {
float left = Math.min(box.getOrigin().x, box.getCurrent().x);
float right = Math.max(box.getOrigin().x, box.getCurrent().x);
float top = Math.min(box.getOrigin().y, box.getCurrent().y);
float bottom = Math.max(box.getOrigin().y, box.getCurrent().y);
canvas.drawRect(left, top, right, bottom, mBoxPaint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
PointF current = new PointF(event.getX(), event.getY());
String action = "";
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
action = "ACTION_DOWN";
// Reset drawing state
mCurrentBox = new Box(current);
mBoxen.add(mCurrentBox);
break;
case MotionEvent.ACTION_MOVE:
action = "ACTION_MOVE";
if (mCurrentBox != null) {
mCurrentBox.setCurrent(current);
invalidate();
}
break;
case MotionEvent.ACTION_UP:
action = "ACTION_UP";
mCurrentBox = null;
break;
case MotionEvent.ACTION_CANCEL:
action = "ACTION_CANCEL";
mCurrentBox = null;
break;
}
Log.i(TAG, action + " at x=" + current.x +
", y=" + current.y);
return true;
}
@Override
public Parcelable onSaveInstanceState(){
super.onSaveInstanceState();
//How do I proceed here?
}
}
所以最後我:Parcelable
對象的ArrayList,我要保存。我怎麼做?
P.S:如果我使用方法Bundle.putParcelableArrayList(String key, ArrayList<? extends Parcelable> value)
,它不起作用,因爲Box
類實現Parcelable
接口不擴展它。
它的工作!最初我會這樣做,但是我糾纏在提供的參數詢問和參數中。所以,方法bundle.putParcelableArrayList();要求<?擴展了Parcelable>但我認爲自從我的類實現了Parcelable之後,類型兼容性就會出現問題。但是當我嘗試在參數中將列表轉換爲ArrayList時,它工作正常。這是我的Java基礎上的生鏽。實際上,它應該可以工作,因爲任何擴展Parcelable的類型都與Parcelable具有「is-a」關係,並且任何擴展此類型的類型也都與Pacelable具有「is-a」關係。 –
感謝您發佈代碼。 –