導航:首頁 > 操作系統 > android刷新

android刷新

發布時間:2022-01-29 23:01:42

android 怎麼刷新當前的頁面

如果希望點擊鏈接由自己處理,而不是新開Android的系統browser中響應該鏈接。給WebView加一個事件監聽對象(WebViewClient)並重寫其中的一些方法:shouldOverrideUrlLoading:對網頁中超鏈接按鈕的響應。當按下某個連接時WebViewClient會調用這個方法,並傳遞參數:按下的url。 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); //在當前的webview中跳轉到新的url return true; } });

② android view怎樣刷新

兩種方式刷新:

  1. 主線程可以直接調用Invalidate()方法刷新

  2. 子線程可以直接調用postInvalidate()方法刷新。

  3. API的描述是這樣的 :Invalidatethe whole view. If the view is visible, onDraw(Canvas) will be called at somepoint in the future. This must be called from a UI thread. To call from anon-UI thread, call postInvalidate().。當Invalidate()被調用的時候,View的OnDraw()就會被調用,Invalidate()必須是在UI線程中被調用,如果在新線程中更新視圖的就調用postInvalidate()。

③ Android怎麼強制刷新view

關鍵的一句話就是:
在Android的布局體系中,父View負責刷新、布局顯示子View;而當子View需要刷新時,則是通知父View來完成。
步驟就是:
1、調用子View的invalidate()
2、跳轉到上一層的invalidateChild函數中區
3、在一次調用invalidateChildInParent的函數一次層層刷新
4、具體的刷新後續操作,我就不清楚了,調用invalidate最終在代碼上就在invalidateChild終止了的,所以表示有點點不清晰,求各位大牛介紹一下吧。。。。。?在此謝過了。。
讓我來閱讀源代碼:
首先在View類中:
/**
* Invalidate the whole view. If the view is visible, {@link #onDraw} will
* be called at some point in the future. This must be called from a
* UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
*/
public void invalidate() {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
}
if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
final ViewParent p = mParent; //獲得父類View的對象
final AttachInfo ai = mAttachInfo;//獲得匹配
if (p != null && ai != null) {
final Rect r = ai.mTmpInvalRect;
r.set(0, 0, mRight - mLeft, mBottom - mTop);//設置本View的尺寸,其實就是大小沒有設置位置
// Don't call invalidate -- we don't want to internally scroll
// our own bounds
p.invalidateChild(this, r); //調用父類的刷新函數
}
}
}
下面我們來到Viewgroup對象:

