adjustpan not work in expandablelistview when click second time for the same edittext

前端 未结 2 531
别跟我提以往
别跟我提以往 2021-01-26 16:56

first time,click an edittext in expandablelistview,the whole layout scrolled ,and user can see edittext. then click softkeyboard finish to collapse softkeyboard and click this e

相关标签:
2条回答
  • 2021-01-26 17:27

    By many days spent,I found a solution. It is a hack.

    1. put you edittext wrapped in linearlayout.
    2. detect softkeyboard show by visiable layout change.

       public final void setKeyboardListener(final OnKeyboardVisibilityListener listener) {
      
       final View activityRootView = getWindow().getDecorView()
          .findViewById(android.R.id.content);
           activityRootView.getViewTreeObserver()
          .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      
            private final Rect r = new Rect();
            private boolean wasOpened;
      
            @Override
            public void onGlobalLayout() {
              activityRootView.getWindowVisibleDisplayFrame(r);
      
              int heightDiff = activityRootView.getRootView().getHeight()
                  - (r.bottom - r.top);
              boolean isOpen = heightDiff > 100;
      
              if (isOpen == wasOpened) {
                return;
              }
      
              wasOpened = isOpen;
              listener.onVisibilityChanged(isOpen);
            }
          });
         }
      
        public interface OnKeyboardVisibilityListener {
      
         void onVisibilityChanged(boolean visible);
          }
      
    3. when the softkeyboard show,pick up a textview in listview item or expandablelistview child item. then set it text for any thing, the whole view scrolled as normal! code like this:

      activity.setKeyboardListener(new OnKeyboardVisibilityListener() {
        @Override
      public void onVisibilityChanged(boolean visible) {
      if (visible) {
        Message message = new Message();
        message.what = 999;
        handler.sendMessage(message);
        }
       }
      });
      private Handler handler = new Handler() {
       @Override
      public void handleMessage(Message msg) {
      if (msg.what == 999) {
        if (mDataMap.size() > 0) {
        for (int i = 0; i < expandablelistview.getChildCount(); i++) {
          View v = expandablelistview.getChildAt(i);
          TextView tvCannotSee = (TextView) v.findViewById(R.id.tv_cannot_see );
          if (tvCannotSee != null) {
            //this is the key hack to make adjuspan work! and scroll the whole view up as normal
            tvCannotSee.setText("this is an android bug");
            return;
            }
          }
         }
         return;
        }
       super.handleMessage(msg);
       }
      };
      
    4. this textview show invisble to user for proper purpose.if it is a expandablelistview and the edittext in childitem,the textview update must be in a childitem.

    0 讨论(0)
  • 2021-01-26 17:32

    I also faced the same issue, but after googling the issue for some time i ended up the problem as below.

    1) Add android:windowSoftInputMode="adjustPan" tag inside activity of manifest file

        <activity
            android:name=".MainActivity"
            android:configChanges="locale|keyboardHidden|orientation|screenSize|layoutDirection|screenLayout"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"></activity>
    

    2) add android:descendantFocusability="beforeDescendants" attribute to your expandable listview as below.

        <ExpandableListView
                    android:id="@android:id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/textDate"
                    android:layout_below="@+id/relativeLayout2"
                    android:descendantFocusability="beforeDescendants"
                    android:layout_margin="@dimen/margin_meium"
                    android:groupIndicator="@android:color/transparent" />
    
    0 讨论(0)
提交回复
热议问题