grantonzhuang / QuestionAndSlove4Android

Question And Slove 4 Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

6 at 2016/12/19

  • desc

在 selector 的资源文件中使用 9 图,显示不正常

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <bitmap android:src="@drawable/bg_common_btn_pressed" />
    </item>

    <item android:state_pressed="false">
        <bitmap android:src="@drawable/bg_common_btn_normal" />
    </item>

</selector>
  • reslove

应该指定为 drawable 而不是 bitmap

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/bg_common_btn_pressed" android:state_pressed="true" />

    <item android:drawable="@drawable/bg_common_btn_normal" android:state_pressed="false" />

</selector>

5 at 2016/11/08

  • desc

使用 intent 调用 Android 的相机拍照的时候,返回的 uri 为 null。

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1000);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri uri = data.getData();
    // uri is null sometimes
}
  • reslove

换成 getExtras() 方法可以获得正确的数据,如果要获取文件的 uri,可以先在 intent 中指定 uri。参考官方文档

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bundle bundle = data.getExtras();
    Bitmap bitmap = bundle.get("data");
}

4 at 2016/11/07

  • desc

在使用 CollapsingToolbarLayout 包含一个 ToolBar,实现一个 “收缩设置标题,展开隐藏标题” 的需求的时候,收缩和展开的时候,出现了如下警告

requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout
  • why

代码如下,这个监听会一直调用,如果不加上判断,会一直要求重新绘制 CollapsingToolbarLayout 或者 ToolBar,所以出现警告

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
    int maxScroll = appBarLayout.getTotalScrollRange();
    float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
    if (percentage == 1) {
        if (getSupportActionBar() == null) {
            return;
        }
        getSupportActionBar().setTitle(R.string.title);
    } else {
        collapsingToolbarLayout.setTitle("");
    }
}
  • reslove 加上对标题是否非空的判断即可
if (percentage == 1) {
    if (getSupportActionBar() == null) {
        return;
    }
    if (TextUtils.isEmpty(collapsingToolbarLayout.getTitle())) {
        getSupportActionBar().setTitle(R.string.title);
    }
} else {
    if (!TextUtils.isEmpty(collapsingToolbarLayout.getTitle())) {
        collapsingToolbarLayout.setTitle("");
    }
}

3 at 2016/07/30

  • desc

在 recyclerView 中使用 swtich 控件的时候,遇到 Recycleview notifyDataSetChanged() IllegalStateException

  • why

You should move method 'setOnCheckedChangeListener()' to ViewHolder which is inner class on your adapter.

onBindViewHolder() is not a method that initialize ViewHolder. This method is step of refresh each recycler item. When you call notifyDataSetChanged(), onBindViewHolder() will be called as the number of each item times.

So If you notifyDataSetChanged() put into onCheckChanged() and initialize checkBox in onBindViewHolder(), you will get IllegalStateException because of circular method call.

click checkbox -> onCheckedChanged() -> notifyDataSetChanged() -> onBindViewHolder() -> set checkbox -> onChecked...

  • resolve
private boolean onBind;

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (onbind) {
        return;
    }
    // other opts
}

@Override
public void onBindViewHolder(YourAdapter.ViewHolder viewHolder, int position) {
    onBind = true;
    bind(holder, view);
    onBind = false;
}

2 at 2016/07/02

  • desc

ClassA 中持有对 ClassB 实例的引用, 同时 ClassB 中持有对 ClassA 的引用, 对 ClassA 或者 ClassB 的实例使用 Gson.toJson() 方法, 会引起 StackOverflowError

  • slove

使用 transient 关键字将相应的成员变量排除

public class ClassA {
    transient ClassB instance;
}
public class ClassB {
    transient ClassA instance;
}

1 at 2016/06/13

  • desc

在 SDK 23 中,Android 官方去掉了 HttpClient 等包,项目中引用的 org.apache.http 等类库无法导入

  • slove

在 module 层级的 build.gradle 文件中添加如下代码

android {
    useLibrary 'org.apache.http.legacy'
}

0 at 2016/03/08

  • desc

NestedScrollView中,使用一个ScrollAbleView比如RecyclerView的时候,RecyclerView不显示。

  • slove:

需要自定义RecyclerViewLayoutManager,以重新计算该控件的高度。

  • desc:

但是RecyclerView依旧不能滑动

  • slove:

代码如下

recyclerView.setLayoutManager(new CustomLayoutManager(this));
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);
recyclerView.setAdapter(mAdapter);

About

Question And Slove 4 Android