1.必须设置setLayoutManager
2.在onCreateViewHolder方法中填充布局的时候,inflate的最后一参数必须为false
1 |
View itemview = mInflater.inflate(R.layout.box_list_item, parent, false); |
3.Adapter类的getItemCount方法返回的数量必须是大于0的数量
1.必须设置setLayoutManager
2.在onCreateViewHolder方法中填充布局的时候,inflate的最后一参数必须为false
1 |
View itemview = mInflater.inflate(R.layout.box_list_item, parent, false); |
3.Adapter类的getItemCount方法返回的数量必须是大于0的数量
使用gson进行数据解析后,必须在混淆脚本中添加如下代码,如果不加的话json解析就会失败。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
##---------------Begin: proguard configuration for Gson ---------- # Gson uses generic type information stored in a class file when working with fields. Proguard # removes such information by default, so configure it to keep all of it. -keepattributes Signature # For using GSON @Expose annotation -keepattributes *Annotation* # Gson specific classes -keep class sun.misc.Unsafe { *; } #-keep class com.google.gson.stream.** { *; } -keepclassmembers class * { public <init> (org.json.JSONObject); } # Application classes that will be serialized/deserialized over Gson -keep class com.bcoder.test.datatypes.** { *; } ##---------------End: proguard configuration for Gson ---------- |
其中-keep class com.bcoder.test.datatypes.** { *; }这一行class和.**之间改成你的Bean类所在的包名
我们知道,当app的屏幕进行旋转或者其他的配置变化的时候,Fragment会执行它的周期变化,重新创建Fragment并加载(除非设置了setRetainInstance(true)),那么在重建的时候Fragment中的变量就会被重新初始化,运行中的状态将无法保留。
setArgument中的Bundle可以用来存放各种需要保持的数据,即使Fragment由于配置改变而重建,这些保存的数据也不会被清除。
下面我们将通过一个实例来测试一下,在实例的Fragment中共有3个TextView和一个Button,为了使测试的内容更清晰,下面把三个TextView对应的字符串列表如下:
textview1->banana, textview2->apple, textview3->pear
对应的三个字符串:
测试程序的布局代码和java代码如下:
Fragment部局文件的代码
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 31 32 33 34 35 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bcoder.myapplication.TestFragment" android:orientation="vertical"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textview1" android:id="@+id/textview1"/> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview2"/> <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textview3"/> <Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button1"/> </LinearLayout> |
Fragment的Java代码
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package com.bcoder.myapplication; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class TestFragment extends Fragment { private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM3 = "param3"; private String mParam1; private String mParam2; private String mParam3; TextView mTextView1; TextView mTextView2; TextView mTextView3; Button button1; View fragmentView; public TestFragment() { // Required empty public constructor } public static TestFragment newInstance(String param1, String param3) { TestFragment fragment = new TestFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM3, param3); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam3 = getArguments().getString(ARG_PARAM3); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { fragmentView = inflater.inflate(R.layout.fragment_test, container, false); initViews(); mTextView1.setText(mParam1); mTextView2.setText(mParam2); mTextView3.setText(mParam3); return fragmentView; } private void initViews() { mTextView1 = (TextView) fragmentView.findViewById(R.id.textview1); mTextView2 = (TextView) fragmentView.findViewById(R.id.textview2); mTextView3 = (TextView) fragmentView.findViewById(R.id.textview3); button1 = (Button) fragmentView.findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mParam1 = "banana2"; mTextView1.setText(mParam1); mParam2 = "apple"; mTextView2.setText(mParam2); mParam3 = "pear click"; getArguments().putString(ARG_PARAM3, mParam3); mTextView3.setText(mParam3); } }); } @Override public void onAttach(Context context) { super.onAttach(context); } @Override public void onDetach() { super.onDetach(); } } |
主Activity的代码
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 |
package com.bcoder.myapplication; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { LinearLayout layoutContainer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); TestFragment fragment = TestFragment.newInstance("banana", "pear"); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.layoutFragmentContainer, fragment); transaction.commit(); } private void initViews() { layoutContainer = (LinearLayout) findViewById(R.id.layoutFragmentContainer); } } |
主Activity的布局文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bcoder.myapplication.MainActivity" android:orientation="vertical"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layoutFragmentContainer"> </LinearLayout> </LinearLayout> |
程序启动时显示默认如下内容:
textview1和textview3的值取自getArgument并显示在了屏幕上,当点击按钮时会重新给三个String变量赋值,并显示到textview中,如下图:
这时三个字符串变量都有了新的值,那么当我们把屏幕进行一下旋转操作后,看结果如何呢?
由图可知,字符串1回到了最初的状态(创建Fragment时传入的值),而字符串2被清除了。
字符串3因为在点击按钮的时候重新被保存在getArgument中,而没有任何的变化的被保留下来了。
因此大家在给Fragment传入参数的时候最好通过setArgument进行保存,而不是简单的通过一个变量进行保存。
注意:
测试环境Python3.5 + Mac
1 2 3 4 5 6 7 8 9 10 |
import os import sys print(os.getcwd()) #执行python脚本的路径 print(os.curdir) print(os.path.abspath('.')) #执行python脚本的路径 print(sys.argv[0]) #所执行的脚本文件的相对路径 print(os.path.realpath(sys.argv[0])) #所执行的脚本文件的绝对路径 print(os.path.split(os.path.realpath(sys.argv[0]))[0]) #所执行的脚本文件所在目录的路径 |
1.删除一行:
方法一:将光标定位到要删除的行,输入dd
方法二:输入1d,然后按回车键
2. 删除多行:
输入ndd删除多行,n为要删除的行数
3. 定位到行首:输入0
4. 定位到行尾:输入$
5. 进入到编辑模式:输入i
6. 进入到编辑模式,并新加行:输入o
7. 定位到文档起始位置:输入:1或者gg
8. 定位到文档结尾:输入:$或者G(大写)
9. 查找文字:输入/,然后输入要搜索的内容(区分大小写)
10. 查找下一个:执行上面的命令后,输入n查找下一个符合条件的内容
11. 替换文字:
:s/home/disk2/ 替换当前行第一个 home 为 disk2
:s/home/disk2/g 替换当前行所有 home 为 disk2
:n,$s/home/disk2/ 替换第 n 行开始到最后一行中每一行的第一个 home 为 disk2
:n,$s/home/disk2/g 替换第 n 行开始到最后一行中每一行所有 home 为 disk2
(n 为数字,若 n 为 .,表示从当前行开始到最后一行)
:%s/home/disk2/(等同于 :g/home/s//disk2/) 替换每一行的第一个 home 为 disk2
:%s/home/disk2/g(等同于 :g/home/s//disk2/g) 替换每一行中所有 home 为 disk2
12. 显示行号:输入:set nu
13. 跳转到某行:ngg,n为要跳转到行号
在mac os下ssh登录的时候出现了如下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is SHA256:5vuaB3OnzyT/hXdeckzmIRARZMczEj3fJJXYVV8sg10. Please contact your system administrator. Add correct host key in /Users/liuderu/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /Users/liuderu/.ssh/known_hosts:4 RSA host key for 10.10.10.10 has changed and you have requested strict checking. Host key verification failed. |
解决办法:在命令行执行如下命令
1 |
vi ~/.ssh/known_hosts |
在打开的编辑器中删除相关的信息,按Esc,输入wq保存即可
基本使用方法:
命令格式:
watch [options] command
选项:
-b, –beep 播放提示音当命令返回非0退出代码
-c, –color interpret ANSI color sequences
-d, –differences[=<permanent>] 高亮显示每次更新的变化内容
-e, –errexit 当命令返回非0的退出代码时退出watch命令
-g, –chgexit 当命令输出内容变化时退出watch命令
-n, –interval <secs> watch执行时间间隔,单位秒,可以小为数,最小可以为0.1秒
-p, –precise attempt run command in precise intervals
-t, –no-title 关闭顶部Every 2.0s:提示
-x, –exec pass command to exec instead of “sh -c”
-h, –help display this help and exit
-v, –version output version information and exit
For more details see watch(1).
使用场景:
使用举例:
1 |
watch -n 1 -d 'ls -l' |
直接上代码
Calendar calendar = Calendar.getInstance();
//显示当前时间
System.out.println(calendar.getTime());
//各种日期格式化
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);
String str = format.format(calendar.getTime());
System.out.println(str);
format = new SimpleDateFormat(“yyyyMMdd”);
str = format.format(calendar.getTime());
System.out.println(str);
format = new SimpleDateFormat(“yyyy/MM/dd”);
str = format.format(calendar.getTime());
System.out.println(str);
format = new SimpleDateFormat(“yyyy/M/d”);
str = format.format(calendar.getTime());
System.out.println(str);
//获取时间戳
str = String.valueOf(calendar.getTimeInMillis());
System.out.println(“时间戳: ” + str);
//减去一天
calendar.add(Calendar.DAY_OF_YEAR, -1);
format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);
str = format.format(calendar.getTime());
System.out.println(str);
//减去一个月
calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);
str = format.format(calendar.getTime());
System.out.println(str);
//减去一年
calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);
str = format.format(calendar.getTime());
System.out.println(str);
//只获取日期,不包含时间
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);
str = format.format(calendar.getTime());
System.out.println(str);
//获取本月的第一天
calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);
str = format.format(calendar.getTime());
System.out.println(str);
//时间戳字符串转日期时间
calendar = Calendar.getInstance();
str = String.valueOf((calendar.getTimeInMillis() – 60 * 1000) ); //减去一分钟
calendar.setTimeInMillis(Long.parseLong(str));
format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);
str = format.format(calendar.getTime());
System.out.println(str);
打开Terminal终端
重启apache服务:sudo /usr/sbin/apachectl restart
停止apache服务:sudo /usr/sbin/apachectl stop
启动apache服务:sudo /usr/sbin/apachectl start
要给app做一个定时提醒的功能,使用到了AlarmManager,因为涉及到很多天以后的提醒,所以不好测试,但是通过adb是可以查看系统已保存的AlarmManager 的,具体命令如下:
1 |
adb shell dumpsys alarm |
如果我们只想看我们自己的应用的情况,则用linux的过滤器也可以(MAC下使用未在Windows下测试),命令如下:
1 |
adb shell dumpsys alarm|grep ebox |