Skip to content

Commit

Permalink
TextView 改为弱引用, 但未能解决TextView在内存中不断增多的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerey-Jobs committed Jun 8, 2017
1 parent 02a11e3 commit 6ab893f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ protected void afterCreate(Bundle savedInstanceState) {
mAdapter.notifyDataSetChanged();
}


} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ protected void afterCreate(Bundle savedInstanceState) {
initUI();
dynamicAddView(mToolbarLayout, "ContentScrimColor", R.color.app_main_color);
mAdapter = new DayFragmentAdapter(getActivity());
Calendar c = null;
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime(new Date());
year = calendar.get(java.util.Calendar.YEAR);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
package com.jerey.themelib.loader;

import android.graphics.Typeface;
import android.util.Log;
import android.widget.TextView;

import com.jerey.themelib.utils.TypefaceUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


/**
* Created by _SOLID
* Date:2016/7/12
* Time:17:58
* 字体改变工厂
*/
public class TextViewRepository {
private static List<TextView> mTextViews = new ArrayList<>();
private static final String TAG = "TextViewRepository";
/**
* 虽使用了弱引用来解决TextView得不到释放的问题,但是没有效果,内存中的TextView依旧在不断增多.
* 即使不加入列表里面,TextView数量依旧是在使用过程中中变多
*/
private static ArrayList<WeakReference<TextView>> mTextViewsWeakList = new ArrayList<>();

public static void add(TextView textView) {
mTextViews.add(textView);
mTextViewsWeakList.add(new WeakReference<TextView>(textView));
textView.setTypeface(TypefaceUtils.CURRENT_TYPEFACE);
Log.d(TAG, "TextViewRepository mTextViews count: " + mTextViewsWeakList.size());
}

public static void clear() {
mTextViews.clear();
mTextViewsWeakList.clear();
}

public static void remove(TextView textView) {
mTextViews.remove(textView);
mTextViewsWeakList.remove(new WeakReference<TextView>(textView));
}

public static void applyFont(Typeface tf) {
for (TextView textView : mTextViews) {
textView.setTypeface(tf);
for (WeakReference<TextView> textViewWeak : mTextViewsWeakList) {
TextView textView = textViewWeak.get();
if (textView != null) {
textView.setTypeface(tf);
}
}
}
}

0 comments on commit 6ab893f

Please sign in to comment.