<pre class="java" name="code">package com.example.clicktoreload; import com.example.clicktoreload.ClickScreenToReload.Reload; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { ClickScreenToReload clickScreenToReload ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); clickScreenToReload = (ClickScreenToReload) findViewById(R.id.clickScreenToReload1); new Thread(){ public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } handler.sendEmptyMessage(0); }; }.start(); clickScreenToReload.reload.clicktoreload();{ }; } private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { clickScreenToReload.setloadfail(); }; }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.example.clicktoreload; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.RelativeLayout; public class ClickScreenToReload extends View { private View view; private Context context; public ClickScreenToReload(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; // TODO Auto-generated constructor stub init(); } private RelativeLayout loadingLayout,reloadlayout; Reload reload; private void init(){ reload = new Reload() { @Override public void clicktoreload() { // TODO Auto-generated method stub } }; view = LayoutInflater.from(context).inflate(R.layout.clickroreload, null); loadingLayout = (RelativeLayout) view.findViewById(R.id.reloadlayout); reloadlayout = (RelativeLayout) view.findViewById(R.id.reloadlayout); setLoading(); reloadlayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub reload.clicktoreload(); setLoading(); } }); } public void setLoading(){ reloadlayout.setVisibility(View.GONE); loadingLayout.setVisibility(View.VISIBLE); } public void setloadfail(){ reloadlayout.setVisibility(View.VISIBLE); loadingLayout.setVisibility(View.GONE); } public interface Reload{ public void clicktoreload(); } }
布局如下
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/loadinglayout" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:orientation="horizontal"> <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:layout_toRightOf="@+id/linearLayout1" android:text="加载中..." /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/reloadlayout" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="点击屏幕重新加载" /> </RelativeLayout> </FrameLayout>
原文地址:http://blog.csdn.net/jgszhuzhu/article/details/39325123