仿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寫的 算了 當我沒寫

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