TextViews
内有CardView
个TextView
。我必须为某些条件隐藏其中一个textview.SetVisibility(View.GONE)
。即使在写<?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>
之后,它仍然可见。
请帮忙
list_item.xml
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+"\t\t"+timeString);
}
}
}
这些用于我的recyclerview。如果listitemdatedetails为空,我会尝试隐藏它 所以在CursorAdapter的onBindViewHolder方法中我试图隐藏它但它不会消失
ViewController
答案 0 :(得分:0)
基本上,在您的示例中,您无法隐藏LinearLayout中的任何TextView,它是CardView的子项,因为不允许将LinearLayout用于CardView。
如果您希望能够隐藏内部元素,请尝试将主布局设置为LinearLayout,并在其中将CardView与要隐藏的TextView放在一起。就像这样:
<?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>
然后尝试设置元素的可见性。