我试图在单击时使Cardview扩展。我可以通过将原始Cardview大小设置为50dp,然后将其高度设置为WRAP_CONTENT来做到这一点,如下图所示:
以下是代码:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.cvCard.setLayoutParams(new ConstraintLayout.LayoutParams(holder.cvCard.getWidth(), ConstraintLayout.LayoutParams.WRAP_CONTENT));
}
});
如您所见,问题在于,通过更改Cardview的大小,它会失去边距,并且看起来没有对齐。你们可以帮我吗?
答案 0 :(得分:2)
这样做
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(YOURCARDVIEW.getVisibility() == View.VISIBLE) {
YOURCARDVIEW.setVisibility(View.GONE);
}else {
YOURCARDVIEW.setVisibility(View.VISIBLE);
}
});
在您的XML中保留visibility= gone
答案 1 :(得分:1)
尝试一下!
您的视图持有人xml代码应类似于以下内容:
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardCornerRadius="4dp"
android:clipChildren="true"
android:layout_margin="16dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--visible content-->
<LinearLayout
android:id="@+id/lytVisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="16dp"
android:gravity="center_vertical">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_print_black_24dp"
android:tint="#555555"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Click me to expand"
android:layout_marginLeft="16dp"
android:textSize="16dp"
android:textColor="#333333"/>
</LinearLayout>
<!--end visible content-->
<!--hidden content-->
<LinearLayout
android:id="@+id/lytHidden"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."/>
</LinearLayout>
<!--end hidden content-->
</LinearLayout>
</android.support.v7.widget.CardView>
这是您的Java代码:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View hiddenView = holder.itemView.findViewById(R.id.lytHidden);
hiddenView.setVisibility( hiddenView.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
});
祝你好运!