跟随键盘变化的布局

跟随键盘变化的布局_第1张图片跟随键盘变化的布局_第2张图片



"1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/et_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/list_item_background_normal"
            android:gravity="top|left"
            android:hint="@string/tweet_hint"
            android:padding="@dimen/space_12"
            android:textColor="@color/main_black"
            android:textSize="@dimen/text_size_16" />

        <RelativeLayout
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/list_item_background_normal"
            android:gravity="bottom"
            android:padding="@dimen/space_8" >


            <RelativeLayout
                android:id="@+id/rl_img"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:visibility="gone" >

                <ImageView
                    android:id="@+id/iv_img"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:background="@color/light_gray"
                    android:clickable="true"
                    android:scaleType="centerCrop" />

                <ImageView
                    android:id="@+id/iv_clear_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:background="@drawable/ib_clear_image_selector" />
            

            <TextView
                android:id="@+id/tv_clear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/rl_img"
                android:layout_alignParentRight="true"
                android:background="@drawable/compose_clear_selector"
                android:clickable="true"
                android:gravity="center_vertical"
                android:textColor="@color/main_gray"
                android:textSize="@dimen/text_size_14" />
    

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/list_divider_color" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/list_item_background_normal"
        android:orientation="horizontal"
        android:weightSum="4" >

        <ImageButton
            android:id="@+id/ib_picture"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:padding="@dimen/space_8"
            android:src="@drawable/compose_toolbar_picture_selector" />

        <ImageButton
            android:id="@+id/ib_mention"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:padding="@dimen/space_8"
            android:src="@drawable/compose_toolbar_mention_selector" />

        <ImageButton
            android:id="@+id/ib_trend_software"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:padding="@dimen/space_8"
            android:src="@drawable/compose_toolbar_trend_selector" />

        <ImageButton
            android:id="@+id/ib_emoji_keyboard"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:padding="@dimen/space_8"
            android:src="@drawable/compose_toolbar_emoji_selector" />
    

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/list_divider_color" />

    <FrameLayout
        android:id="@+id/emoji_keyboard_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />


private final EmojiKeyboardFragment keyboardFragment = new EmojiKeyboardFragment();
    // 用表情键盘填充帧布局容器
    getFragmentManager().beginTransaction() .replace(R.id.emoji_keyboard_fragment, keyboardFragment).commit();
    keyboardFragment.setOnEmojiClickListener(new OnEmojiClickListener() {
    @Override
    public void onEmojiClick(Emojicon v) {
        InputHelper.input2OSC(mEtInput, v);
    }
    @Override
    public void onDeleteButtonClick(View v) {
        InputHelper.backspace(mEtInput);
        }
    });



    case R.id.ib_emoji_keyboard:
       if (!keyboardFragment.isShow()) { // emoji隐藏中
             keyboardFragment.showEmojiKeyBoard();
           keyboardFragment.hideSoftKeyboard();
       } else {
           keyboardFragment.hideEmojiKeyBoard();
           keyboardFragment.showSoftKeyboard(mEtInput);
       }
       break;

/**
 * 表情键盘
 */
