autojs模仿qq消息列表侧拉置顶删除菜单

2021-08-12  本文已影响0人  牙叔教程

牙叔教程 简单易懂

效果展示

效果.gif

思路

使用安卓qq可知, 消息列表每次只能有一个侧拉菜单被打开


思路.png

autojs版本

9.0.4

你将学到以下知识点

脚本概况

几个重要方法

难点

代码讲解

1. 导入类
importClass(Packages.androidx.recyclerview.widget.LinearLayoutManager);
importClass(Packages.androidx.recyclerview.widget.RecyclerView);
importClass(android.content.pm.ActivityInfo);
importClass(android.view.WindowManager);
importClass(Packages.androidx.recyclerview.widget.DividerItemDecoration);
importClass(android.graphics.BitmapFactory);
importClass(android.graphics.Paint);
importClass(android.graphics.Color);
importClass(Packages.androidx.recyclerview.widget.GridLayoutManager);
importClass(android.graphics.drawable.GradientDrawable);
importClass(android.view.View);
importClass(android.view.MotionEvent);
importClass(android.widget.HorizontalScrollView);
importClass(android.view.VelocityTracker);
2. 导入子模块
let dataList = require("./dataList");
let setItemOnTouch = require("./setItemOnTouch");
let onItemTouchListener = require("./onItemTouchListener");
let onScrollListener = require("./onScrollListener");
3. UI界面
ui.layout(
  <vertical>
    <text text="牙叔教程 简单易懂" textSize="28sp" textColor="#fbfbfe" bg="#00afff" w="*" gravity="center"></text>
    <androidx.recyclerview.widget.RecyclerView id="recyclerView"></androidx.recyclerview.widget.RecyclerView>
  </vertical>
);
4. 设置recyclerView属性
let layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
let recycleAdapter = createRecyclerViewAdapter(dataList);
recyclerView.setAdapter(recycleAdapter);
recycleAdapter.notifyDataSetChanged();
recyclerView.addOnItemTouchListener(onItemTouchListener);
recyclerView.addOnScrollListener(onScrollListener);
5. 获取头像
function getProfilePhoto() {
  let filePath = files.path("./牙叔正方形128.jpg");
  let img = images.read(filePath);
  let bitmap = img.getBitmap();
  events.on("exit", () => {
    bitmap.recycle();
    img.recycle();
  });
  return bitmap;
}
6. 创建rv的adapter
function createRecyclerViewAdapter(dataList) {
  let boxXml = (
    <HorizontalScrollView h="100dp">
      <horizontal id="horizontalParent">
        {/* 消息 */}
        <horizontal>
            ...
        </horizontal>
        {/* 菜单 */}
        <horizontal id="menuParent" h="match_parent">
            ...
        </horizontal>
      </horizontal>
    </HorizontalScrollView>
  );
  return RecyclerView.Adapter({
    onCreateViewHolder: function (parent, viewType) {
      log("onCreateViewHolder");
      // 视图创建
      let view;
      let holder;
      view = ui.inflate(boxXml, parent, false);
      holder = JavaAdapter(RecyclerView.ViewHolder, {}, view);
        ...
      return holder;
    },
    onBindViewHolder: function (holder, position) {
      log("onBindViewHolder");
      // 数据绑定
        ...
    },
    getItemCount: function () {
      return dataList.length;
    },
    getItemViewType: function (position) {
      if ((position & 1) == 0) {
        return "EvenNumber";
      } else {
        return "OddNumber";
      }
    },
    getDataList: function () {
      return dataList;
    },
  });
}
7. isTouchCoordinateInViewRange
function isTouchCoordinateInViewRange(view, x, y) {
  let LocationOnScreen = view.getLocationOnScreen();
  let frame = new Rect();
  view.getHitRect(frame);
  log("LocationOnScreen = ");
  log(LocationOnScreen);
  log("frame = " + frame);
  // 触摸点: x=511.5, y=180.5
  // Rect(0, 0 - 1470, 300)
  let left = LocationOnScreen[0];
  let top = LocationOnScreen[1];
  let width = frame.width();
  let height = frame.height();
  frame.left = left >= 0 ? left : 0;
  frame.top = top;
  frame.right = left + width;
  frame.bottom = top + height;
  log("子控件当前区域frame");
  log(frame);
  if (frame.contains(x, y)) {
    log("在控件内" + view);
    return true;
  } else {
    log("不在控件内" + view);
    log("触摸点: x=" + x + ", y=" + y);
    return false;
  }
}

名人名言

思路是最重要的, 其他的百度, bing, stackoverflow, 安卓文档, autojs文档, 最后才是群里问问

----牙叔教程

声明

部分内容来自网络
本教程仅用于学习, 禁止用于其他用途

上一篇下一篇

猜你喜欢

热点阅读