1,判断 RecyclerView滑动到底部
loadingMoreListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(!loading && !recyclerView.canScrollVertically(1)){
loading = true;
loadMoreDate();
}
}
}
2,android studio 3.1版本依赖库中的libs jar 文件正确依赖方法为 api 。而不是
implementation
https://stackoverflow.com/questions/49628149/import-class-from-3rd-party-library-android-studio-3-1-graddle-3-1?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
这是最新版as3.1发现的问题。
3,view加阴影需要设置背景颜色和elevation属性值
4,模拟touch事件
val motionEvent = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis() + 100,
MotionEvent.ACTION_UP,
0f,
0f,
0
)
video_view.dispatchTouchEvent(motionEvent)
5,跳转应用市场评分
lin_shop.setOnClickListener {
val appPackageName = packageName // getPackageName() from Context or Activity object
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appPackageName")))
} catch (anfe: android.content.ActivityNotFoundException) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.daywin.tv")))
}
}
6,使用系统的播放视频控件videoview,点击事件需要使用touch方法。onclick是没有效果的
video_view.setOnTouchListener { p0, p1 ->
when (p1?.action) {
MotionEvent.ACTION_UP -> {
if (fullscreen) {//全屏点击隐藏按钮
hideControl()
if (lin_hd_choose.visibility == View.VISIBLE) {//隐藏画质
lin_hd_choose.visibility = View.GONE
}
}
}
}
true
}
7,设置textview字体大小
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13f)
8,根据名字获取drawable的文件
resources.getIdentifier("lunch", "drawable", packageName)
9,recycleView 单选,不用通过notifyDataSetChanged()方法
var selectedPos = 0
override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
if (holder is ViewHolder) {
holder.apply {
//选择效果
if (selectedPos == position) {
lin.isSelected = true
} else {
lin.isSelected = false
}
lin.setOnClickListener {
//选择效果
notifyItemChanged(selectedPos)
selectedPos = layoutPosition
notifyItemChanged(selectedPos)
}
}
}
}
10 recycleView加边距
article_recycle.addItemDecoration(object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
outRect.set(0, 30, 0, 0)
}
})
11 NestedScrollView 嵌套recycleView滑动卡顿解决方法
var grid = GridLayoutManager(activity, 2)
grid.isAutoMeasureEnabled = true
home_recycle.layoutManager = grid
home_recycle.isNestedScrollingEnabled = false
12,设置tablayout的下划线和文字一样长度
public static void wrapTabIndicatorToTitle(TabLayout tabLayout, int externalMargin, int internalMargin) {
View tabStrip = tabLayout.getChildAt(0);
if (tabStrip instanceof ViewGroup) {
ViewGroup tabStripGroup = (ViewGroup) tabStrip;
int childCount = ((ViewGroup) tabStrip).getChildCount();
for (int i = 0; i < childCount; i++) {
View tabView = tabStripGroup.getChildAt(i);
//set minimum width to 0 for instead for small texts, indicator is not wrapped as expected
tabView.setMinimumWidth(0);
// set padding to 0 for wrapping indicator as title
tabView.setPadding(0, tabView.getPaddingTop(), 0, tabView.getPaddingBottom());
// setting custom margin between tabs
if (tabView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) tabView.getLayoutParams();
if (i == 0) {
// left
setMargin(layoutParams, externalMargin, internalMargin);
} else if (i == childCount - 1) {
// right
setMargin(layoutParams, internalMargin, externalMargin);
} else {
// internal
setMargin(layoutParams, internalMargin, internalMargin);
}
}
}
tabLayout.requestLayout();
}
}
public static void setMargin(ViewGroup.MarginLayoutParams layoutParams, int start, int end) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
layoutParams.setMarginStart(start);
layoutParams.setMarginEnd(end);
} else {
layoutParams.leftMargin = start;
layoutParams.rightMargin = end;
}
}
13 显示密码
edt_password.transformationMethod = PasswordTransformationMethod.getInstance()
隐藏密码
edt_password.transformationMethod = HideReturnsTransformationMethod.getInstance()
14,从网络下载文件,java代码报错Assignments are not expressions,and only expressions are allowed in this context
do { byteCount = inputStream.read(buffer) if (byteCount == -1) { break } os.write(buffer, 0, byteCount) } while (true)
14,复制文本
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager val clip = ClipData.newPlainText("label",bank_no.text.toString()) clipboard.primaryClip = clip toa(getString(R.string.copy_succ))
15,选择年月日
val datePickerDialog = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, year, month, day -> txt_birth.text = year.toString() + "-" + (month + 1) + "-" + day }, 2000, 1, 1) datePickerDialog.show()
16,密码包含字母和数字
// fun passwordReg(postCode: String): Boolean { val reg = "^(?=.*?[A-Za-z])(?=.*?[0-9]).{6,18}\$" return Pattern.matches(reg, postCode) }
17,retrofit2全局判断返回数据,可以处理登陆成功后过一段时间登陆过期。
18,中文转换为拼音
import android.util.Log; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import java.util.Comparator; public class PinyinComparator implements Comparator