OOM

在最近的项目中,使用了ViewPager+ImageView进行图片的浏览,OOM的代码:

1
2
3
4
5
6
7
@Override
public Object instantiateItem(ViewGroup container, int position) {
Work.Att att = list.get(position);
PhotoView view = new PhotoView(container.getContext());
container.addView(view);
return view;
}

这样会重复的创建ImageView,可能没有被回收,所以解决的方案是创建一个全局的集合,让图片都放进去,在使用的时候根据位置进行设置不同的图片去展示

1
2
3
4
5
6
7
@Override
public Object instantiateItem(ViewGroup container, int position) {
Work.Att att = list.get(position);
PhotoView view = Application.getImageList().get(position);
container.addView(view);
return view;
}

####关键

但是这样同样会让Viewpager指针指向了这个集合中的ImageView,因此在onDestory中添加:viewpager.removeAllViews();

分享到 评论

recyclerview notifyItemInserted没有更新

在使用RecyclerView过程中使用 notifyItemInserted notifyItemRangeRemoved等
方法时,还要调用notifyItemRangeChanged(position,list.size());

1
2
3
4
list.add(0, "leefeng.me" + "==onRefresh");
recycleview.stopRefresh(b);
adapter.notifyItemInserted(0);
adapter.notifyItemRangeChanged(0,list.size());

分享到 评论

使用RadioButton图片选中切换的问题

在使用RadioButton的时候在选择图片切换的时候出现的一个问题:

1
2
<item android:drawable="@drawable/my_on" android:state_checked="true" />
<item android:drawable="@drawable/my_off" />

不能添加state_pressed的状态,否则就出现了不能够正常切换DrawableTop图片

分享到 评论

Android ViewPager左右滑动取消的方法

Android ViewPager左右滑动取消的方法是重写子View
实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (getScrollX()<0) {
LogUtils.w("WifiCustomWebView---> getScrollX <= 0");
getParent().requestDisallowInterceptTouchEvent(false);
return false;
} else if (getScrollX() >= computeHorizontalScrollRange()) { //- getWidth()
LogUtils.w("WifiCustomWebView---> getScrollX >= computeHorizontalScrollRange()");
return false;
} else {
LogUtils.w(computeHorizontalScrollRange()+"--"+getScrollX()+"=="+"WifiCustomWebView---> true");
getParent().requestDisallowInterceptTouchEvent(true); //子view中,可以中断pager获取到事件
return true;
}
// return super.onInterceptTouchEvent(ev);
}

分享到 评论

EventBus的使用

一年多前就出现来这个技术,但是也没有怎么去了解和使用,一直以为EventBus和广播类似呢,后来才发现,EventBus更好用,类型更明确,发送的消息可以是任何自定义的对象,因此写个简单的使用,以免忘记

Github项目框架的地址:https://github.com/greenrobot/EventBus

使用步骤:

一、创建消息类(实体),用于存放消息的内容,例如:

1
2
3
4
5
6
7
8
9
10
class EventMessage{
String messge;
public void setMessge(String message){
this.message=message;
}
public String getMessage(){
return message;
}

}

二、注册消息:

EventBus.getDefault().register(this);

三、接受消息:

