CardView 中的 TextView 不会隐藏



我在CardView内有 2 个TextViews。我必须隐藏某些条件的TextView之一,即使在写textview.SetVisibility(View.GONE)之后,它仍然可见。 请帮忙

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:padding="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/listitemtaskname"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
tools:text="Hi" />

<TextView
android:id="@+id/listitemdatedetails"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
tools:text="By" />
</LinearLayout>
>
</android.support.v7.widget.CardView>

这些在我的回收器视图中使用。如果列表项日期详细信息为空,我尝试隐藏它 所以在 CursorAdapter 的 onBindViewHolder 方法中,我试图隐藏它,但它并没有消失

public class TaskCursorAdapter extends RecyclerCursorAdapter<TaskCursorAdapter.ViewHolder> {
Context context;
Cursor cursor;

public TaskCursorAdapter(Context context, Cursor cursor){
super(context,cursor);
this.cursor=cursor;
this.context=context;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView taskname;
public TextView dateDetails;
Context context;
Cursor cursor;
public ViewHolder(View view,Context context,Cursor cursor) {
super(view);
this.context = context;
this.cursor = cursor;
taskname=(TextView)view.findViewById(R.id.listitemtaskname);
dateDetails=(TextView)view.findViewById(R.id.listitemdatedetails);
view.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// Form the content URI that represents the specific pet that was clicked on,
// by appending the "id" (passed as input to this method) onto the
// {@link PetEntry#CONTENT_URI}.
// For example, the URI would be "content://com.example.android.pets/pets/2"
// if the pet with ID 2 was clicked on.
Intent intent = new Intent(context, EditorActivity.class);
Uri currentPetUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, this.getAdapterPosition()+1);
intent.setData(currentPetUri);
context.startActivity(intent);

}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view= layoutInflater.inflate(R.layout.list_item,parent,false);
return new ViewHolder(view,context,cursor);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {
int taskColumnIndex=cursor.getColumnIndex(TaskContract.TaskEntry.NAME);
int dateColumnIndex=cursor.getColumnIndex(TaskContract.TaskEntry.DUEDATE);
int timeColumnIndex=cursor.getColumnIndex(TaskContract.TaskEntry.DUETIME);
String task=cursor.getString(taskColumnIndex);
String dateString=cursor.getString(dateColumnIndex);
String timeString=cursor.getString(timeColumnIndex);
viewHolder.cursor = cursor;
viewHolder.taskname.setText(task);
if(timeString==null && dateString==null){
//viewHolder.dateDetails.setText("");
dateDetails.setVisibility(View.GONE);
}
else {
viewHolder.dateDetails.setText(dateString+"tt"+timeString);
}
}
}

基本上,在您的示例中,您无法隐藏CardView子级LinearLayout中的任何TextView,因为不允许在CardView中使用LinearLayout。

如果您希望能够隐藏其中的元素,请尝试将主布局设置为LinearLayout,并在其中放置CardView和要隐藏的文本视图。就像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:padding="30dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/listitemtaskname"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
tools:text="Hi" />

<TextView
android:id="@+id/listitemdatedetails"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
tools:text="By" />
</android.support.v7.widget.CardView>
</LinearLayout>

然后尝试设置元素的可见性。

最新更新