Java 保存自定义视图的实例
我尝试在Android Studio中编写自定义视图。到目前为止,上帝 除了保存视图的实例外,一切都正常 该视图包含4个元素Java 保存自定义视图的实例,java,android,android-custom-view,savestate,Java,Android,Android Custom View,Savestate,我尝试在Android Studio中编写自定义视图。到目前为止,上帝 除了保存视图的实例外,一切都正常 该视图包含4个元素 线性布局(包含2个文本视图的内容) ImageView 旋转屏幕或按Back重新打开应用程序后,ImageView不会保存其状态/图像。它是默认的图像 我试过多种方法,但都不管用 从不调用onSaveInstanceState()和onRestoreInstanceState(可打包),我不知道为什么:( 希望你能帮助我 #编辑:添加问题的GIF 以下是我目前的代码
(包含2个线性布局
)文本视图的内容
ImageView
ImageView
不会保存其状态/图像。它是默认的图像
我试过多种方法,但都不管用
从不调用onSaveInstanceState()
和onRestoreInstanceState(可打包)
,我不知道为什么:(
希望你能帮助我
#编辑:添加问题的GIF
以下是我目前的代码:
package de.codersgen.activitycontrol;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Ben Ny on 21.02.2017.
*/
public class ActivityView extends LinearLayout {
private Context context;
private static String STATE_SUPER_CLASS = "ActivitySuperClass";
private static String STATE_HEADER_TEXT = "HeaderText";
private static String STATE_INFO_TEXT = "InfoText";
private static String STATE_STATUS_IMAGE = "StatusImage";
private LinearLayout mTextContainer;
private TextView mHeaderText;
private TextView mInfoText;
private ImageView mStatusImage;
private int state = 0;
private int[] stateImages = {
R.drawable.infosign_black_24dp,
R.drawable.check_black_24dp
};
public ActivityView(Context context) {
super(context);
this.context = context;
initializeViews(context);
}
public ActivityView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initializeViews(context);
}
public ActivityView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
initializeViews(context);
}
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.container_view, this);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mTextContainer = (LinearLayout) this
.findViewById(R.id.textContainer);
mHeaderText = (TextView) this
.findViewById(R.id.headerText);
mInfoText = (TextView) this
.findViewById(R.id.infoText);
mStatusImage = (ImageView) this
.findViewById(R.id.statusImage);
mStatusImage
.setBackgroundResource(android.R.drawable.ic_media_next);
}
@Override
protected Parcelable onSaveInstanceState() {
super.onSaveInstanceState();
Toast.makeText(context, "SAVE", Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putParcelable(STATE_SUPER_CLASS,
super.onSaveInstanceState());
bundle.putString(STATE_HEADER_TEXT, mHeaderText.getText().toString());
bundle.putString(STATE_INFO_TEXT, mInfoText.getText().toString());
bundle.putInt(STATE_STATUS_IMAGE, state);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
super.onRestoreInstanceState(bundle.getParcelable(STATE_SUPER_CLASS));
setHeaderText("TEST");
setInfotext(bundle.getString(STATE_INFO_TEXT));
if (bundle.getInt(STATE_STATUS_IMAGE) == 1)
setStatusFinish();
else
setStatusUnfinished();
}
else {
super.onRestoreInstanceState(state);
}
}
@Override
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
super.dispatchFreezeSelfOnly(container);
}
@Override
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
super.dispatchThawSelfOnly(container);
}
public String getHeaderText() {
mHeaderText = (TextView) this
.findViewById(R.id.headerText);
return mHeaderText.getText().toString();
}
public void setHeaderText(String text) {
mHeaderText = (TextView) this
.findViewById(R.id.headerText);
mHeaderText.setText(text);
}
public String getInfoText() {
mInfoText = (TextView) this
.findViewById(R.id.infoText);
return mInfoText.getText().toString();
}
public void setInfotext(String text) {
mInfoText = (TextView) this
.findViewById(R.id.infoText);
mInfoText.setText(text);
}
public void setStatusUnfinished() {
mStatusImage = (ImageView) this
.findViewById(R.id.statusImage);
state = 0;
mStatusImage.setImageResource(R.drawable.infosign_black_24dp);
}
public void setStatusFinish() {
mStatusImage = (ImageView) this
.findViewById(R.id.statusImage);
state = 1;
mStatusImage.setImageResource(R.drawable.check_black_24dp);
}
public void setStatusOnClickListener(OnClickListener listener) {
mStatusImage = (ImageView) this
.findViewById(R.id.statusImage);
mStatusImage.setOnClickListener(listener);
}
}
package de.codersgen.activitycontrol;
导入android.content.Context;
导入android.os.Bundle;
导入android.os.Parcelable;
导入android.util.AttributeSet;
导入android.util.SparseArray;
导入android.view.LayoutInflater;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入android.widget.Toast;
/**
*由Ben Ny于2017年2月21日创建。
*/
公共类活动视图扩展了LinearLayout{
私人语境;
私有静态字符串状态\u SUPER\u CLASS=“ActivitySuperClass”;
私有静态字符串状态\u HEADER\u TEXT=“HeaderText”;
私有静态字符串状态\u INFO\u TEXT=“InfoText”;
私有静态字符串状态\u STATUS\u IMAGE=“StatusImage”;
专用线路布局mTextContainer;
私有文本视图mHeaderText;
私有文本视图mInfoText;
私有图像查看mStatusImage;
私有int状态=0;
私有int[]状态图像={
R.drawable.infosign_black_24dp,
R.可拉拔。黑色支票\U 24dp
};
公共活动视图(上下文){
超级(上下文);
this.context=上下文;
初始化视图(上下文);
}
公共活动视图(上下文、属性集属性){
超级(上下文,attrs);
this.context=上下文;
初始化视图(上下文);
}
公共活动视图(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
this.context=上下文;
初始化视图(上下文);
}
私有void initializeViews(上下文){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
充气机。充气(右布局。容器视图,本);
}
@凌驾
充气时受保护的空隙(){
超级充气();
mTextContainer=(LinearLayout)此
.findViewById(R.id.textContainer);
mHeaderText=(TextView)此
.findViewById(R.id.headerText);
mInfoText=(TextView)此
.findviewbyd(R.id.infoText);
mStatusImage=(图像视图)此
.findviewbyd(R.id.statusImage);
mStatusImage
.setBackgroundResource(android.R.drawable.ic_media_next);
}
@凌驾
受保护的地块onSaveInstanceState(){
super.onSaveInstanceState();
Toast.makeText(上下文“保存”,Toast.LENGTH_SHORT).show();
Bundle=新Bundle();
bundle.putParcelable(STATE\u SUPER\u类,
super.onSaveInstanceState());
bundle.putString(STATE_HEADER_TEXT,mHeaderText.getText().toString());
bundle.putString(STATE_INFO_TEXT,mInfoText.getText().toString());
bundle.putInt(STATE\u STATUS\u IMAGE,STATE);
返回包;
}
@凌驾
RestoreInstanceState上的受保护无效(可包裹状态){
if(包的状态实例){
Bundle=(Bundle)状态;
super.onRestoreInstanceState(bundle.getParcelable(STATE_super_CLASS));
setHeaderText(“测试”);
setInfotext(bundle.getString(STATE_INFO_TEXT));
if(bundle.getInt(STATE\u STATUS\u IMAGE)==1)
setStatusFinish();
其他的
setStatusUnfinished();
}
否则{
super.onRestoreInstanceState(状态);
}
}
@凌驾
受保护的void dispatchSaveInstanceState(SparseArray容器){
超级发货单(集装箱);
}
@凌驾
受保护的无效dispatchRestoreInstanceState(SparseArray容器){
超级发货单(集装箱);
}
公共字符串getHeaderText(){
mHeaderText=(TextView)此
.findViewById(R.id.headerText);
返回mHeaderText.getText().toString();
}
公共无效setHeaderText(字符串文本){
mHeaderText=(TextView)此
.findViewById(R.id.headerText);
mHeaderText.setText(文本);
}
公共字符串getInfoText(){
mInfoText=(TextView)此
.findviewbyd(R.id.infoText);
返回mInfoText.getText().toString();
}
公共void setInfotext(字符串文本){
mInfoText=(TextView)此
.findviewbyd(R.id.infoText);
mInfoText.setText(text);
}
公共无效设置状态未完成(){
mStatusImage=(图像视图)此
.findviewbyd(R.id.statusImage);
状态=0;
mStatusImage.setImageResource(R.drawable.infosign\u black\u 24dp);
}
public void setStatusFinish(){
mStatusImage=(图像视图)此
.findviewbyd(R.id.statusImage);
状态=1;
mStatusImage.setImageResource(R.drawable.check_black_24dp);
}
公共void setStatusOnClickListener(OnClickListener侦听器){
mStatusImage=(图像视图)此
.findviewbyd(R.id.statusImage);
mStatusImage.setOnClickListener(listener);
}
}
编辑编号2:添加我的CustomView的XML
您可以添加一个可设置样式的属性
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/textContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/headerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="@android:color/black"
android:text="Example Header"
android:textSize="19sp">
</TextView>
<TextView
android:id="@+id/infoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_red_dark"
android:paddingLeft="10dp"
android:paddingBottom="3dp"
android:text="Info Header"
android:textSize="12sp">
</TextView>
</LinearLayout>
<ImageView
android:id="@+id/statusImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:padding="5dp"
android:src="@drawable/add_black_24dp"
android:layout_weight="0.001">
</ImageView>
</merge>