打开布局,将项目移至下方

时间:2019-07-16 15:00:51

标签: android material-design

如何实现以下功能。我有一个回收站清单。当我单击下面的项目时,出现的项目说明。但是同时,列表项必须向下移动才能变为打开的描述下方。

2 个答案:

答案 0 :(得分:1)

跟随链接Folding Cell

然后单击克隆或下载按钮进行下载 您可以使用git

下载后,将其解压缩,然后将折叠单元文件夹从解压缩的文件夹复制到您的项目文件夹 我的意思是你必须将折叠式单元格平行于应用程序文件夹

一旦您这样做,请转到您的android studio并进行以下更改:

打开settings.gradle 文件可能看起来像这样

include ':app'

您必须将其更新为以下内容:

include ':app'
    ':folding-cell'

之后,打开您的应用程序build.gradle文件 并将以下行添加到依赖项部分

implementation project(':folding-cell')

完成后,只需同步项目 构建完成后,您将得到与以前一样的错误

所以现在您必须打开folding-cell build.gradle文件并将androidx库更新为android

只需将整个文件更新为此

    apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'com.android.support:appcompat-v7:28.0.0'

    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:2.22.0'
}

现在再次构建项目

一切都会正常运行

如果您遇到任何问题,请告诉我!

答案 1 :(得分:0)

对于这种情况,您想利用多个ViewHolders,每个ViewHolders都有自己的布局:

data class Item(
    title: String, 
    description: String, 
    showDescription: Boolean = false
)

open class TitleViewHolder(
    itemView: View,
    onClicked: (Int) -> Unit
) : RecyclerView.ViewHolder(itemView) {

    private val titleView: TextView = itemView.findViewById(R.id.title)

    init {
        itemView.setOnClickListener { 
            if (adapterPosition != RecyclerView.NO_POSITION)
                onClicked(adapterPosition)
        }
    }

    fun bindTo(item: Item) {
        titleView.text = item.title
        onBind(item)
    }

    protected open fun onBind(item: Item) {
        // Do nothing
    }

}

class TitleAndDescriptionViewHolder(
    itemView: View,
    onClicked: (Int) -> Unit
) : TitleViewHolder(itemView) {
    private val description: TextView = itemView.findViewById(R.id.description)

    override fun onBind(item: Item) {
        description.text = item.description
    }
}

override fun getItemViewType(position: Int) = if (items[position].showDescription)
    R.layout.item_with_description else R.layout.item_with_title

override fun createViewHolder(...) {
    val view = LayoutInflater.from(...).inflate(viewType, parent, false)

    // Note: This presentation logic can be easily extracted, utilizing a
    // Lambda passed to the adapter which takes (Item) and returns Unit.
    // You'd then need a `setList` method which uses `DiffUtil` to update the
    // List but the result should be the same.
    val onClick: (Int) -> Unit = {
        val dataItem = data[it]
        val newItem = dataItem.copy(showDescription = !dataItem.showDescription)
        data[it] = newItem
        notifyItemChanged(it)
    }

    return when (viewType) {
        R.layout.item_with_description -> TitleAndDescriptionViewHolder(view)
        R.layout.item_with_title -> TitleViewHolder(view)
    }
}

override fun bindViewHolder(holder, position) {
    holder.bindTo(data[position])
}

单击某项时,将更新其showDescription标志,并通知适配器该位置发生了更改。这将触发构建新项目,执行适当的动画等。