Android - delayed clicks in ListView

前端 未结 6 1577
忘了有多久
忘了有多久 2021-02-04 03:45

I have the following structure in my app:

FragmentActivity with ViewPager holding multiple fragments managed by FragmentStatePagerAdapter using

相关标签:
6条回答
  • 2021-02-04 04:06

    What worked for me was assigning an OnItemClickListener to the ListView through setOnItemClickListener, rather than an OnClickListener to the individual list items. Obviously the button still needs its own OnClickListener, but I haven't tested that scenario.

    0 讨论(0)
  • 2021-02-04 04:11

    Not sure if this helps anyone, but I had a similar problem instead with a TableLayout. The above solutions didn't fix my issue.

    For me the problem was: android:animateLayoutChanges="true"

    Removing this allowed my button clicks within my TableLayout rows to work properly. I had to then animate my views manually instead of relying on the above property.

    0 讨论(0)
  • 2021-02-04 04:19

    I had a similar problem and it took me 2 days to debug and solve it. I have a ListAdapter which creates several TextViews in a LinearLayout for every list item. Each TextView has it's own OnClickListener, because I need to handle clicks on each item.

    When I changed the implementation so that I reuse the Views the OnClickListener stopped working correctly. On 4.4.2 most clicks worked but sometimes there was no reaction until I scrolled in the list. On 2.3 the first clicks would not work and then all clicks where handled in a burst.

    In my special case I created all the View in Java code and not by inflating resources. And the critical point was, that I did set the LayoutParams of the LinearLayout even when the view was reused (this seems to be more safe, then assuming that the reused view has the correct layout parameters). When I don't set the LayoutParams when reusing everything works fine! Here is the critical code:

    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout tapeLine = null;
        if (convertView != null && convertView instanceof LinearLayout && ((LinearLayout)convertView).getChildCount() == 4) tapeLine = (LinearLayout) convertView; // Reuse view
        else tapeLine = new LinearLayout(activity);
        if (convertView == null) { // Don't set LayoutParams when reusing view
            ViewGroup.LayoutParams tapeLineLayoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            tapeLine.setLayoutParams(tapeLineLayoutParams);
        }
        ScrollingTape scrollingTape = calculatorHolder.getCalculator().getScrollingTape();
        int tapeWidthPx = parent.getWidth();
        TapeLineTextSizeInfo tapeLineTextSizeInfo = calculatorHolder.getTapeLineTextSizeHelper().createTapeLineTextSizeInfo(tapeWidthPx);
        ScrollingTapeLine line = scrollingTape.getLine(position);
        tapeLine.setOrientation(LinearLayout.HORIZONTAL);
        int tapeBackgroundColor = getBackgroundColor(line);
        tapeLine.setBackgroundColor(tapeBackgroundColor);
        addColumnViews(tapeLine, line, tapeLineTextSizeInfo);
        tapeLine.setTag(R.id.scrollingtapeadapter_viewtag_position, position);
        tapeLine.setOnLongClickListener(longClickListener);
        tapeLine.setOnClickListener(remainClickListener);
        return tapeLine;
    }
    

    What is the background for this strange behaviour of the list view? I did a bit of debugging and research in the Android sources. When android updates a view there are two important steps onMeasure and onLayout. The getView method of the ListAdapter is not only called to draw the view but also earlier during onMeasure. In this later case the view is created but it is not yet registered in the event chain to handle click events.

    When a view which has been created for onMeasure is reused later to be acutally drawn to the screen, it must be register from the Android system to handle click events. For this special case the Android develepers had done something, which could be considered as a dirty hack. A special flag in the LayoutParams is used to decide that the view has to be registered in the event change.

    Now my problem: by resetting the LayoutParams also when a view is reused, this flag was always reset. Therefore the Android system would not register the view and events would not come through.

    to summarize: when resusing a view in getView of a ListAdapter don't overwrite the LayoutParams because they keep internal information of the android system.

    0 讨论(0)
  • 2021-02-04 04:20

    In case anybody wondered how I solved this. Basically I had to simplify my layouts. It seems that when you have complex nested structures events can take too long to bubble and if you start scrolling list at the same time event can trigger wrong action. I trimmed the layouts by switching to RelativeLayout as much as possible and that seemed to help a lot

    0 讨论(0)
  • 2021-02-04 04:24

    I ran into same problem, but in my case the solution was not to keep the references to views, which caused problems with ListView's view caching. After properly implementing getView() method with use of converView, all strange behaviour with lost / unexpected click calls was gone.

    0 讨论(0)
  • 2021-02-04 04:24

    It seems you are executing some blocking process(like invoking webservices or opening files) in event thread, so your Event thread is blocked. If this is the case, Please process your blocking code into another thread than Event THread.

    0 讨论(0)
提交回复
热议问题