简体   繁体   English

在 recyclerView 中设置 cardView 可见性的问题

[英]Problem with setting visibility of cardView in recyclerView

Hello I have a problem with setting the visibility of the cardView part.您好,我在设置cardView部分的可见性时遇到问题。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout(..........)>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        (..........)>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/titleLayout"
            (..........)>

            <TextView
                android:id="@+id/text_view_title"
                (..........)/>

            <androidx.constraintlayout.widget.ConstraintLayout 
                android:visibility="gone" <<<<<<<<<
                android:id="@+id/expandableView"
                (..........)>

                <TextView
                    android:id="@+id/text_view_description"
                    (..........) />

                <TextView
                    android:id="@+id/text_view_priority"
                   (..........) />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

How you can see I divided the cardView into two parts.你怎么看我把cardView分成两部分。 In my showDescription() method I want to change the visibility of description section to visible or (if it's already visible) to gone.在我的showDescription()方法中,我想将描述部分的可见性更改为可见或(如果它已经可见)消失。

View cardView = inflater.inflate(R.layout.note_item, container, false);
        final ConstraintLayout expandableView = cardView.findViewById(R.id.expandableView);

        adapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(final DocumentSnapshot documentSnapshot, int position) {

                Note note = documentSnapshot.toObject(Note.class);
                int pathInt = position;
                String path =String.valueOf(pathInt);
                showDescription(expandableView);
            }
        });
        return RootView;
    }
    public void showDescription (ConstraintLayout expandableView){
        if(expandableView.getVisibility()== View.VISIBLE){
            expandableView.setVisibility(View.GONE);
        }else {
            expandableView.setVisibility(View.GONE);
        }
    }
}

I don't know why but after click on cardView (no matter which one) nothing happens.我不知道为什么,但是在点击cardView (无论是哪一个)之后什么都没有发生。 Maybe it's since my app don't know on witch cardView execute that method?也许是因为我的应用程序不知道在女巫cardView上执行该方法? How I can make it works?我怎样才能让它工作?

The problem is here:问题在这里:

final ConstraintLayout expandableView = cardView.findViewById(R.id.expandableView);

You are making your expandableView final .您正在使您的expandableView final final variable can not be modified. final变量不能修改。 Remove the final keyword:删除final关键字:

ConstraintLayout expandableView = cardView.findViewById(R.id.expandableView);

I think also you should place your showDescription function's code inside setOnItemClickListener instead of passing `expandableView' to a function.我认为您还应该将showDescription函数的代码放在setOnItemClickListener中,而不是将“expandableView”传递给 function。

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

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