Browse Source

添加base基类

316044749 7 years ago
parent
commit
1e1f8d271e

+ 1 - 0
app/build.gradle

@@ -19,5 +19,6 @@ android {
 
 
 dependencies {
 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile fileTree(dir: 'libs', include: ['*.jar'])
+    compile project(':library')
     compile 'com.android.support:appcompat-v7:26.1.0'
     compile 'com.android.support:appcompat-v7:26.1.0'
 }
 }

+ 1 - 0
app/src/main/AndroidManifest.xml

@@ -3,6 +3,7 @@
     package="com.ynstkz.shitu.android">
     package="com.ynstkz.shitu.android">
 
 
     <application
     <application
+        android:name=".application.STApplication"
         android:allowBackup="true"
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:label="@string/app_name"

+ 8 - 2
app/src/main/java/com/ynstkz/shitu/android/activity/HomeActivity.java

@@ -1,15 +1,21 @@
 package com.ynstkz.shitu.android.activity;
 package com.ynstkz.shitu.android.activity;
 
 
-import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.os.Bundle;
 
 
 import com.ynstkz.shitu.android.R;
 import com.ynstkz.shitu.android.R;
+import com.ynstkz.shitu.android.base.BaseActivity;
+import com.ynstkz.shitu.android.base.TitleBarActivity;
 
 
-public class HomeActivity extends AppCompatActivity {
+public class HomeActivity extends TitleBarActivity {
 
 
     @Override
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_home);
         setContentView(R.layout.activity_home);
     }
     }
+
+    @Override
+    protected String getBarTitle() {
+        return "测试";
+    }
 }
 }

+ 23 - 0
app/src/main/java/com/ynstkz/shitu/android/application/STApplication.java

@@ -0,0 +1,23 @@
+package com.ynstkz.shitu.android.application;
+
+import android.app.Application;
+import android.content.Context;
+
+/**
+ * 作者:fuchangle on 2018/1/27 21:56
+ */
+
+public class STApplication extends Application{
+
+    private static Context context;
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        context = getApplicationContext();
+    }
+
+    public static Context getContext(){
+        return context;
+    }
+}

+ 18 - 0
app/src/main/java/com/ynstkz/shitu/android/base/BaseActivity.java

@@ -0,0 +1,18 @@
+package com.ynstkz.shitu.android.base;
+
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+import android.support.v7.app.AppCompatActivity;
+
+/**
+ * 作者:fuchangle on 2018/1/27 21:50
+ */
+
+public class BaseActivity extends AppCompatActivity{
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+    }
+    
+}

+ 45 - 0
app/src/main/java/com/ynstkz/shitu/android/base/TitleBarActivity.java

@@ -0,0 +1,45 @@
+package com.ynstkz.shitu.android.base;
+
+import android.os.Bundle;
+import android.os.PersistableBundle;
+import android.view.View;
+
+import com.ynstkz.shitu.android.helper.TitleBarHelper;
+
+/**
+ * 作者:fuchangle on 2018/1/27 22:25
+ */
+
+public abstract class TitleBarActivity extends BaseActivity{
+
+    private TitleBarHelper titleBarHelper;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+    }
+
+    @Override
+    public void setContentView(int layoutResID) {
+        titleBarHelper = new TitleBarHelper(this,layoutResID);
+        titleBarHelper.setBarTitle(getBarTitle());
+        titleBarHelper.getBackUpView().setOnClickListener(new BackUpListener());
+        setContentView(titleBarHelper.getContentView());
+    }
+
+    /**
+     *设置标题名称
+     */
+    protected abstract String getBarTitle();
+
+
+    /**
+     *返回监听
+     */
+    protected  class BackUpListener implements View.OnClickListener{
+        @Override
+        public void onClick(View v) {
+            TitleBarActivity.this.finish();
+        }
+    }
+}

+ 10 - 0
app/src/main/java/com/ynstkz/shitu/android/common/Constants.java

@@ -0,0 +1,10 @@
+package com.ynstkz.shitu.android.common;
+
+/**
+ * 作者:fuchangle on 2018/1/27 21:54
+ */
+
+public class Constants {
+
+    public final static String ST_SHAREPREFREENCE="stsharepref";//SharePreference名字
+}

