Focusable EditText inside ListView

后端 未结 13 2375
无人共我
无人共我 2020-11-21 23:56

I\'ve spent about 6 hours on this so far, and been hitting nothing but roadblocks. The general premise is that there is some row in a ListView (whether it\'s g

相关标签:
13条回答
  • 2020-11-22 00:23

    We're trying this on a short list that does not do any view recycling. So far so good.

    XML:

    <RitalinLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
      <ListView
          android:id="@+id/cart_list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scrollbarStyle="outsideOverlay"
          />
    </RitalinLayout>
    

    Java:

    /**
     * It helps you keep focused.
     *
     * For use as a parent of {@link android.widget.ListView}s that need to use EditText
     * children for inline editing.
     */
    public class RitalinLayout extends FrameLayout {
      View sticky;
    
      public RitalinLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        ViewTreeObserver vto = getViewTreeObserver();
    
        vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
          @Override public void onGlobalFocusChanged(View oldFocus, View newFocus) {
            if (newFocus == null) return;
    
            View baby = getChildAt(0);
    
            if (newFocus != baby) {
              ViewParent parent = newFocus.getParent();
              while (parent != null && parent != parent.getParent()) {
                if (parent == baby) {
                  sticky = newFocus;
                  break;
                }
                parent = parent.getParent();
              }
            }
          }
        });
    
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
          @Override public void onGlobalLayout() {
            if (sticky != null) {
              sticky.requestFocus();
            }
          }
        });
      }
    }
    
    0 讨论(0)
提交回复
热议问题