1导入依赖
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
implementation 'com.android.support:design:28.0.0'
implementation project(':channelmanagement')
implementation project(':pulltorefresh_library')
implementation 'com.google.code.gson:gson:2.2.4'
2继承baseactivity
package com.example.lianyue;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
public abstract class BaseActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(bindLayout());
initView();
initData();
bindEvent();
}
protected abstract int bindLayout();
protected abstract void initView();
protected abstract void initData();
protected abstract void bindEvent();
}
3继承basefragment
package com.example.lianyue;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(bindLayout(),container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
initData();
bindEvent();
}
protected abstract int bindLayout();
protected abstract void initView();
protected abstract void initData();
protected abstract void bindEvent();
}
4倒计时跳转布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="34dp"
android:layout_marginEnd="40dp"
android:textSize="20sp" />
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="@mipmap/ww"/>
</RelativeLayout>
5倒计时activity
package com.example.lianyue;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
public class MainActivity extends BaseActivity {
private TextView time;
private ImageView image;
private SharedPreferences sp;
private int i=5;
@Override
protected int bindLayout() {
return R.layout.activity_main;
}
@Override
protected void initView() {
time = findViewById(R.id.time);
sp = getSharedPreferences("login", Context.MODE_PRIVATE);
if(sp.getBoolean("第一次",false)){
startActivity(new Intent(MainActivity.this, SecondActivity.class));
i=-1;
finish();
return;
}
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean("第一次",true);
edit.commit();
}
@Override
protected void initData() {
new Thread(){
@Override
public void run() {
super.run();
while (true){
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
Message message = handler.obtainMessage();
message.obj=i;
message.what=1;
handler.sendMessage(message);
i--;
if(i<0){
break;
}
}
}
}.start();
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int w = msg.what;
if(w==1){
int p=(Integer) msg.obj;
if(p>=0){
time.setText(p+"S");
if (p==0){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
}
}
};
@Override
protected void bindEvent() {
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
i=-1;
startActivity(intent);
return;
}
});
}
}
6主页面布局
&l