+ 91 - 0
app/src/main/java/com/ynstkz/shitu/android/data/SharedPreferencesUtils.java

@@ -0,0 +1,91 @@
+package com.ynstkz.shitu.android.data;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
+
+import com.ynstkz.shitu.android.application.STApplication;
+import com.ynstkz.shitu.android.common.Constants;
+
+import utils.AppInfoUtil;
+
+/**
+ * Created by fucl on 2016/4/6.
+ */
+public class SharedPreferencesUtils {
+
+    private static final String FILE_NAME = Constants.ST_SHAREPREFREENCE;
+    private static final String PACK_NAME = AppInfoUtil.getAppPackageName(STApplication.getContext());
+
+    /**
+     * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
+     * @param key			键名
+     * @param object		值
+     */
+    public static boolean set(String key, Object object) {
+        try {
+            Context otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
+            String type = object.getClass().getSimpleName();
+            SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
+            SharedPreferences.Editor editor = sp.edit();
+
+            if ("String".equals(type)) {
+                editor.putString(key, (String) object);
+            } else if ("Integer".equals(type)) {
+                editor.putInt(key, (Integer) object);
+            } else if ("Boolean".equals(type)) {
+                editor.putBoolean(key, (Boolean) object);
+            } else if ("Float".equals(type)) {
+                editor.putFloat(key, (Float) object);
+            } else if ("Long".equals(type)) {
+                editor.putLong(key, (Long) object);
+            }
+            return editor.commit();
+        } catch (PackageManager.NameNotFoundException e) {
+            e.printStackTrace();
+        }
+        return false;
+    }
+
+    public static boolean remove(String key){
+        Context otherAppsContext;
+        try {
+            otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
+            SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
+            SharedPreferences.Editor editor = sp.edit();
+            editor.remove(key);
+            return editor.commit();
+        } catch (PackageManager.NameNotFoundException e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+    /**
+     * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
+     * @param key
+     * @param defaultObject
+     * @return
+     */
+    public static Object get(String key, Object defaultObject) {
+        try {
+            String type = defaultObject.getClass().getSimpleName();
+            Context otherAppsContext = STApplication.getContext().createPackageContext(PACK_NAME, Context.CONTEXT_IGNORE_SECURITY);
+            SharedPreferences sp = otherAppsContext.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
+            if ("String".equals(type)) {
+                return sp.getString(key, (String) defaultObject);
+            } else if ("Integer".equals(type)) {
+                return sp.getInt(key, (Integer) defaultObject);
+            } else if ("Boolean".equals(type)) {
+                return sp.getBoolean(key, (Boolean) defaultObject);
+            } else if ("Float".equals(type)) {
+                return sp.getFloat(key, (Float) defaultObject);
+            } else if ("Long".equals(type)) {
+                return sp.getLong(key, (Long) defaultObject);
+            }
+            return defaultObject;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return defaultObject;
+        }
+    }
+}

+ 138 - 0
app/src/main/java/com/ynstkz/shitu/android/helper/TitleBarHelper.java

@@ -0,0 +1,138 @@
+package com.ynstkz.shitu.android.helper;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.FrameLayout;
+import android.widget.ImageView;
+import android.widget.RelativeLayout;
+import android.widget.TextView;
+
+import com.ynstkz.shitu.android.R;
+
+/**
+ * 作者:fuchangle on 2018/1/27 22:28
+ */
+
+public class TitleBarHelper {
+
+    /*上下文,创建view的时候需要用到*/
+    private Context mContext;
+
+    /*base view*/
+    private FrameLayout mContentView;
+
+    /*用户定义的view*/
+    private View mUserView;
+
+    /*视图构造器*/
+    private LayoutInflater mInflater;
+
+    private ImageView iv_back;
+    private TextView barTitle;
+    private TextView tv_title_right;
+    private ImageView ivTitleRight;
+    private RelativeLayout rlBarView;
+
+    public TitleBarHelper(Context context, int layoutId) {
+        this.mContext = context;
+        mInflater = LayoutInflater.from(mContext);
+        /*初始化整个内容*/
+        initContentView();
+        /*初始化用户定义的布局*/
+        initUserView(layoutId);
+        /*初始化titleBar*/
+        initTitleBar();
+    }
+
+
+    private void initContentView() {
+        /*直接创建一个帧布局,作为视图容器的父容器*/
+        mContentView = new FrameLayout(mContext);
+        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
+                ViewGroup.LayoutParams.MATCH_PARENT);
+        mContentView.setLayoutParams(params);
+    }
+
+    private void initTitleBar() {
+        /*通过inflater获取toolbar的布局文件*/
+        View titleBar = mInflater.inflate(R.layout.view_titlebar, mContentView);
+        iv_back = titleBar.findViewById(R.id.iv_back);
+        barTitle = titleBar.findViewById(R.id.tv_barTitle);
+        rlBarView = titleBar.findViewById(R.id.rl_bar_view);
+        tv_title_right = titleBar.findViewById(R.id.tv_title_right);
+        ivTitleRight = titleBar.findViewById(R.id.iv_title_right);
+    }
+
+    private void initUserView(int id) {
+        mUserView = mInflater.inflate(id, null);
+        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
+        params.topMargin = (int) (mContext.getResources().getDimension(R.dimen.titlebar_height)) - 1;
+        mContentView.addView(mUserView, params);
+    }
+
+    public FrameLayout getContentView() {
+        return mContentView;
+    }
+
+    /**
+     * 设置titlebar标题
+     * @param title
+     */
+    public void setBarTitle(String title){
+        barTitle.setText(title);
+    }
+
+    /**
+     * 获取返回按钮
+     * @return
+     */
+    public ImageView getBackUpView(){
+        return iv_back;
+    }
+
+    /**
+     * 右侧字体
+     * @return
+     */
+    public TextView getTitleRightTxt(){
+        return tv_title_right;
+    }
+
+    /**
+     * 右侧图片
+     * @return
+     */
+    public ImageView getIvTitleRight() {
+        return ivTitleRight;
+    }
+
+    public void setIvTitleRight(ImageView ivTitleRight) {
+        this.ivTitleRight = ivTitleRight;
+    }
+
+    public RelativeLayout getRlBarView() {
+        return rlBarView;
+    }
+
+    public void setRlBarView(RelativeLayout rlBarView) {
+        this.rlBarView = rlBarView;
+    }
+
+    public ImageView getIv_back() {
+        return iv_back;
+    }
+
+    public void setIv_back(ImageView iv_back) {
+        this.iv_back = iv_back;
+    }
+
+    public TextView getBarTitle() {
+        return barTitle;
+    }
+
+    public void setBarTitle(TextView barTitle) {
+        this.barTitle = barTitle;
+    }
+}

+ 1 - 3
app/src/main/res/layout/activity_home.xml

@@ -1,9 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    tools:context="com.ynstkz.shitu.android.activity.HomeActivity">
+    android:layout_height="match_parent">
 
 
     <TextView
     <TextView
         android:layout_width="wrap_content"
         android:layout_width="wrap_content"

+ 51 - 0
app/src/main/res/layout/view_titlebar.xml

@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+    android:fitsSystemWindows="true"
+    android:background="#0000ff">
+
+    <RelativeLayout
+        android:id="@+id/rl_bar_view"
+        android:layout_width="match_parent"
+        android:layout_height="@dimen/titlebar_height">
+
+        <ImageView
+            android:id="@+id/iv_back"
+            android:layout_width="wrap_content"
+            android:layout_height="20dp"
+            android:layout_centerVertical="true"
+            android:layout_marginLeft="10dp"/>
+
+        <TextView
+            android:id="@+id/tv_barTitle"
+            android:layout_width="260dp"
+            android:layout_height="wrap_content"
+            android:layout_centerInParent="true"
+            android:gravity="center"
+            android:maxLines="1"
+            android:ellipsize="end"
+            android:textSize="18sp"/>
+
+        <TextView
+            android:id="@+id/tv_title_right"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_alignParentRight="true"
+            android:layout_centerVertical="true"
+            android:padding="10dp"
+            android:textSize="14sp"
+            android:visibility="gone"/>
+
+        <ImageView
+            android:id="@+id/iv_title_right"
+            android:layout_width="20dp"
+            android:layout_height="20dp"
+            android:layout_alignParentRight="true"
+            android:layout_centerVertical="true"
+            android:layout_margin="10dp"
+            android:visibility="gone"/>
+    </RelativeLayout>
+
+</LinearLayout>

+ 9 - 0
app/src/main/res/values-v19/styles.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
+        <item name="android:windowTranslucentStatus">true</item>
+        <item name="android:windowTranslucentNavigation">true</item>
+    </style>
+
+</resources>

+ 12 - 0
app/src/main/res/values-v21/styles.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
+        <item name="android:windowTranslucentStatus">false</item>
+        <item name="android:windowTranslucentNavigation">true</item>
+        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
+        <item name="android:statusBarColor">@android:color/transparent</item>
+
+    </style>
+
+</resources>

+ 2 - 0
app/src/main/res/values/colors.xml

@@ -3,4 +3,6 @@
     <color name="colorPrimary">#3F51B5</color>
     <color name="colorPrimary">#3F51B5</color>
     <color name="colorPrimaryDark">#303F9F</color>
     <color name="colorPrimaryDark">#303F9F</color>
     <color name="colorAccent">#FF4081</color>
     <color name="colorAccent">#FF4081</color>
+
+    <color name="white">#ffffff</color>
 </resources>
 </resources>

+ 4 - 0
app/src/main/res/values/dimens.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <dimen name="titlebar_height">50dp</dimen>
+</resources>

+ 1 - 1
app/src/main/res/values/strings.xml

@@ -1,3 +1,3 @@
 <resources>
 <resources>
-    <string name="app_name">shitunet</string>
+    <string name="app_name">仕途网</string>
 </resources>
 </resources>

+ 1 - 1
app/src/main/res/values/styles.xml

@@ -1,7 +1,7 @@
 <resources>
 <resources>
 
 
     <!-- Base application theme. -->
     <!-- Base application theme. -->
-    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
+    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
         <!-- Customize your theme here. -->
         <!-- Customize your theme here. -->
         <item name="colorPrimary">@color/colorPrimary</item>
         <item name="colorPrimary">@color/colorPrimary</item>
         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

+ 0 - 26
library/src/androidTest/java/test/kotlin/com/library/ExampleInstrumentedTest.java

@@ -1,26 +0,0 @@
-package test.kotlin.com.library;
-
-import android.content.Context;
-import android.support.test.InstrumentationRegistry;
-import android.support.test.runner.AndroidJUnit4;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import static org.junit.Assert.*;
-
-/**
- * Instrumented test, which will execute on an Android device.
- *
- * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
- */
-@RunWith(AndroidJUnit4.class)
-public class ExampleInstrumentedTest {
-    @Test
-    public void useAppContext() throws Exception {
-        // Context of the app under test.
-        Context appContext = InstrumentationRegistry.getTargetContext();
-
-        assertEquals("test.kotlin.com.library.test", appContext.getPackageName());
-    }
-}

+ 15 - 1
library/src/main/java/utils/AppVersionUtil.java

@@ -8,7 +8,7 @@ import android.content.pm.PackageManager;
  * 作者:fuchangle on 2018/1/26 16:29
  * 作者:fuchangle on 2018/1/26 16:29
  */
  */
 
 
-public class AppVersionUtil {
+public class AppInfoUtil {
 
 
     /**
     /**
      * 获取版本号
      * 获取版本号
@@ -45,4 +45,18 @@ public class AppVersionUtil {
             return 1;
             return 1;
         }
         }
     }
     }
+
+    /**
+     * 获取程序包名
+     * @param context
+     * @return
+     */
+    public static String getAppPackageName(Context context){
+        try {
+            return context.getPackageName();
+        } catch (Exception e){
+            e.printStackTrace();
+        }
+        return "";
+    }
 }
 }

+ 0 - 17
library/src/test/java/test/kotlin/com/library/ExampleUnitTest.java

@@ -1,17 +0,0 @@
-package test.kotlin.com.library;
-
-import org.junit.Test;
-
-import static org.junit.Assert.*;
-
-/**
- * Example local unit test, which will execute on the development machine (host).
- *
- * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
- */
-public class ExampleUnitTest {
-    @Test
-    public void addition_isCorrect() throws Exception {
-        assertEquals(4, 2 + 2);
-    }
-}