Android开发爬坑笔记

1. 数组元素总是莫名丢失?说不定是这个原因

(1)数组list2所有元素都赋值给list1,地址一样。list1修改元素,list2也会对应跟着修改。

list1 = list2;

(2) 数组list2所有元素都赋值给list1,地址不一样。两个数组互不影响。

list1.clear();
list1.addAll(list2);

用第二种解决问题。

2. java.lang.IllegalStateException: Timer was canceled

不妨使用以下代码

if (timer != null) {
    timer.cancel();
    timer.purge();
    timer = null; // (可选)
}

3. java.lang.IllegalStateException: TimerTask is scheduled already

试试这招

if (mTimerTask != null){
     mTimerTask.cancel();  // 将原任务从队列中移除
}
mTimerTask = new MyTimerTask();  // 新建一个任务      
mTimer.schedule(mTimerTask, 3000);

4. Listview实现多种adapter布局方式容易忽略的点

  1. getView函数中convertView==null时没有设置setTag
public View getView(int position, View convertView, ViewGroup parent) {
        ViewRecord viewRecord = null;
        ViewPhase viewPhase = null;

        int type = getItemViewType(position);
        if (convertView == null) {
            inflater = LayoutInflater.from(mContext);
            if (type == Type_record) {
                convertView = inflater.inflate(R.layout.live_study_record_layout, parent, false);
                viewRecord = new ViewRecord();
                viewRecord.firstTime = (TextView) convertView.findViewById(R.id.live_study_record_first_time);
                convertView.setTag(viewRecord);
            } else {
                convertView = inflater.inflate(R.layout.live_study_item_layout, parent, false);
                viewPhase = new ViewPhase();
                viewPhase.title = (TextView) convertView.findViewById(R.id.live_study_item_title);
                convertView.setTag(viewPhase);
            }
        } else {
            if (type == Type_record) {
                viewRecord = (ViewRecord) convertView.getTag();
            } else {
                viewPhase = (ViewPhase) convertView.getTag();
            }
        }
}
  1. getItemViewType函数需从零开始赋值,并且getViewTypeCount > getItemViewType,如下2 > 1。
private static final int Type_record = 0;
private static final int Type_phase = 1;

public int getItemViewType(int position) {
        if (position == 0) {
            return Type_record;
        } else {
            return Type_phase;
        }
}

public int getViewTypeCount() {
        return 2;
}

5. 上、下、左、右滑动手势识别以及子控件挡住手势的处理。

1. 添加手势

public class GameActivity extends AppCompatActivity implements android.view.GestureDetector.OnGestureListener

    //定义手势检测器实例
    GestureDetector detector;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        //创建手势检测器
        detector = new GestureDetector(this, this);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float minMove = 0;          //最小滑动距离,可根据需求自定义设置
        float minVelocity = 0;      //最小滑动速度,demo未使用,可根据需求添加该判断
        float beginX = e1.getX();
        float endX = e2.getX();
        float beginY = e1.getY();
        float endY = e2.getY();

        if (beginX-endX > minMove && Math.abs(beginY-endY) < Math.abs(beginX-endX)) {
            Log.e(TAG, "左滑");
        } else if (beginX-endX < minMove && Math.abs(beginY-endY) < Math.abs(beginX-endX)) {
            Log.e(TAG, "右滑");
        } else if (beginY-endY > minMove && Math.abs(beginY-endY) > Math.abs(beginX-endX)) {
            Log.e(TAG, "上滑");
        } else if (beginY-endY < minMove && Math.abs(beginY-endY) > Math.abs(beginX-endX)) {
            Log.e(TAG, "下滑");
        }

        return false;
    }

滑动手势可能为左上、右下这样包含两个方向属性,所以做左右和上下滑动距离绝对值的大小判断,大的则为该滑动手势的方向。

2. 子控件挡住滑动手势的处理

为该子控件也添加上setOnTouchListener:

btn.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          return detector.onTouchEvent(event);
      }
});

6. substring(beginIndex,endIndex)

begingIndex:包括
endIndex:不包括。
用数学的表达方式为 [begingIndex, endIndex)

"abcdefg".substring(2,4) 
returns "cd"

7.Plugin Error: required plugin “Android Support” is disabled

更详细的错误信息如下:

Plugin "Google Analytics Uploader" was not loaded: required plugin "Android Support" is disabled.
Plugin "SDK Updater" was not loaded: required plugin "Android Support" is disabled.
Plugin "Android NDK Support" was not loaded: required plugin "Android Support" is disabled.
Plugin "Google App Indexing" was not loaded: required plugin "Android Support" is disabled.
Plugin "Google Cloud Tools For Android Studio" was not loaded: required plugin "Android Support" is disabled.
Plugin "Google Cloud Testing" was not loaded: required plugin "Android Support" is disabled.
Plugin "Google Services" was not loaded: required plugin "Android Support" is disabled.

解决方法:

依次点击菜单“File”——“Settings”——“Plugins”,可看到“Android Support”被取消勾选了。此时按如下步骤操作,先取消勾选其中一项,然后再勾选“Android Support”,单击“Apply”按钮后再单击“OK”按钮,Android Studio会提示重启,重启Android Studio,然后就恢复正常了。

持续更新中….

    原文作者:Tyler_X
    原文地址: https://www.jianshu.com/p/a50ec6d83a88
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