更新视图的一些解决方案

1.如果在webview中:

使用webview.reload方法

2.在activity中,但是该activity是背景,例如弹出一个alertdialog这种情况下的更新视图,即:本视图的更新是有事件触发的情况下可以使用该方式.

使用recreat()方法

例如:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_collect"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal"

        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="8"
            android:text="返回"
            android:id="@+id/set_back"
            android:gravity="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="设置"
            android:gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="字体大小"
            android:gravity="center_vertical"
            android:textSize="15dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15dp"
            android:text=">" />
    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shap_devide" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_weight="2"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="下载路径"
            android:gravity="center_vertical"
            android:textSize="15dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15dp"
            android:text=">" />
    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shap_devide" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_weight="2"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="清除数据"
            android:gravity="center_vertical"
            android:textSize="15dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15dp"
            android:text=">" />
    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shap_devide2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_weight="2"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="软件更新"
            android:gravity="center_vertical"
            android:textSize="15dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15dp"
            android:text=">" />
    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shap_devide" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_weight="2"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="关于本软件"
            android:gravity="center_vertical"
            android:textSize="15dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15dp"
            android:text=">" />
    </LinearLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shap_devide" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="退出登录"
        android:id="@+id/quitlogin"
        android:layout_gravity="center"
        android:visibility="gone"
        />
</LinearLayout>

activity文件:(弹出一个alerdialog,如果点击退出登录就执行 SetActivity.this.recreate();)

package imqsl.com.explorer.UserActivities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import imqsl.com.explorer.R;

public class SetActivity extends AppCompatActivity {
TextView textback=null;
    Button quit=null;
    SharedPreferences pref;
    boolean islogin=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        textback = (TextView) findViewById(R.id.set_back);
        quit= (Button) findViewById(R.id.quitlogin);
        pref=getSharedPreferences("login",MODE_PRIVATE);
        islogin=pref.getBoolean("islogin",false);
        if (islogin){
            quit.setVisibility(View.VISIBLE);
            quit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    new AlertDialog.Builder(SetActivity.this).setMessage("确定要退出登录吗?").setNegativeButton("退出登录", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            SharedPreferences.Editor editor=pref.edit();
                            editor.clear();
                            editor.apply();
                            dialogInterface.dismiss();
                            quit.setVisibility(View.GONE);
                            SetActivity.this.recreate();
                        }
                    }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).setTitle("退出登录").create().show();
                }
            });
        }

        textback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SetActivity.this.finish();
            }
        });
    }
}

截图如下:

3.第三种方式:利用activity的生命周期

可以看到在在一个activity执行完之后,想要刷新前一个页面的UI,可以利用这个生命周期,比如restart或者其他的

  protected void onRestart() {
        MainActivity.this.recreate();
        super.onRestart();
    }

 

posted @ 2017-04-20 11:28  黑夜不再来  阅读(438)  评论(0编辑  收藏  举报