Skip to content

Commit

Permalink
add support for FragmentActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjun committed Aug 6, 2015
1 parent f4e8487 commit 813cd85
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 118 deletions.
Empty file added apk/readme.txt~
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import cn.feng.skin.manager.listener.ISkinUpdate;
import cn.feng.skin.manager.loader.SkinInflaterFactory;
import cn.feng.skin.manager.loader.SkinManager;
import cn.feng.skin.manager.util.L;

/**
* Base Activity for development
Expand All @@ -16,7 +15,7 @@
*
* @author fengjun
*/
public class SkinPluginActivity extends Activity implements ISkinUpdate{
public class BaseActivity extends Activity implements ISkinUpdate{

/**
* Whether response to skin changing after create
Expand Down
84 changes: 84 additions & 0 deletions lib/src/cn/feng/skin/manager/base/BaseFragmentActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cn.feng.skin.manager.base;

import java.lang.reflect.Field;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import cn.feng.skin.manager.listener.ISkinUpdate;
import cn.feng.skin.manager.loader.SkinInflaterFactory;
import cn.feng.skin.manager.loader.SkinManager;

/**
* Base Fragment Activity for development
*
* <p>NOTICE:<br>
* You should extends from this if you what to do skin change
*
* @author fengjun
*/
public class BaseFragmentActivity extends FragmentActivity implements ISkinUpdate{

/**
* Whether response to skin changing after create
*/
private boolean isResponseOnSkinChanging = true;

private SkinInflaterFactory mSkinInflaterFactory;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

try {
Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
field.setAccessible(true);
field.setBoolean(getLayoutInflater(), false);

mSkinInflaterFactory = new SkinInflaterFactory();
getLayoutInflater().setFactory(mSkinInflaterFactory);

} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

}

@Override
protected void onResume() {
super.onResume();
SkinManager.getInstance().attach(this);
}

@Override
protected void onDestroy() {
super.onDestroy();
SkinManager.getInstance().detach(this);
}

/**
* dynamic add a skin view
*
* @param view
* @param attrName
* @param attrValueResId
*/
protected void dynamicAddNewSkinWidget(View view, String attrName, int attrValueResId){
// developing ...
}

final protected void enableResponseOnSkinChanging(boolean enable){
isResponseOnSkinChanging = enable;
}

@Override
public void onThemeUpdate() {
if(!isResponseOnSkinChanging) return;
mSkinInflaterFactory.applySkin();
}
}
3 changes: 3 additions & 0 deletions lib/src/cn/feng/skin/manager/loader/SkinInflaterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class SkinInflaterFactory implements Factory {

@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {

L.e("tgtg",name);

// if this is NOT enable to be skined , simplly skip it
boolean isSkinEnable = attrs.getAttributeBooleanValue(SkinConfig.NAMESPACE, SkinConfig.ATTR_SKIN_ENABLE, false);
if (!isSkinEnable){
Expand Down
22 changes: 19 additions & 3 deletions lib/src/cn/feng/skin/manager/loader/SkinManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@

/**
* Skin Manager Instance
*
*
* <ul>
* <strong>global init skin manager, MUST BE CALLED FIRST ! </strong>
* <li> {@link #init()} </li>
* </ul>
* <ul>
* <strong>get single runtime instance</strong>
* <li> {@link #getInstance(Context)} </li>
* <li> {@link #getInstance()} </li>
* </ul>
* <ul>
* <strong>attach a listener (Activity or fragment) to SkinManager</strong>
Expand Down Expand Up @@ -60,14 +65,26 @@ public class SkinManager implements ISkinLoader{
private String skinPath;
private boolean isDefaultSkin = false;

/**
* whether the skin being used is from external .skin file
* @return
*/
public boolean isExternalSkin(){
return !isDefaultSkin && mResources != null;
}

/**
* get present skin path
* @return
*/
public String getSkinPath() {
return skinPath;
}

/**
* return a global static instance of {@link SkinManager}
* @return
*/
public static SkinManager getInstance() {
if (instance == null) {
synchronized (synchronizedLock) {
Expand All @@ -87,8 +104,7 @@ public Resources getResources(){
return mResources;
}

private SkinManager() {
}
private SkinManager() { }

public void init(Context ctx){
context = ctx.getApplicationContext();
Expand Down
4 changes: 1 addition & 3 deletions sample/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:versionName="1.0.0_beta" >

<uses-sdk
android:minSdkVersion="10"
android:minSdkVersion="13"
android:targetSdkVersion="17" />

<!-- 在SDCard中创建与删除文件权限 -->
Expand All @@ -14,8 +14,6 @@
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



<application
android:name="cn.feng.skin.demo.app.SkinApplication"
android:allowBackup="true"
Expand Down
27 changes: 3 additions & 24 deletions sample/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:skin="http://schemas.android.com/android/skin"
android:id="@+id/root_layout"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
skin:enable="true"
android:background="@color/color_app_bg" >

<include
android:id="@+id/title_bar_layout"
layout="@layout/include_main_title_bar" />

<ListView
android:id="@+id/news_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/title_bar_layout"
android:cacheColorHint="#00000000"
android:divider="@color/color_new_list_divider"
android:dividerHeight="1dp"
skin:enable="true"
android:listSelector="@drawable/news_item_selector"
android:scrollbars="none" />

</RelativeLayout>
android:layout_height="match_parent" />
4 changes: 2 additions & 2 deletions sample/res/layout/activity_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@id/title_bar_layout"
android:textColor="@color/color_new_item_title"
android:textColor="@color/color_sel_skin_btn_text"
android:textSize="20sp"
android:text="官方默认"
skin:enable="true"
Expand All @@ -35,7 +35,7 @@
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@id/divider"
android:textColor="@color/color_new_item_title"
android:textColor="@color/color_sel_skin_btn_text"
android:textSize="20sp"
skin:enable="true"
android:text="暗夜诱惑"
Expand Down
26 changes: 26 additions & 0 deletions sample/res/layout/fragment_article_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:skin="http://schemas.android.com/android/skin"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_app_bg"
skin:enable="true" >

<include
android:id="@+id/title_bar_layout"
layout="@layout/include_main_title_bar" />

<ListView
android:id="@+id/news_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:layout_below="@id/title_bar_layout"
android:divider="@color/color_new_list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/news_item_selector"
android:scrollbars="none"
skin:enable="true" />

</RelativeLayout>
2 changes: 1 addition & 1 deletion sample/res/values/color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
<drawable name="color_news_item_normal">#ffffff</drawable>
<drawable name="color_news_item_pressed">#11000000</drawable>


<color name="color_sel_skin_btn_text">#24302b</color>
</resources>
4 changes: 2 additions & 2 deletions sample/src/cn/feng/skin/demo/activity/DetailActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import android.os.Bundle;
import android.widget.TextView;
import cn.feng.skin.demo.R;
import cn.feng.skin.manager.base.SkinPluginActivity;
import cn.feng.skin.manager.base.BaseActivity;
import cn.feng.skin.manager.util.ResourceUtils;

public class DetailActivity extends SkinPluginActivity{
public class DetailActivity extends BaseActivity{

private TextView titleText;
private TextView detailText;
Expand Down
92 changes: 14 additions & 78 deletions sample/src/cn/feng/skin/demo/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,94 +1,30 @@
package cn.feng.skin.demo.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import cn.feng.skin.demo.R;
import cn.feng.skin.demo.entity.News;
import cn.feng.skin.manager.base.SkinPluginActivity;
import cn.feng.skin.manager.util.CommonBaseAdapter;
import cn.feng.skin.manager.util.CommonViewHolder;
import cn.feng.skin.manager.util.L;
import cn.feng.skin.demo.fragment.ArticleListFragment;
import cn.feng.skin.manager.base.BaseFragmentActivity;

public class MainActivity extends SkinPluginActivity{

private TextView titleText;
private Button settingBtn;
private ListView newsList;
private NewsAdapter adapter;
private List<News> datas;
public class MainActivity extends BaseFragmentActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initData();
initView();
}

private void initData() {
datas = new ArrayList<News>();
for(int i = 0;i < 10; i++){
News news = new News();
news.content = "Always listen to your heart because even though it's on your left side, it's always right.";
news.title = "每日随笔";
datas.add(news);
}
}

private void initView() {
titleText = (TextView) findViewById(R.id.title_text);
newsList = (ListView) findViewById(R.id.news_list_view);
settingBtn = (Button) findViewById(R.id.title_bar_setting_btn);

titleText.setText("Small Article");

adapter = new NewsAdapter(this, datas);

newsList.setAdapter(adapter);

newsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, DetailActivity.class);
startActivity(intent);
}
});

settingBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SettingActivity.class);
startActivity(intent);
}
});
initFragment();
}

private class NewsAdapter extends CommonBaseAdapter<News>{

public NewsAdapter(Context context, List<News> mDatas) {
super(context, mDatas, new int[]{R.layout.item_news_title});
}

@Override
public void convertItemView(CommonViewHolder holder, News item, int position) {
holder.setText(R.id.item_news_title, item.title);
holder.setText(R.id.item_news_synopsis, item.content);
private void initFragment() {
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if(fragment == null){
fragment = new ArticleListFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
}
Loading

0 comments on commit 813cd85

Please sign in to comment.