在invalidate中,調用父View的invalidateChild,這是一個從第向上回溯的過程,每一層的父View都將自己的顯示區域與傳入的刷新Rect做交集。
/**
* Don't call or override this method. It is used for the implementation of
* the view hierarchy.
*/
public final void invalidateChild(View child, final Rect dirty) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD);
}
ViewParent parent = this;
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
final int[] location = attachInfo.mInvalidateChildLocation;
// 刷新子View的位置
location[CHILD_LEFT_INDEX] = child.mLeft;
location[CHILD_TOP_INDEX] = child.mTop;
// If the child is drawing an animation, we want to this flag onto
// ourselves and the parent to make sure the invalidate request goes
// through
final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;
// Check whether the child that requests the invalidate is fully opaque
final boolean isOpaque = child.isOpaque() && !drawAnimation &&
child.getAnimation() != null;
// Mark the child as dirty, using the appropriate flag
// Make sure we do not set both flags at the same time
final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
do {
View view = null;
if (parent instanceof View) {
view = (View) parent;
}
if (drawAnimation) {
if (view != null) {
view.mPrivateFlags |= DRAW_ANIMATION;
} else if (parent instanceof ViewRoot) {
((ViewRoot) parent).mIsAnimating = true;
}
}
// If the parent is dirty opaque or not dirty, mark it dirty with the opaque
// flag coming from the child that initiated the invalidate
if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
}
parent = parent.invalidateChildInParent(location, dirty);
} while (parent != null);
}
/**
* Don't call or override this method. It is used for the implementation of
* the view hierarchy.
*
* This implementation returns null if this ViewGroup does not have a parent,
* if this ViewGroup is already fully invalidated or if the dirty rectangle
* does not intersect with this ViewGroup's bounds.
*/
public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD_IN_PARENT);
}
if ((mPrivateFlags & DRAWN) == DRAWN) {
if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
FLAG_OPTIMIZE_INVALIDATE) {
// 由父類的的位置,偏移刷新區域
dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
location[CHILD_TOP_INDEX] - mScrollY);
final int left = mLeft;
final int top = mTop;
if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||
(mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
mPrivateFlags &= ~DRAWING_CACHE_VALID;
location[CHILD_LEFT_INDEX] = left;
location[CHILD_TOP_INDEX] = top;
return mParent;
}
} else {
mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
location[CHILD_LEFT_INDEX] = mLeft;
location[CHILD_TOP_INDEX] = mTop;
dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],
mBottom - location[CHILD_TOP_INDEX]);
return mParent;
}
}
return null;
}
另外:
Invalidate()方法不能放在線程中,所以需要把Invalidate()方法放在Handler中。在MyThread中只需要在規定時間內發送一個Message給handler,當Handler接收到消息就調用Invalidate()方法。
postInvalidate()方法就可以放在線程中做處理,就不需要Handler。

而上面的新線程MyThre可以放在OnCreate()中開始,也可以放在OnStart()中開始。
Invalidate()方法和postInvalidate()都可以在主線程中調用而刷新視圖。
Invalidate()方法在SDK中是這樣描述的:Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate(). 當Invalidate()被調用的時候,View的OnDraw()就會被調用,Invalidate()必須是在UI線程中被調用,如果在新線程中更新視圖的就調用postInvalidate()。
簡言之,如果確定是在main thread中調用調用話, 使用 invaludate()

否則要調用 postInvalidate()

另外,橫豎屏切換使用重新構造 activity的。所以一定會重新刷新view 。

④ Android studio怎麼設置刷新文件狀態

可以通過定時刷新項目中的文件,首先需要打開的是Android studio的軟體,並在軟體中載入項目,點擊菜單中的file的選項。

彈出的下拉的菜單中可以看到的是為「settings」的選項。

這樣就進入到了settings的設置界面中,在設置選項的列表中有一項為version control,選項並點擊進入即可。

點擊完version control的選項之後,就會展開這個選項的所有菜單,可以直接點擊background的選項。

直接進入到了background的設置界面中,在這個界面中,找到一項為VCS History cache settings的選項,把refresh changes every的選項勾選上,對時間進行設定根據自己需要設置時間長短。

⑤ android 怎麼刷新fragment頁面

  1. 通常的做法,在onResume中發一起一個非同步的請求去拿數據,通過回調,收到返回的數據,然後更新UI。

  2. 網路獲取數據結束判斷數據有更新,然後通過set將控制項的數據更新。

舉例:

  1. 這個是我的一個fragment,我把它放在一個viewpager的fragment中

java">{
privatestaticfinalStringARG_CITY="city";
privateStringmCity;
privateTextViewtmpD;
privateTextViewtmpN;
privateSimpleDraweeViewimageD;
privateSimpleDraweeViewimageN;
(Stringcity){
BottomFragmentOnefragment=newBottomFragmentOne();
Bundleargs=newBundle();
args.putString(ARG_CITY,city);
fragment.setArguments(args);
returnfragment;
}
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
if(getArguments()!=null){
mCity=getArguments().getString(ARG_CITY);
}
}
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
BundlesavedInstanceState){
//
Viewview=inflater.inflate(R.layout.bottom_fragment_one,container,false);
tmpD=(TextView)view.findViewById(R.id.tmp_d);
tmpN=(TextView)view.findViewById(R.id.tmp_n);
imageD=(SimpleDraweeView)view.findViewById(R.id.image_d);
imageN=(SimpleDraweeView)view.findViewById(R.id.image_n);
setUI();
returnview;
}
//用來設置UI,更新UI時重新從資料庫獲取數據,進行設置
publicvoidsetUI(){
Forecastforecast=mFrecastDao.getForecastByCity(mCity);
tmpD.setText(forecast.getDaily_1_max());
tmpN.setText(forecast.getDaily_1_min());
imageD.setImageURI(getImageUri(forecast.getDaily_1_code_d()));
imageN.setImageURI(getImageUri(forecast.getDaily_1_code_n()));
}
}

