尝试用kotlin做一个app(十五)

解决上一篇的几个问题

1.点击发布之后,刷新帖子列表

在dialog类中跳转到帖子发布activity中的,但是使用的却是从PostListActivity传过来的context,所以直接在PostListActivity中接受结果返回就好了

在dialog类中使用startActivityForResult,一是不能直接使用context,可以转换成activity,参考startActivityForResult经过强转换变为可用

二是强转为activity可能报错,参考android.view.ContextThemeWrapper cannot be cast to android.app.Activity

2.上传图片裁剪

之前我判断上传大图,EditText会自动适应屏幕是错的,我无意中上传的都是刚好屏幕大小的图,后来发现上传超过屏幕宽度的图,它只能显示一部分,并且屏幕不会自动滚动。所以需要对上传图片的大小做一个判断。

如果图片的宽度超过屏幕的宽度,那么需要缩小到屏幕宽度,同时它的高度也要等比例缩小:

//resize bitmap
    fun resizeBitmap(originBitmap:Bitmap):Bitmap{
        var bitmapWidth=originBitmap.width
        var bitmapHeight=originBitmap.height

        var scale:Float=1f
        //获取屏幕宽度的80%
        var screenWidth=ScreenDimen.getInstance(this).getScreenWidthPix().toFloat()
        screenWidth=(screenWidth*0.8).toFloat()

        if(bitmapWidth>screenWidth){
            scale=screenWidth/bitmapWidth
        }

        var matrix=Matrix()
        matrix.postScale(scale,scale)

        var newBitmap=Bitmap.createBitmap(originBitmap,0,0,bitmapWidth,bitmapHeight,matrix,true)
        return newBitmap
    }

之前我是直接把原始图片的二进制流编码为base64,那现在需要把裁剪后的图片转换为base64

ar newBitmap=resizeBitmap(originBitmap)
//图片转字节,字节转base64
var baos=ByteArrayOutputStream()
newBitmap.compress(Bitmap.CompressFormat.JPEG,100,baos)
var byteData:ByteArray=baos.toByteArray()
var base64ImgString=Base64.encodeToString(byteData,Base64.NO_WRAP)

 3.之前获取缩略图默认读取帖子内容的第一张图片,那要是帖子里没有图片就不能正确添加了

 这样判断一下就好

String thumb="null";
        int startPos=content.indexOf("data:image/jpeg;base64,");
        if(startPos!=-1)
        {
        //
        }    

4.检测一下输入是否完整

标题,内容,分类应该都有输入,判断它们不为空即可

 这里还可以对标题,内容的长度进行检查,但是除非万不得已,我是懒得处理这些细节了

5.帖子列表页其实可以做得更好看一些,至少点进去发现布局不那么违和

 6.为帖子列表页细节处理

->上拉刷新,下拉加载更多

https://www.cnblogs.com/vocus/p/12446597.html

 有几个问题

(1)布局使用了NestedScrollView,SwipeFreshLayout应该放在NestedScrollView外面

(2)在onCreateViewHolder中判断一下postList.size==0那么 返回的条目应该是“没有数据了”

 ->搜索框处理

遇到一个问题,因为activity上有一个点击发布文章的dialog,导致点击搜索框的editText无法弹出软键盘

这个问题不知道怎么解决,但是我现在不需要在当前页面输入文字搜索;而是点击搜索框,跳转到搜索的activity

 
 
toolbar.toolbar_search.setOnFocusChangeListener(object:View.OnFocusChangeListener{
override fun onFocusChange(v: View?, hasFocus: Boolean) {
if(hasFocus){
activity.startActivity(Intent(activity,BbsSearchActivity::class.java))
toolbar.clearFocus() //要清除一下输入焦点,不然返回之后,再次点击,焦点不会变了
}
}
})

当搜索edittext获得输入焦点的时候,跳转就行。当前初始化的时候,可以让它失去焦点。在其父布局,也就是toolbar上添加

android:focusableInTouchMode="true"
android:focusable="true"

搜索布局个简单的

 这个页面暂时不处理了,有一个参考资料下留着 基于RelativeLayout实现自动换行标签控件

->子分类导航的点击事件

点击分类按钮,跳转到子分类页面

子分类页面的布局·可折叠的toolbar

要做可折叠的toolbar,用到Material Design这个概念。我第一次听说这个词,但是网上有挺多介绍文章的;另外android studio新建scrolling activity,它就会自动生成一个可折叠的toolbar。

最简单的大概是这样

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width="match_parent"
            android:layout_height="180dp"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:background="@color/colorWhite"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:title="haha">
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="@string/test_text2" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="@string/test_text2" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="@string/test_text2" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="@string/test_text2" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="@string/test_text2" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="@string/test_text2" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="@string/test_text2" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="@string/test_text2" />
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>
    </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

有几个注意的点

(1)toolbar应该被CollapsingToolbarLayout包裹。另外这个toolbar一定得是androidx.appcompat.widget.Toolbar

(2)CollapsingToolbarLayout应该设置app:layout_scrollFlags="scroll|exitUntilCollapsed"

(3)NestedScrollView可以换成其他,但是内容高度要大于屏幕高度。我这弄很多textview就是让高度超出屏幕

 

 但是这也不是我想要的效果

~还是多分几天来做吧

猜你喜欢

转载自www.cnblogs.com/vocus/p/12544659.html
今日推荐