2016-09-07 57 views
0

我在互联网上搜索了“自定义对象传递”问题。有几种解决方案,但他们都使用Intent进行通信。有意图,有方法,以方便,但方法public Parcelable View.onSaveInstanceState()如何通过View.onSaveInstanceState()方法传递自定义对象的列表?

所以,我的要求是保存视图的状态,因为它是一个自定义视图,我不能保存在片段中的状态。相反,我必须重写方法View.onSaveInstanceState()View.onRestoreInstanceState()

这些方法适用于Parcelable。我有一个视图叫做,BoxDrawingViewBox类包含在视图中。

还有一件事 - 我已经尝试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接口不扩展它。

回答

0

保存状态

@Override 
public Parcelable onSaveInstanceState(){ 
    Parcelable superState = super.onSaveInstanceState(); 
    Bundle bundle = new Bundle(); 
    bundle.putParcelable("super", superState); 
    bundle.putParcelableArrayList("list", mBoxen); 
    return bundle; 
} 

和恢复

@Override 
protected void onRestoreInstanceState(Parcelable state) { 
    if (state instanceof Bundle) { 
     super.onRestoreInstanceState(((Bundle) state).getParcelable("super")); 
     mBoxen = ((Bundle) state).getParcelableArrayList("list"); 
    } else { 
     super.onRestoreInstanceState(state); 
    } 
} 

这应该工作。 bundle.putParcelableArrayList("list", mBoxen);有什么问题?

+0

它的工作!最初我会这样做,但是我纠缠在提供的参数询问和参数中。所以,方法bundle.putParcelableArrayList();要求<?扩展了Parcelable>但我认为自从我的类实现了Parcelable之后,类型兼容性就会出现问题。但是当我尝试在参数中将列表转换为ArrayList时,它工作正常。这是我的Java基础上的生锈。实际上,它应该可以工作,因为任何扩展Parcelable的类型都与Parcelable具有“is-a”关系,并且任何扩展此类型的类型也都与Pacelable具有“is-a”关系。 –

+0

感谢您发布代码。 –

-1

我认为你应该使用:

@Override 
protected void onSaveInstanceState(Bundle outState) {} 

和putExtra到outState束。

+0

你的评论没有回答这个问题 – Dika

相关问题