public class EmojiKeyboardFragment extends Fragment implements SoftKeyboardStateListener {
    private LinearLayout mEmojiContent;
    private RadioGroup mEmojiBottom;
    /** 底部工具条按钮集合,不包含删除按钮 */
    private View[] mEmojiTabs;
    private ViewPager mEmojiPager;
    private EmojiPagerAdapter adapter;
    /** 表情键盘根布局 包含显示表情的ViewPger和底部水平工具条  */
    private LinearLayout mRootView;
    private OnEmojiClickListener listener;
    public static int EMOJI_TAB_CONTENT;
    private SoftKeyboardStateHelper mKeyboardHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mRootView = (LinearLayout) inflater.inflate(R.layout.frag_keyboard, container, false);
        initWidget(mRootView);
        return mRootView;
    }

    private void initWidget(View rootView) {
        // bottom 底部工具条布局
        mEmojiBottom = (RadioGroup) rootView.findViewById(R.id.emoji_bottom);
        mEmojiBottom.setVisibility(View.VISIBLE);
        EMOJI_TAB_CONTENT = mEmojiBottom.getChildCount() - 1; // 减一是因为有一个删除按钮
        mEmojiTabs = new View[EMOJI_TAB_CONTENT];
        if (EMOJI_TAB_CONTENT <= 1) { // 只有一个分类的时候就不显示了
            mEmojiBottom.setVisibility(View.GONE);
        }
        for (int i = 0; i < EMOJI_TAB_CONTENT; i++) {
            mEmojiTabs[i] = mEmojiBottom.getChildAt(i);
            /** 给工具条按钮添加单击监听器,根据序号来切换对应的viewPager*/
            mEmojiTabs[i].setOnClickListener(getBottomBarClickListener(i));
        }
        mEmojiBottom.findViewById(R.id.emoji_bottom_del).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onDeleteButtonClick(v);
                        }
                    }
        });
        // content必须放在bottom下面初始化
        mEmojiContent = (LinearLayout) rootView.findViewById(R.id.emoji_content);
        mEmojiPager = (ViewPager) mEmojiContent.findViewById(R.id.emoji_pager);
        adapter = new EmojiPagerAdapter(getFragmentManager(), EMOJI_TAB_CONTENT, listener);
        mEmojiPager.setAdapter(adapter);
        mEmojiContent.setVisibility(View.VISIBLE);
        mKeyboardHelper = new SoftKeyboardStateHelper(getActivity().getWindow().getDecorView());
        mKeyboardHelper.addSoftKeyboardStateListener(this);
    }

    /**
     * 底部栏点击事件监听器
     *  @param indexfff
     *  @return
     */
    private OnClickListener getBottomBarClickListener(final int index) {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmojiPager.setCurrentItem(index);
            }
        }
    };

    public void setOnEmojiClickListener(OnEmojiClickListener l) {
        this.listener = l;
    }

    public void hideAllKeyBoard() {
        hideEmojiKeyBoard();
        hideSoftKeyboard();
    }

    public boolean isShow() {
        return mEmojiContent.getVisibility() == View.VISIBLE;
    }

    /**
     *  隐藏Emoji并显示软键盘
     */
    public void hideEmojiKeyBoard() {
        mEmojiBottom.setVisibility(View.GONE);
        mEmojiContent.setVisibility(View.GONE);
    }

    /**
     *  显示Emoji并隐藏软键盘
     */

    public void showEmojiKeyBoard() {
        mEmojiContent.setVisibility(View.VISIBLE);
        if (EMOJI_TAB_CONTENT > 1) {
            mEmojiBottom.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏软键盘
     */
     public void hideSoftKeyboard() {
         ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mEmojiBottom.getWindowToken(), 0);
     }

    /**
     *  显示软键盘
     */
    public void showSoftKeyboard(EditText et) {
        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(et, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 当软键盘显示时回调
     */
    @Override
    public void onSoftKeyboardOpened(int keyboardHeightInPx) {
        mEmojiBottom.setVisibility(View.GONE);
        mEmojiContent.setVisibility(View.GONE);
    }

    @Override
    public void onSoftKeyboardClosed() {}

    @Override
    public void onStop() {
        super.onStop();
        hideSoftKeyboard();
    }
}


/**
 *  表情页适配器(FragmentPagerAdapter的好处是fragment常驻内存,对于要求效率而页卡很少的表情控件最合适)
 */
public class EmojiPagerAdapter extends FragmentPagerAdapter {
    private OnEmojiClickListener listener;
    public EmojiPagerAdapter(FragmentManager fm, int tabCount, OnEmojiClickListener l) {
        super(fm);
        KJEmojiFragment.EMOJI_TAB_CONTENT = tabCount;
        listener = l;
    }

    public EmojiPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public EmojiPageFragment getItem(int index) {
        if (KJEmojiFragment.EMOJI_TAB_CONTENT > 1) {
            return new EmojiPageFragment(index, index, listener);

        } else {
            return new EmojiPageFragment(index, 0, listener);
        }


        /**
         * 显示模式:如果只有一种Emoji表情,则像QQ表情一样左右滑动分页显示
* 如果有多种Emoji表情,每页显示一种,Emoji筛选时上下滑动筛选。 */
    @Override
    public int getCount() {
       if (KJEmojiFragment.EMOJI_TAB_CONTENT > 1) {
    	   return KJEmojiFragment.EMOJI_TAB_CONTENT;
       } else {
           // 采用进一法取小数
            return (DisplayRules.getAllByType(0).size() + KJEmojiConfig.COUNT_IN_PAGE -1) / KJEmojiConfig.COUNT_IN_PAGE;
       }

   }


    public interface OnEmojiClickListener {
        void onDeleteButtonClick(View v);
        void onEmojiClick(Emojicon v);
    }
}

/** * 键盘状态监听辅助类 */
public class SoftKeyboardStateHelper implements  ViewTreeObserver.OnGlobalLayoutListener {
    /**
     * 键盘打开关闭回调接口
     */

    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);
        void onSoftKeyboardClosed();
    }

    /** 观察者集合 */
    private final List listeners = new LinkedList();
    private final View activityRootView;
    /** 键盘高度 */
    private int lastSoftKeyboardHeightInPx;
    /** 键盘是否打开标识位 */
    private boolean isSoftKeyboardOpened;

    public SoftKeyboardStateHelper(View activityRootView) {
        this(activityRootView, false);
    }

    public SoftKeyboardStateHelper(View activityRootView,  boolean isSoftKeyboardOpened) {
        this.activityRootView = activityRootView;
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        // r will be populated with the coordinates of your view that area still visible.
        // activityRootView.getWindowVisibleDisplayFrame(r);
        // 获取窗体高度变化差
        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        if (!isSoftKeyboardOpened && heightDiff > 100) {
            // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
        } else if (isSoftKeyboardOpened && heightDiff < 100) {
            isSoftKeyboardOpened = false;
            notifyOnSoftKeyboardClosed();
        }
    }

    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }

    public boolean isSoftKeyboardOpened() {
        return isSoftKeyboardOpened;
    }

    /**
     * Default value is zero (0)
     * @return last saved keyboard height in px
     */
    public int getLastSoftKeyboardHeightInPx() {
        return lastSoftKeyboardHeightInPx;
    }

    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.add(listener);
    }

    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.remove(listener);
    }

    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardOpened(keyboardHeightInPx);
            }
        }
    }

    private void notifyOnSoftKeyboardClosed() {
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardClosed();
            }
        }
    }
}

你可能感兴趣的:(android-UI)