2017-08-10 2 views
-1

Ich habe 2 TextViews in einem CardView. Ich muss einen der TextView für bestimmte Conditons verstecken. Selbst nach dem Schreiben textview.SetVisibility(View.GONE), bleibt es immer noch sichtbar. Bitte helfenTextView in einem CardView nicht verstecken

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> 

Diese wird in meinem recyclerview verwendet. Wenn listitemdatedetails leer ich versuche, es zu verbergen so in Cursor der onBindViewHolder Methode, wo ich versuche, es zu verbergen, aber es geht nicht weg

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

fügen Sie Ihren Code in Ihre Frage –

+0

Bitte werfen Sie einen Blick.Ich habe die Frage bearbeitet – Fargo

+0

Kompiliert dies überhaupt? Sie greifen auf dateDetails als Mitglied Ihres Adapters zu, aber es ist Teil des Ansichtshalters. Versuchen Sie viewHolder.dateDetails.setVisibility (View.GONE); – k0bin

Antwort

0

Grundsätzlich in Ihrem Beispiel können Sie keine der Textviews in Ihrem Linearlayout verstecken Dies ist ein untergeordnetes Element von CardView, da es nicht zulässig ist, LinearLayout in CardView zu verwenden.

Wenn Sie in der Lage sein möchten, die Elemente darin zu verstecken, versuchen Sie, Ihr Hauptlayout auf LinearLayout zu setzen, und setzen Sie das CardView mit den TextViews, die Sie ausblenden möchten. Einfach so:

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

Und dann versuchen, Sichtbarkeit auf Ihre Elemente zu setzen.

Verwandte Themen