2.除了上面那個,還有一個類似的fragment,我想實按下按鈕後,在兩個fragment之間進行切換。下面是按鈕代碼

@Override
publicvoidonClick(Viewv){
FragmentManagerfm=getChildFragmentManager();
//開啟Fragment事務
=fm.beginTransaction();
switch(v.getId())
{
caseR.id.button_left:
if(mBottomOne==null)
{
mBottomOne=newBottomFragmentOne().newInstanceOne(mCity);
}//使用當前Fragment的布局替代id_content的控制項
transaction.replace(R.id.bottom_weather,mBottomOne);
break;
caseR.id.button_right:
if(mBottomTwo==null)
{
mBottomTwo=newBottomFragmentTwo().newInstanceTwo(mCity);
}
transaction.replace(R.id.bottom_weather,mBottomTwo);
break;
}
//事務提交
transaction.commit();
}

3.下面是viewpager中的fragment用來更新上面兩個fragment數據的方法。

publicvoidUpdateUI(){
mBottomOne.setUI();
mBottomTwo.setUI();
}

⑥ 如何刷新Android的列表視圖

從列表視圖中刪除數據之後,你必須調用refreshDrawableState()刷新Android的列表視圖
下面是例子:
final DatabaseHelper db = new DatabaseHelper (ActivityName.this);

db.open();

db.deleteContact(arg3);

mListView.refreshDrawableState();

db.close();

和deleteContact在方法DatabaseHelper類將是長得像
public boolean deleteContact(long rowId) {

return db.delete(TABLE_NAME, BaseColumns._ID + "=" + rowId, null) > 0;

}

⑦ android列表數據刷新問題。。。

這種我建議設定一個自己的邏輯思維,採用sharedpreferences存儲,那麼你讀取只能在這裡面讀取,不要去讀取網路獲取到的。方法有很多種,這個採用非同步網路請求,我覺得最簡單,一旦數據變化更新看你放到那個位置.如果是你頁面沒及時刷新,採用生命周期來做。so
easy!

⑧ android 怎麼刷新UI組件

首先,android的UI刷新是在主線程(UI線程)中完成的。四大組件中,activity和service運行在主線程中。現在總結自己在項目中常用到的UI刷新方式。
第一,利用子線程發消息刷新UI。
子線程負責處理UI需要的數據,然後發消息到主線程來刷新UI。代碼結構如下:
new Thread(new Runnable() {

@Override
public void run() {
Person person=new Person();
person.setName(mName.getText().toString().trim());
person.setPhone(mPhone.getText().toString().trim());
Log.i("person",person.toString());
DatabaseInfoFactory.getPersonDao(mContext).addPerson(person);
Looper.prepare();
Message msg=Message.obtain();
msg.what=0x123456;
handler.sendMessage(msg);
Looper.loop();

}
}).start();
主線程中:
private Handler mHandler=new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what==0x123456||msg.what==0x123){
fillData();
setListener();
}

}
};
第二,利用非同步任務更新UI。代碼結構如下:
new AsyncTask<void,void,void>() {

@Override
protected void onPostExecute(Void result) {

if(mAdapter==null){
mAdapter=new LeaveInfoAdapter();
//設置數據適配器
mLVleaveInfos.setAdapter(mAdapter);
Log.i("測試", "非同步任務顯示後台獲得資料庫數據");
}
else {
mAdapter.notifyDataSetChanged();

}

super.onPostExecute(result);
}

@Override
protected Void doInBackground(Void... params) {
//獲得要顯示的數據
mleaveInfos=mLeaveInfosDao.findAll();
if (mleaveInfos==null) {
Toast.makeText(HomeActivity.this,"請假數據不存在或是已經清除!", 500).show();

}

Log.i("測試", "非同步任務後台獲得資料庫數據"+mleaveInfos.size());

return null;
}
}.execute();</void,void,void>
第三,利用配置文件+activity的生命周期方法刷新UI。

