简体   繁体   English

如何在卡片视图中灰卡?

[英]How to greyout a card in a card view?

I have a card view that lists a set of cards dynamically. 我有一张卡片视图,可以动态列出一组卡片。 I have used recyclerview for that. 我已经使用了recyclerview。 Now I want to disable a card(based on value)by greying out the card or something like that. 现在我想通过灰卡或类似的东西来禁用一张卡(基于价值)。 This is the layout that contains the card view 这是包含卡片视图的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="112dp"
        android:layout_height="140dp"
        android:layout_margin="10dp"
        android:clickable="true"
        android:focusable="true"
        android:scaleType="fitXY">

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="150dp"
            android:padding="8dp">

            <ImageView
                android:id="@+id/appIcon"
                android:layout_width="match_parent"
                android:layout_height="62dp"
                android:padding="5dp"
                tools:ignore="VectorDrawableCompat" />

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/appIcon"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                android:textColor="@drawable/selector"
                android:textSize="10dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/appVersion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_marginBottom="5dp"
                android:layout_marginEnd="1dp"
                android:text="version 0.0"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                android:textSize="8dp" />

        </RelativeLayout>

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

</LinearLayout> 

I have found some related queries and have tried out using selectors and greying out each field instead of the entire card. 我找到了一些相关的查询,并尝试使用选择器并灰化每个字段而不是整个卡。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@color/colorAppName" />
    <item android:state_enabled="false" android:color="@color/colorPrimary" />

</selector>

and in my class(Adapter.kt) 在我的班级(Adapter.kt)

name = itemView.findViewById(R.id.name) as TextView
if(value==true)
{
  name.isEnabled=false
}

But I want to disable the entire card. 但我想禁用整张卡。 Please help 请帮忙

Add android:background="@drawable/selector" to your RelativeLayout or LinearLayout . android:background="@drawable/selector"RelativeLayoutLinearLayout That will work for enable and disable. 这将适用于启用和禁用。

Note:- This is not related to question but if you follow. 注意: - 这与问题无关,但如果您关注。 It will good for you. 这对你有好处。

We compare if(booleanVariable) directly instead of comparison like if(booleanVariable==true) . 我们直接比较if(booleanVariable)而不是比较if(booleanVariable==true)

You can set tint to your relative layout which is direct child of card view in your item design. 您可以将色调设置为相对布局,该布局是项目设计中卡片视图的直接子项。 You have to do this in xml: 你必须在xml中这样做:

 <android.support.v7.widget.CardView
        android:layout_width="112dp"
        android:layout_height="140dp"
        android:layout_margin="10dp"
        android:clickable="true"
        android:focusable="true"
        android:scaleType="fitXY">

        <RelativeLayout
            android:id="@+id/relParent"
            android:background="#fff"
            android:layout_width="100dp"
            android:layout_height="150dp"
            android:padding="8dp">
      ....>

I have added two property in RelativeLayout first is id and second is background. 我在RelativeLayout中添加了两个属性,第一个是id,第二个是background。

Now in adapter you can do this: 现在在适配器中你可以这样做:

if(value==true)  
{
itemView.relParent.background.setColorFilter(Color.parseColor("#6F6F6F"),PorterDuff.Mode.SRC_IN)
        itemView.relParent.isEnabled=false
}
else
{
itemView.relParent.background.setColorFilter(Color.parseColor("#FFFFFF"),PorterDuff.Mode.SRC_IN)
        itemView.relParent.isEnabled=true
}

Note: You must have to set background to RelativeLayout from xml otherwise itemView.relParent.background will return null and your app will crash. 注意:您必须从xml将背景设置为RelativeLayout,否则itemView.relParent.background将返回null并且您的应用程序将崩溃。

Update You need to set forground color to your card you in that case do this: 更新您需要为卡片设置地面颜色,在这种情况下执行此操作:

itemView.cardDashboard.foreground= ColorDrawable(Color.parseColor("#906F6F6F"))

Here cardDashboard is card view which you are using in your item design. 这里cardDashboard是您在项目设计中使用的卡片视图。

So the final code will be like this: 所以最终的代码将是这样的:

       if(value==true)  
        {
        itemView.cardDashboard.foreground= ColorDrawable(Color.parseColor("#906F6F6F"))     
   itemView.relParent.isEnabled=false
        }
        else
        {
        itemView.cardDashboard.foreground= ColorDrawable(Color.parseColor("#006F6F6F"))
                itemView.relParent.isEnabled=true
        }

Try setting the following to all the views, 尝试将以下内容设置为所有视图,

android:duplicateParentState="true"

When this attribute is set to true, the view gets its drawable state (focused, pressed, etc.) from its direct parent rather than from itself. 当此属性设置为true时,视图将从其直接父级而不是从其自身获取其可绘制状态(聚焦,按下等)。

Then dynamically disable the layout of the item (at a particular position) resulting in disabling all its child views. 然后动态禁用项目的布局(在特定位置),从而禁用其所有子视图。

Note: Since your cardviews are in a recycler view you need to maintain the state for diffrent positions inside your adapter. 注意:由于您的cardviews位于回收站视图中,因此您需要维护适配器内不同位置的状态。

You can maintain the background states using the selectors as you did before. 您可以像以前一样使用选择器维护背景状态。

I changed your layouts by adding the above mentioned attribute:- 我通过添加上面提到的属性来改变你的布局: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="112dp"
    android:layout_height="140dp"
    android:layout_margin="10dp"

    android:id="@+id/cd"
    android:scaleType="fitXY">

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:duplicateParentState="true"
        android:padding="8dp">

        <ImageView
            android:id="@+id/appIcon"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:padding="5dp"
            android:duplicateParentState="true"
            tools:ignore="VectorDrawableCompat" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:duplicateParentState="true"

            android:text="@string/app_name"
            android:layout_below="@+id/appIcon"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:onClick="clickMe"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"

            android:textSize="10dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/appVersion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:duplicateParentState="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="1dp"
            android:text="version 0.0"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
            android:textSize="8dp" />

    </RelativeLayout>

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

</LinearLayout>

And have tested with disabling the cardview state, its child views also show the change with their color fading to light grey(haven't used selector for views so it changes to default disabled state (tested for textview)) .But for any clicklistner on the views you will have to use the saved state for the position disabled and not perform task when state is disabled. 并且已经通过禁用cardview状态进行了测试,其子视图也显示了颜色渐变为浅灰色的变化(没有使用选择器进行视图,因此它更改为默认禁用状态(测试textview))。但是对于任何clicklistner on您必须使用保存状态来禁用位置的视图,并且在禁用状态时不执行任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM