仿soul发布帖子

先看图才不会觉得我是耍流氓 差不多就是这样

效果图

 

1.布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/cl_contentView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.LinearLayoutCompat
            android:id="@+id/ll_top"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#FFFFFF"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <FrameLayout
                android:id="@+id/fl_left"
                android:layout_width="40dp"
                android:layout_height="match_parent">

                <android.support.v7.widget.AppCompatImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@mipmap/ic_back2" />
            </FrameLayout>

            <android.support.v7.widget.AppCompatTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:text="发表帖子"
                android:textColor="#333333"
                android:textSize="16sp"
                android:textStyle="bold" />


            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/tv_confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="10dp"
                android:paddingStart="5dp"
                android:paddingTop="10dp"
                android:paddingEnd="5dp"
                android:paddingBottom="10dp"
                android:text="发布"
                android:textColor="#007EFF"
                android:textSize="16sp" />

        </android.support.v7.widget.LinearLayoutCompat>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/cl_content_bottom"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:minHeight="45dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <android.support.constraint.Guideline
                android:id="@+id/guideline1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.5" />

            <FrameLayout
                android:id="@+id/fl_picture"
                android:layout_width="45dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"
                app:layout_constraintEnd_toStartOf="@id/guideline1"
                app:layout_constraintStart_toStartOf="parent">

                <android.support.v7.widget.AppCompatImageView
                    android:id="@+id/iv_picture_tab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@mipmap/ic_picture_light" />

            </FrameLayout>

            <FrameLayout
                android:id="@+id/fl_softboard"
                android:layout_width="45dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/guideline1">

                <android.support.v7.widget.AppCompatImageView
                    android:id="@+id/iv_softboard"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@mipmap/ic_softboard_gay" />

            </FrameLayout>

        </android.support.constraint.ConstraintLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_select_picture"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            app:layout_constraintBottom_toTopOf="@id/cl_content_bottom" />


        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/et_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@null"
            android:gravity="top"
            android:hint="请输入内容"
            android:maxHeight="100dp"
            android:maxLength="120"
            android:minHeight="50dp"
            android:paddingStart="15dp"
            android:paddingTop="5dp"
            android:paddingRight="15dp"
            android:paddingBottom="5dp"
            android:textColor="#333333"
            android:textColorHint="#999999"
            android:textSize="14sp"
            app:layout_constraintBottom_toTopOf="@id/rv_select_picture"
            app:layout_constraintTop_toBottomOf="@id/ll_top"
            app:layout_constraintVertical_weight="1" />


    </android.support.constraint.ConstraintLayout>

    <FrameLayout
        android:id="@+id/ll_picture_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:visibility="invisible">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp" />

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/iv_folder"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_gravity="center_horizontal|bottom"
            android:src="@mipmap/ic_picture_down" />
    </FrameLayout>


</android.support.v7.widget.LinearLayoutCompat>

布局效果图:

2.代码 差不多就是这样

/**
 * Just :
 * @author by Zian
 * @date on 2019/03/29 11
 */
class PublishActivity : AppCompatActivity() {

    private lateinit var mInputManager: InputMethodManager//软键盘管理类
    private var bootomHeight = 0//软件盘高度 (软件盘高度+底部虚拟按键导航栏高度)
    private var contentViewHeight = 0 //内容布局的高度
    private var hasNavigationBar: Boolean = false//是否存在底部虚拟按键导航栏
    private var navigationBarHeight: Int = 0//底部虚拟按键导航栏高度

    //从系统中筛选图片
    private lateinit var mediaLoader: MediaLoader
    private var allMediaList: MutableList<MediaEntity> = ArrayList()
    private lateinit var pickAdapter: PictureAdapter
    //从系统中筛选图片文件夹
    private var allFolderList: MutableList<MediaFolder> = ArrayList()
    private var popFolder: PopPictureFolder? = null
    //以上筛选图片部分代码和类出自 https://github.com/sucese/phoenix

    //最大选中以及选中图片列表
    private val maxSelect = 4
    private lateinit var selectPictureAdapter: PictureSelectAdapter
    private var selectMediaList: MutableList<MediaEntity> = ArrayList()