1
2
3
4
5
@Subscribe(threadMode = ThreadMode.MAIN)
public void showEventBuss(EventMessage event) {
String msg = "收到了消息:" + event.getMessage();
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

其中方法名无所谓,内部运用了反射的机制,获取到方法名,进行的调用,ThreadMode有四种:
/**

  • Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery
  • implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for
  • simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers
  • using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.
    */
    POSTING,

/**

  • Subscriber will be called in Android’s main thread (sometimes referred to as UI thread). If the posting thread is
  • the main thread, event handler methods will be called directly. Event handlers using this mode must return
  • quickly to avoid blocking the main thread.
    */
    MAIN,在主线程中调用执行

/**

  • Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods
  • will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single
  • background thread, that will deliver all its events sequentially. Event handlers using this mode should try to
  • return quickly to avoid blocking the background thread.
    */
    BACKGROUND,在子线程中执行

/**

  • Event handler methods are called in a separate thread. This is always independent from the posting thread and the
  • main thread. Posting events never wait for event handler methods using this mode. Event handler methods should
  • use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number
  • of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus
  • uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
    */
    ASYNC

四、注销消息:

EventBus.getDefalt().unregister(this);

分享到 评论

Fragment and FragmentActivity and FragmLayout

Android Fragment 的认知学习

由于工作中很少用到fragment 导致fragment and fragmentActivity and FragmLayout 的混淆,特此学习笔记一下。

Fragment 与FragmentActivity

Fragment是一种具有生命周期的View,能够使用Fragmentmanager进行操作
FragmentActivity是继承自Activity的子类,为兼容4.0以下版本使用的。4.0以上版本还是使用Activity的其他类型进行管理Fragment。

生命周期:

new Fragement():

onAttach -> onCreate -> onCreatView -> onActivityCreated -> onStart -> onResume

manager.remove(fragment):

onPause -> onStop -> onDestory -> onDetach

manager.add(fragment):

manager.replace:
onPause -> onStop -> onDestoryView -> onDestory -> onDetach -> (新的Fragment )onAttach -> onCreate -> onCreatView -> onActivityCreated -> onStart -> onResume

FrameLayout

是一种继承自ViewGroup的控件,能够放置很多的子控件,并且叠加在一起
可以作为容器被activity进行操作放置Fragment对象。

分享到 评论

动态设置控件的大小文本的大小

在项目中,动态的设置文本的大小,控件的大小的时候使用px2dp而不是dp2px。

分享到 评论

xutils传递json串作为请求体中文编码错误

网络请求

使用Xutils 3.0在传递JSON作为请求体的时候,中文乱码的结果解决:

方案时自定义RequestParams,设置这个params.setAsJsonContent(true);

分享到 评论

EditText的代码设置

最近工作中用到了动态设置EditText控件的内容:

动态设置EditText的输入格式需要在最后设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
editText = new EditText(context);
editText.setBackground(null);
editText.addTextChangedListener(this);
editText.setHint(mHint);
editText.setHintTextColor(getResources().getColor(R.color.cellview_edite_texthintcolor));
editText.setTextColor(getResources().getColor(R.color.cellview_edite_textcolor));
editText.setTextSize(DisplayUtil.dip2px(context, DEFALT_TEXT_SIZE));
editText.setGravity(Gravity.RIGHT);

if (length > 0) {
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
}
//

editText.setSingleLine();
editText.measure(0, 0);
w = DisplayUtil.getScreenWith(context) - w - 2 * padLeft;
h = editText.getMeasuredHeight();
MarginLayoutParams editTextmp = new MarginLayoutParams(w
, h); //item的宽高
editTextmp.setMargins(0, mt, padLeft, 0);//分别是margin_top那四个属性
RelativeLayout.LayoutParams editeparams = new RelativeLayout.LayoutParams(editTextmp);
editeparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// editeparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editText.setLayoutParams(editeparams);
if (isPassword) {
// editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
addView(editText);

分享到 评论

JAVA去除字符串中重复字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void main(String[] args) {
String s1 = "善南街道善国苑善国苑24号楼12单元14层1402";
String s2 = "枣庄滕州市龙泉街道苹果花园苹果花园9号楼3单元18层144023";
String s3 = "枣庄滕州市张汪镇邱仓/闫道沟邱仓/闫道沟";
String s = "枣庄滕州市西岗镇西岗镇沿街市场西岗镇沿街市场423号楼34单元12342号箱10235";
int l = 2;
String congfu="";
for (int i = 0; i < s.length() - l; i++) {
String ss = s.substring(i, i + l + 1);
String sss = s.replaceFirst(ss, "");
if (sss.contains(ss)) {
congfu=ss;
l++;
i--;
}
}
System.out.println(congfu);
s=s.replaceFirst(congfu, "");
System.out.println(s);
}

注意:字符串中含有“\”时

1
2
string.contains("\\");
string.replace("\\\\","");

分享到 评论