⑨ android開發怎麼刷新 scrollview

代碼很簡單,但是很實用,適合在一個Activity中要刷新局部的UI,比如在掃描一維碼的時候,要把每次掃描的結果都顯示在界面上

  1. 創建一個包含ScrollView的布局文件:

2.實現ScrollView的刷新

代碼如下:package com.example.uirefresh;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ScrollView;

import android.widget.TextView;

public class MainActivity extends Activity implements android.view.View.OnClickListener{

private Button btnExit;

private TextView text;

private ScrollView scroll;

private boolean bool = true;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnExit = (Button) findViewById(R.id.exit);

text = (TextView) findViewById(R.id.text);

scroll = (ScrollView) findViewById(R.id.scroll);

btnExit.setOnClickListener(this);

//在主線程main中開一子線程來刷新局部的ScrollView

new Thread(new RefreshThread()).start();

}

@Override

public void onClick(View v) {

switch(v.getId()) {

case R.id.exit :

finish();

break;

}

}

public class RefreshThread implements Runnable{

@Override

public void run() {

while(bool) {

try {

Thread.sleep(5000);//每五秒刷新一次

} catch (InterruptedException e) {

e.printStackTrace();

}

synchronized (this) {

//用post方法刷新

text.post(new Runnable() {

@Override

public void run() {

text.append("Hello World...n");

}

});

}

}

}

}

@Override

protected void onDestroy(){

super.onDestroy();

bool = false;

}

}

⑩ android怎麼刷新activity

刷新activity的方法:
1. 刷新當前activity界面數據(手動刷新):
在activity類下新增一個refresh()方法:
/**
* 刷新, 這種刷新方法,只有一個Activity實例。
*/
public void refresh() {
onCreate(null);
}

2. 刷新另一個activity界面數據(自動刷新):
在涉及到sqlite3資料庫操作的activity類下重寫onResume()方法:(此處建議復習下activity的生命周期, 並了解下onResume()方法的使用)
/**
* 調用onCreate(), 目的是刷新數據,
* 從另一activity界面返回到該activity界面時, 此方法自動調用
*/
@Override

閱讀全文

與android刷新相關的資料

熱點內容
安卓機玩吃雞什麼畫質 瀏覽:873
徒步緩解壓力的視頻 瀏覽:238
圖像演算法口訣 瀏覽:859
人踩什麼解壓 瀏覽:921
php語法檢查命令 瀏覽:330
如何重設伺服器網關 瀏覽:864
世界經濟pdf 瀏覽:108
異或演算法找缺失的數 瀏覽:325
單片機flagt1 瀏覽:485
單片機清理 瀏覽:660
東風景逸空調壓縮機 瀏覽:158
天津程序員炒股 瀏覽:230
pcl源碼目錄 瀏覽:968
python分類數據轉換 瀏覽:109
wordpdf不能復制 瀏覽:961
快捷方式參數命令 瀏覽:111
cmd命令復制粘貼文件 瀏覽:584
ug實體快速修剪的命令是什麼 瀏覽:123
軟體工程對演算法的要求 瀏覽:935
元史pdf 瀏覽:97