    //切换选图片或者键盘输入的状态
    private val FLAG_ENTER = 1
    private val FLAG_PICTURE = 2
    private val FLAG_NONE = 0
    private var tabFlag = FLAG_PICTURE


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(View.inflate(this, R.layout.activity_publish, null))
    }

    private fun initView() {
        mInputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        //让键盘遮挡布局 布局的高度由我们自己控制
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        //是否存在底部虚拟按键导航栏
        hasNavigationBar = checkDeviceHasNavigationBar(this)
        if (hasNavigationBar) {
            //获取其高度
            navigationBarHeight = getVirtualBarHeigh(this)
        }
        //可以在其他页面获取软件盘的高度 将其写进sharedPreferences 此时获取 如果没有 则默认一个
        bootomHeight = sharedPreferences.getInt(Constant.SP_SOFT_HEIGHT, 760)
        //内容布局的高度 = 屏幕总高度 - 软件盘弹出的高度
        contentViewHeight = getScreenHeight() - bootomHeight
        //进入页面 改变内容布局高度
        lockContentHeight()
        // 有的输入法在切换中英文或者其他情况时 键盘的高度是会改变的 所以需要一直动态改变布局高度?
        cl_contentView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                //在小米8上测试 没有navigationBar 但navigationBarHeight的高度>0
                //所以在getSoftInputHeight() > navigationBarHeight 判断此时软件盘为弹起
                if (getSoftInputHeight() > navigationBarHeight) {
                    iv_softboard.setImageResource(R.mipmap.ic_softboard_light)
                    //此时软件盘弹起 高度改变时 将高度重新写进sharedPreferences
                    if (getSoftInputHeight() != bootomHeight) { //759 1
                        val editor = sharedPreferences.edit()
                        editor.putInt(Constant.SP_SOFT_HEIGHT, getSoftInputHeight())
                        editor.apply()
                    }
                    //重新定义各个布局高度
                    bootomHeight = getSoftInputHeight()
                    contentViewHeight = getScreenHeight() - bootomHeight
                    //此时图片筛选布局可见
                    ll_picture_list.visibility = View.VISIBLE
                } else {
                    //当软件盘隐藏时 让图片筛选布局隐藏 内容布局填充
                    iv_softboard.setImageResource(R.mipmap.ic_softboard_gay)
                    if (tabFlag == FLAG_ENTER) {
                        tabFlag = FLAG_NONE
                        ll_picture_list.visibility = View.INVISIBLE
                        val params = cl_contentView.layoutParams as LinearLayoutCompat.LayoutParams
                        params.height = ViewGroup.LayoutParams.MATCH_PARENT
                 
                    }
                }
            }
        })
        initPictureView()
        initSelectPictureView()


        et_content.setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_UP) {
                tabFlag = FLAG_ENTER
                lockContentHeight()
                showSoftInput()
            }
            false
        }

        fl_picture.setOnClickListener {
            if (tabFlag != FLAG_PICTURE) {
                tabFlag = FLAG_PICTURE
                showPictureView()
            }
        }

        fl_softboard.setOnClickListener {
            if (tabFlag != FLAG_ENTER) {
                tabFlag = FLAG_ENTER
                lockContentHeight()
                showSoftInput()
            }
        }

        iv_folder.setOnClickListener { showPopFolder() }
    }


    /**
     * 初始化图片筛选视图
     */
    private fun initPictureView() {
        initPictureRecyclerView()
        mediaLoader =
                MediaLoader(this, MimeType.ofImage(), false, 1, 1)
        mediaLoader.loadAllMedia(object : MediaLoader.LocalMediaLoadListener {
            override fun loadComplete(folders: MutableList<MediaFolder>) {
                //folders:图片文件夹列表
                if (folders.size > 0) {
                    allFolderList.clear()
                    allFolderList.addAll(folders)

                    allMediaList.clear()
                    val folder = folders[0]
                    folder.isChecked = true
                    val localImg = folder.images
                    //填充数据到 图片筛选布局 (略)
                }
            }
        })
    }

    /**
     * 初始化图片选择列表
     */
    private fun initPictureRecyclerView() {
        //略
    }

    /**
     * 初始化选中图片视图
     */
    private fun initSelectPictureView() {
        //略
    }

    /**
     * 弹出文件夹筛选popuopwindow
     */
    private fun showPopFolder() {
        //略
    }

    /**
     * 获取是否存在NavigationBar
     * @param context
     * @return
     */
    fun checkDeviceHasNavigationBar(context: Context): Boolean {
        var hasNavigationBar = false
        val rs = context.resources
        val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id)
        }
        try {
            val systemPropertiesClass = Class.forName("android.os.SystemProperties")
            val m = systemPropertiesClass.getMethod("get", String::class.java)
            val navBarOverride = m.invoke(systemPropertiesClass, "com.inesv.quantitativetrade") as String
            if ("1" == navBarOverride) {
                hasNavigationBar = false
            } else if ("0" == navBarOverride) {
                hasNavigationBar = true
            }
        } catch (e: Exception) {
        }

        return hasNavigationBar
    }

    /**
     * 获取虚拟功能键高度
     * @param context
     * @return
     */
    fun getVirtualBarHeigh(context: Context): Int {
        var vh = 0
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = windowManager.defaultDisplay
        val dm = DisplayMetrics()
        try {
            val c = Class.forName("android.view.Display")
            val method = c.getMethod("getRealMetrics", DisplayMetrics::class.java)
            method.invoke(display, dm)
            vh = dm.heightPixels - windowManager.defaultDisplay.height
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return vh
    }

    /**
     * 获取屏幕高度
     */
    fun getScreenHeight(): Int {
        val dm = DisplayMetrics()
        val windowManager = this
                .getSystemService(Context.WINDOW_SERVICE) as WindowManager
        windowManager.defaultDisplay.getMetrics(dm)
        return dm.heightPixels

    }

    /**
     * 锁定内容高度
     */
    private fun lockContentHeight() {
        Logs.e(contentViewHeight)
        val params = cl_contentView.layoutParams as LinearLayoutCompat.LayoutParams
        params.height = contentViewHeight
    }

    /**
     * 展示图片布局
     */
    private fun showPictureView() {
        lockContentHeight()
        hideSoftInput()
        ll_picture_list.visibility = View.VISIBLE
        iv_softboard.setImageResource(R.mipmap.ic_softboard_gay)
        iv_picture_tab.setImageResource(R.mipmap.ic_picture_light)
    }

    /**
     * 软键盘弹起时获取高度
     */
    @SuppressLint("ObsoleteSdkInt")
    private fun getSoftInputHeight(): Int {
        val r = Rect()
        /**
         * decorView是window中的最顶层view,可以从window中通过getDecorView获取到decorView。
         * 通过decorView获取到程序显示的区域,包括标题栏,但不包括状态栏。
         */
        window.decorView.getWindowVisibleDisplayFrame(r)
        //获取屏幕的高度
        val screenHeight = window.decorView.rootView.height
        //计算软件盘的高度
        var softInputHeight = screenHeight - r.bottom

        if (hasNavigationBar) {
            softInputHeight -= navigationBarHeight
        }

        if (softInputHeight < 0) {
            softInputHeight = 0
            Logs.w("Keyboard--Warning: value of softInputHeight is below zero!")
        }
        return softInputHeight
    }

    /**
     * 编辑框获取焦点,并显示软件盘
     */
    private fun showSoftInput() {
        iv_softboard.setImageResource(R.mipmap.ic_softboard_light)
        iv_picture_tab.setImageResource(R.mipmap.ic_picture_gay)
        et_content.requestFocus()
        et_content.post { mInputManager.showSoftInput(et_content, 0) }
    }

    /**
     * 隐藏软件盘
     */
    private fun hideSoftInput() {
        mInputManager.hideSoftInputFromWindow(et_content.windowToken, 0)
    }
}

3.瞎JB写的 算了 当我没写

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章