2017-08-10 39 views
-1

我有一個CardView內的2 TextViews。我必須隱藏某些條件的TextView之一。即使寫入textview.SetVisibility(View.GONE),它仍然可見。 請幫助CardView裏面的TextView不會隱藏

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> 

這些在我recyclerview使用。如果listitemdatedetails是空的我試圖掩蓋它的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+"\t\t"+timeString); 
     } 

    } 
} 
+0

在你的問題中加入你的代碼 –

+0

請看看。我編輯了這個問題 – Fargo

+0

這是否甚至編譯?您正在以適配器的成員身份訪問dateDetails,但它是視圖持有者的一部分。 試試viewHolder.dateDetails.setVisibility(View.GONE); – k0bin

回答

0

基本上,在你的榜樣,你不能隱藏任何TextViews你的LinearLayout內這是CardView的子項,因爲不允許將LinearLayout用於CardView。

如果您希望能夠隱藏內部元素,請嘗試將主佈局設置爲LinearLayout,並在其內部將CardView與要隱藏的TextViews放在一起。就像這樣:

<?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> 

然後嘗試設置您的元素的可見性。