【问题标题】:Display n number of images in a ScrollView / RecyclerView using Glide with Kotlin in Android在 Android 中使用 Glide 和 Kotlin 在 ScrollView / RecyclerView 中显示 n 个图像
【发布时间】:2019-03-24 22:44:20
【问题描述】:

我是 Kotlin 语言的新手,所以我被困在我的项目中间。

问题 1:

我的本​​地目录中有一组图像文件(n...文件数),但我无法使用“For Loop”在图像视图中显示所有文件,因为 Kotlin For 循环完全不同,我无法正确理解。

MainActivity.kt

package com.cambarkro.kotlintest

import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.Toast
import com.bumptech.glide.Glide
import java.io.File
import java.util.*
import kotlin.collections.ArrayList

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val linearLayout = findViewById<LinearLayout>(R.id.linearLayoutid)
    val imageView = ImageView(this)

    val path  = File(Environment.getExternalStorageDirectory().toString() + File.separator + "WhatsApp/Media/.Statuses/" )
    val statusFilesAry = path.listFiles() // Storing files in array

    if(path.exists())
    {
        if(statusFilesAry != null && statusFilesAry.size > 0 )
        {
            for(statusFile in statusFilesAry)
            {
                Glide
                        .with(this)
                        .load(statusFile)
                        .into(imageView)

                //linearLayout.addView(imageView)
            }
            linearLayout.addView(imageView)
        }


        Toast.makeText(this, "path found", Toast.LENGTH_SHORT).show()
    }
    else
    {
        Toast.makeText(this, "path Not found", Toast.LENGTH_SHORT).show()
    }
}

}

上面的代码只显示了一张图片。

问题 2:

从上面的 MainActivity.kt 代码中,有一个变量“statusFilesAry”,其中包含所有图像文件的数组。我需要按 lastmodified 对这些数组元素进行排序。

在 java 中,我们可以使用 Comparator 对其进行排序,但在 Kotlin 中?

【问题讨论】:

    标签: android kotlin comparator android-glide


    【解决方案1】:

    尝试在 kotlin 中更改下面的 java 代码

            if(path.exists())
            {
                if(statusFilesAry != null && statusFilesAry.size > 0 )
                {
                  LinearLayout ll = new LinearLayout(this);
              ll.setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
              LinearLayout.LayoutParams.MATCH_PARENT));
              ll.setOrientation(LinearLayout.VERTICAL);
                    for(statusFile : statusFilesAry)
                    {
                      ImageView imageView = ImageView(youractivity.this)
                    LinearLayout.LayoutParams layoutParams = new   LinearLayout.LayoutParams(100, 100);
                    imageView.setLayoutParams(layoutParams);
    
                            Glide
                                .with(this)
                                .load(statusFile)
                                .into(imageView)
    
                        ll.addView(imageView)
                    }
                 ScrollView scrollView = new ScrollView(this);
                 scrollView.setFillViewport(true);
                 scrollView.addView(ll);
                    linearLayout.addView(scrollView)
                }
    

    Kotlin sort array by value in range

    https://grokonez.com/kotlin/kotlin-array-sort-sortby-sortwith

    【讨论】:

    • 感谢您的宝贵回答,它对我有很大帮助并且工作正常。我还有一个问题,如何在回收站视图中实现这一点?因为滚动视图占用大量内存,如果您有时间请帮我解决这个问题,Thank you very much.
    • 看看这个例子......如果有任何问题请自行尝试android.jlelse.eu/…
    【解决方案2】:

    这是修改后的 kotlin 代码

    if (path.exists())
        {
            if (statusFilesAry != null && statusFilesAry.size > 0)
            {
                val ll = LinearLayout(this)
                ll.setOrientation(LinearLayout.VERTICAL)
                for(statusFile in statusFilesAry)
                {
                    val imageView = ImageView(this)
                    val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
                    imageView.setLayoutParams(layoutParams)
                    Glide
                            .with(this)
                            .load(statusFile)
                            .into(imageView)
                    ll.addView(imageView)
                }
                val scrollView = ScrollView(this)
                scrollView.setFillViewport(true)
                scrollView.addView(ll)
                linearLayout.addView(scrollView)
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      相关资源
      最近更新 更多