The tutorial is easy to understand

Results show

Train of thought

Using Android QQ, the message list can only have one pull menu open at a time

Autojs version

9.0.4

Here’s what you’ll learn

  • RecyclerView basic use
  • Intercepts the RV touch event
  • Disable RV scrolling
  • Set the RV layout and adapter
  • Add more methods to the Adapter
  • Determines whether the child control menu is expanded
  • Gets the rect of the control
  • Gets the absolute coordinates of the control on the screen
  • Determines whether the touch coordinates are within the scope of a control
  • Set the rv scroll listener event
  • Judge the RV to scroll to the top and bottom
  • Gets the position of the rv’s first visible subview
  • HorizontalScrollView Adds a scroll listener
  • Pictures turn bitmap
  • Unscroll the slider at the bottom of the HorizontalScrollView and the shadow at the bottom of the scroll
  • HorizontalScrollView scrolls to the specified coordinates
  • Get the rv position from the subview
  • Set top and delete rv update operations
  • Img set the bitmap
  • Gets the number of RV child controls visible on the screen
  • Position is used to get the child controls visible on the screen

Script summary

  • The UI is primarily a RecyclerView, and the child control is the HorizontalScrollView
  • The HorizontalScrollView has an avatar and chat message on the left and a drop-down menu on the right
  • Expand up to one RV child control menu
  • The point is to intercept touch events at the right time. See the flowchart above for ideas

Several important methods

  • HasExpandMenuView determines whether there is a control to expand the menu
  • IsTouchCoordinateInViewRange judgment in the touch coordinates to the specified within the view
  • OnInterceptTouchEvent Intercepts touch events
  • OnTouchEvent consumes a touch event
  • PointToPosition obtains the position of rv subview by touching coordinates

The difficulties in

  • The onInterceptTouchEvent is used to decide whether to intercept touch events,

If the MotionEvent.ACTION_DOWN event returns true, it intercepts the event. If the return false is not intercepting the event, it will not respond to onTouchEvent, but only onTouchEvent

  • View. OnTouchEvent is used to determine whether to consume the touch event,

In MotionEvent.ACTION_DOWN event return true, the event is consumed, return false, the event is not consumed

The code on

1. Import the classes
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);
Copy the code
2. Import submodules
let dataList = require("./dataList");
let setItemOnTouch = require("./setItemOnTouch");
let onItemTouchListener = require("./onItemTouchListener");
let onScrollListener = require("./onScrollListener");
Copy the code
3. The UI
ui.layout(
  <vertical>
    <text text="Uncle Tooth tutorial is easy to understand." textSize="28sp" textColor="#fbfbfe" bg="#00afff" w="*" gravity="center"></text>
    <androidx.recyclerview.widget.RecyclerView id="recyclerView"></androidx.recyclerview.widget.RecyclerView>
  </vertical>
);
Copy the code
4. Set the recyclerView attribute
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);
Copy the code
5. Obtain the profile picture
function getProfilePhoto() {
  let filePath = files.path("./ uncle square 128.jpg");
  let img = images.read(filePath);
  let bitmap = img.getBitmap();
  events.on("exit".() = > {
    bitmap.recycle();
    img.recycle();
  });
  return bitmap;
}
Copy the code
6. Create an RV adapter
function createRecyclerViewAdapter(dataList) {
  let boxXml = (
    <HorizontalScrollView h="100dp">
      <horizontal id="horizontalParent">* /} {/ * news<horizontal>.</horizontal>{/ * * / menu}<horizontal id="menuParent" h="match_parent">.</horizontal>
      </horizontal>
    </HorizontalScrollView>
  );
  return RecyclerView.Adapter({
    onCreateViewHolder: function (parent, viewType) {
      log("onCreateViewHolder");
      // View creation
      let view;
      let holder;
      view = ui.inflate(boxXml, parent, false); holder = JavaAdapter(RecyclerView.ViewHolder, {}, view); .return holder;
    },
    onBindViewHolder: function (holder, position) {
      log("onBindViewHolder");
      // Data binding. },getItemCount: function () {
      return dataList.length;
    },
    getItemViewType: function (position) {
      if ((position & 1) = =0) {
        return "EvenNumber";
      } else {
        return "OddNumber"; }},getDataList: function () {
      returndataList; }}); }Copy the code
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);
  // Touch points: 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("Current region frame of child control");
  log(frame);
  if (frame.contains(x, y)) {
    log("Inside the control" + view);
    return true;
  } else {
    log("Not in the control" + view);
    log("Touch point: x=" + x + ", y=" + y);
    return false; }}Copy the code

Quotes.

Ideas are the most important, other Baidu, Bing, StackOverflow, Android documents, autoJS documents, the last is the group to ask

—- uncle tooth tutorial

The statement

This tutorial is for study only and is prohibited for other purposes