2 回答
TA贡献1824条经验 获得超5个赞
做这个
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
TA贡献1841条经验 获得超3个赞
试试这个!
您的视图持有者 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);
}
});
添加回答
举报