2016-03-29 14 views
3

Ich habe eine Fragment, die zwei CardView s enthält. Diese CardView s verwaltet werden durch eine Klasse programmiere ich mich hier:Cardview Inhalte scheinen nicht korrekt zu laden oder aufzublasen, keine Fehler

public class PersonCardView extends CardView { 

    private ImageView mImageView; 
    private TextView mNameTextView; 
    private TextView mAgeTextView; 
    private TextView mLocationTextView; 
    private Context mContext; 

    public interface Callback { 
     void onClicked(Kid kid); 
     void onClicked(Parent parent); 
    } 
    private Callback mCallback; 
    public void setCallback(Callback cb) { 
     mCallback = cb; 
    } 

    public PersonCardView(Context context) { 
     super(context, null, 0); 
     initialize(context, null, 0); 
     mContext = context; 
    } 

    public PersonCardView(Context context, AttributeSet attrs) { 
     super(context, attrs, 0); 
     initialize(context, attrs, 0); 
     mContext = context; 
    } 

    public PersonCardView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     initialize(context, attrs, defStyle); 
     mContext = context; 
    } 

    private void initialize(Context context, AttributeSet attrs, int defStyle) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
     View root = inflater.inflate(R.layout.card_person, this, true); 

     mImageView = (ImageView) root.findViewById(R.id.icon1); 
     mNameTextView = (TextView)root.findViewById(R.id.name_text); 
     mAgeTextView = (TextView)root.findViewById(R.id.age_text); 
     mLocationTextView = (TextView) root.findViewById(R.id.location_text); 
    }    

    public void displayParent(final Parent parent){ 
     if (parent !=null){ 
      // name 
      mNameTextView.setText(parent.getFullName()); 

      // profile image 
      mImageView.setImageResource(R.drawable.ic_photo); 
      if (!TextUtils.isEmpty((parent.getParentProfile().getProfileImage()))) { 
       Picasso.with(mContext) 
         .load(parent.getParentProfile().getProfileImage()) 
         .resize(Constants.PROFILE_IMAGE_WIDTH, Constants.PROFILE_IMAGE_HEIGHT) 
         .centerInside() 
         .into(mImageView); 
       mImageView.setVisibility(View.VISIBLE); 
      } 

      // age 
      if (!TextUtils.isEmpty(parent.getParentProfile().getDate_of_birth())) { 
       DateTime now = new DateTime(); 
       DateTime bd = DateTime.parse(parent.getParentProfile().getDate_of_birth()); 
       final String s = TimeUtils.toAges(now.getMillis() - bd.getMillis()); 
       mAgeTextView.setText(s); 
       mAgeTextView.setVisibility(View.VISIBLE); 
      } else { 
       mAgeTextView.setVisibility(View.GONE); 
      } 

      if (!TextUtils.isEmpty(parent.getParentProfile().getHome_town())) { 
       mLocationTextView.setText(parent.getParentProfile().getHome_town()); 
       mLocationTextView.setVisibility(View.VISIBLE); 
      } else { 
       mLocationTextView.setVisibility(View.GONE); 
      } 

      // handler 
      mNameTextView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (mCallback != null) { 
         mCallback.onClicked(parent); 
        } 
       } 
      }); 
      mImageView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (mCallback != null) { 
         mCallback.onClicked(parent); 
        } 
       } 
      }); 

     } 
    } 

    public void displayKid(final Kid kid) { 

     if (kid != null) { 

      // name 
      mNameTextView.setText(kid.getUser().getFullName()); 

      // profile image 
      mImageView.setImageResource(R.drawable.ic_photo); 
      if (!TextUtils.isEmpty((kid.getKidProfile().getProfileImage()))) { 
       Picasso.with(mContext) 
         .load(kid.getKidProfile().getProfileImage()) 
         .resize(Constants.PROFILE_IMAGE_WIDTH, Constants.PROFILE_IMAGE_HEIGHT) 
         .centerCrop() 
         .into(mImageView); 
       mImageView.setVisibility(View.VISIBLE); 
      } 

      // age 
      if (!TextUtils.isEmpty(kid.getKidProfile().getDate_of_birth())) { 
       DateTime now = new DateTime(); 
       DateTime bd = DateTime.parse(kid.getKidProfile().getDate_of_birth()); 
       final String s = TimeUtils.toAges(now.getMillis() - bd.getMillis()); 
       mAgeTextView.setText(s); 
       mAgeTextView.setVisibility(View.VISIBLE); 
      } else { 
       mAgeTextView.setVisibility(View.GONE); 
      } 

      if (!TextUtils.isEmpty(kid.getKidProfile().getLocation())) { 
       mLocationTextView.setText(kid.getKidProfile().getLocation()); 
       mLocationTextView.setVisibility(View.VISIBLE); 
      } else { 
       mLocationTextView.setVisibility(View.GONE); 
      } 

      // handler 
      mNameTextView.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (mCallback != null) { 
         mCallback.onClicked(kid); 
        } 
       } 
      }); 
      mImageView.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (mCallback != null) { 
         mCallback.onClicked(kid); 
        } 
       } 
      }); 

     } 

    } //-displayKid 

} 

Und sie werden die Fragment hier hinzugefügt:

PersonCardView cardViewOne = (PersonCardView) mRoot.findViewById(R.id.person_card_one); 
PersonCardView cardViewTwo = (PersonCardView) mRoot.findViewById(R.id.person_card_two); 

cardViewOne.displayKid(mInboxmessage.getFrom_user().getKid()); 
cardViewOne.setVisibility(View.VISIBLE); 
cardViewOne.setCallback(new PersonCardView.Callback() { 
    @Override 
    public void onClicked(Kid kid) { 
     if (mCallback != null) { 
      mCallback.onViewKid(kid); 
     } 
    } 

    @Override 
    public void onClicked(Parent parent) { 
     //Not used here 
    } 
}); 

cardViewTwo.displayParent(mInboxmessage.getFrom_user().getKid().getParent()); 
cardViewTwo.setVisibility(View.VISIBLE); 
cardViewTwo.setCallback(new PersonCardView.Callback() { 
    @Override 
    public void onClicked(Kid kid) { 
     //Not used here 
    } 

    @Override 
    public void onClicked(Parent parent) { 
     if (mCallback != null) { 
      mCallback.onViewParent(parent); 
     } 
    } 
}); 

Allerdings, wenn ich so bekomme ich dies als Ergebnis:

LayoutInflation problem

Beachten Sie die beiden Karten nicht richtig aufblasen, sondern führen zu diesem seltsamen leeren templ aß. Hier ist die XML-Datei für die Karte:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:padding="16dp"> 


<ImageView 
    android:id="@+id/icon1" 
    android:layout_width="64dp" 
    android:layout_height="64dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginBottom="6dp" 
    android:layout_marginLeft="16dp" 
    android:layout_marginStart="16dp" 
    android:layout_marginTop="8dp" 
    android:src="@drawable/ic_photo" /> 

<LinearLayout 
    android:id="@id/content1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="2dp" 
    android:layout_marginRight="16dp" 
    android:layout_marginEnd="16dp" 
    android:clickable="true" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/name_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:ellipsize="marquee" 
     android:maxLines="1" 
     android:textColor="@color/primary" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold" 
     android:text="name" /> 

    <TextView 
     android:id="@+id/age_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:ellipsize="marquee" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/text_inverse_secondary" 
     android:text="sample text" /> 

    <TextView 
     android:id="@+id/location_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:ellipsize="marquee" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/text_inverse_secondary" 
     android:text="sample text" /> 

    <TextView 
     android:id="@+id/phone_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:ellipsize="marquee" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/text_inverse_secondary" 
     android:text="sample text" 
     android:visibility="gone" /> 

    <TextView 
     android:id="@+id/email_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:ellipsize="marquee" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/text_inverse_secondary" 
     android:text="sample text" 
     android:visibility="gone" /> 

    <TextView 
     android:id="@+id/facebook_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:ellipsize="marquee" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/text_inverse_secondary" 
     android:text="sample text" 
     android:visibility="gone" /> 
</LinearLayout> 
</LinearLayout> 

Und hier ist das XML für das Fragment enthält die Karten:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@id/refreshLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/header_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="2dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" 
      android:layout_marginTop="4dp" 
      android:background="@android:color/white" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@id/android:icon1" 
       android:layout_width="16dp" 
       android:layout_height="16dp" 
       android:layout_gravity="center_vertical" 
       android:background="@drawable/ic_tiny_horn" /> 

      <TextView 
       android:id="@id/type_text" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="8dp" 
       android:layout_marginStart="8dp" 
       android:layout_weight="1" 
       android:gravity="left" 
       android:text="@string/profile_image_update" 
       android:textColor="@color/primary" /> 

      <TextView 
       android:id="@id/time_text" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="4dp" 
       android:layout_marginStart="4dp" 
       android:layout_weight="1" 
       android:gravity="right" 
       android:text="Posted" 
       android:textColor="@color/primary" /> 

     </LinearLayout> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/line_dark_separator_light" /> 

     <LinearLayout 
      android:id="@id/content1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:layout_marginEnd="16dp" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="16dp" 
      android:layout_marginStart="8dp" 
      android:layout_marginTop="8dp" 
      android:background="@android:color/white" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/profile_image" 
       android:layout_width="32dp" 
       android:layout_height="32dp" 
       android:layout_marginLeft="8dp" 
       android:layout_marginStart="8dp" 
       android:src="@drawable/ic_photo" /> 

      <TextView 
       android:id="@+id/from_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="6dp" 
       android:layout_marginStart="6dp" 
       android:ellipsize="marquee" 
       android:maxLines="1" 
       android:text="From" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="@color/primary" 
       android:textStyle="bold" /> 


     </LinearLayout> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/line_dark_separator_light" /> 

     <TextView 
      android:id="@+id/subject_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginBottom="8dp" 
      android:layout_marginEnd="16dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" 
      android:layout_marginStart="16dp" 
      android:layout_marginTop="8dp" 
      android:background="@android:color/white" 
      android:ellipsize="marquee" 
      android:text="Subject" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/text_secondary" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/line_dark_separator_light" /> 

     <TextView 
      android:id="@+id/message_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginBottom="16dp" 
      android:layout_marginEnd="16dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" 
      android:layout_marginStart="16dp" 
      android:layout_marginTop="8dp" 
      android:background="@android:color/white" 
      android:ellipsize="marquee" 
      android:text="Message" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/text_secondary" /> 

     <com.myapp.mycode.cards.CommentCardView 
      android:id="@+id/comment_card" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:visibility="gone" 
      app:cardBackgroundColor="@android:color/white" 
      app:cardElevation="@dimen/card_elevation" /> 

     <com.myapp.mycode.cards.PlaydateCardView 
      android:id="@+id/playdate_card" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      app:cardBackgroundColor="@android:color/white" 
      app:cardElevation="@dimen/card_elevation" 
      android:visibility="gone" /> 

     <com.myapp.mycode.cards.PlaydateBookingCardView 
      android:id="@+id/playdate_booking_card" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:visibility="gone" 
      app:cardBackgroundColor="@android:color/white" 
      app:cardElevation="@dimen/card_elevation" /> 

     <com.myapp.mycode.cards.GroupCardView 
      android:id="@+id/group_card" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:visibility="gone" 
      app:cardBackgroundColor="@android:color/white" 
      app:cardElevation="@dimen/card_elevation" /> 


     <com.myapp.myocde.cards.PersonCardView 
      android:id="@+id/person_card_one" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      app:cardBackgroundColor="@android:color/white" 
      app:cardElevation="@dimen/card_elevation" 
      android:visibility="gone" /> 

     <com.myapp.mycode.cards.PersonCardView 
      android:id="@+id/person_card_two" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      app:cardBackgroundColor="@android:color/white" 
      app:cardElevation="@dimen/card_elevation" 
      android:visibility="gone" /> 

    </LinearLayout> 
</ScrollView> 
</android.support.v4.widget.SwipeRefreshLayout> 
+0

Ich erhalte keine Fehler oder Abstürze. – Tukajo

+0

Welcher Farbwert ist text_inverse_secondary? Ich hoffe, es ist nicht dasselbe wie die Hintergrundfarbe der CAD-Ansicht. Ich sehe, dass Ihr Kartenansicht mit dem Info-Symbol oben rechts angezeigt wird, es ist nur der Text, der nicht angezeigt wird. Können Sie auch versuchen, cardview linearlayouts vorübergehend mit einer anderen Farbe zu versehen, nur um zu sehen, ob sie aufgeblasen sind? – random

+0

@Random Änderung der Farbe hat leider nichts behoben. – Tukajo

Antwort

1

Verwenden recyclerView die Karten anstelle von statischen einzelnen Ansichten zu handhaben. Verwenden Sie ViewType, um zwischen Karten zu unterscheiden.

-1

Das Problem ist, dass Sie Ihre Ansicht aufblasen, aber nicht zur Kartenansicht hinzugefügt haben !!! dies tun, es zu beheben:

in Ihrer Funktion INITIALISIEREN, am Ende fügen Sie Ihre Wurzel Layout der aufgeblasenen Sie auf die Karte

private void initialize(Context context, AttributeSet attrs, int defStyle) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(
      Context.LAYOUT_INFLATER_SERVICE); 
     View root = inflater.inflate(R.layout.card_person, this, true); 

     mImageView = (ImageView) root.findViewById(R.id.icon1); 
     mNameTextView = (TextView)root.findViewById(R.id.name_text); 
     mAgeTextView = (TextView)root.findViewById(R.id.age_text); 
     mLocationTextView = (TextView) root.findViewById(R.id.location_text); 
     // ADD YOUR VIEW TO CARD 
     this.addView(root); 
}  
+0

Das verursacht die Anwendung zu hängen? – Tukajo

1

diesen Code Versuchen ich für attachToView in aufblasen falsch gebraucht Methode und nach der Inflation namens addView().

private void initialize(Context context, AttributeSet attrs, int defStyle) { 
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(
         Context.LAYOUT_INFLATER_SERVICE); 
       View root = inflater.inflate(R.layout.card_person, this, false); 
                   // i Used false here 

       mImageView = (ImageView) root.findViewById(R.id.icon1); 
       mNameTextView = (TextView)root.findViewById(R.id.name_text); 
       mAgeTextView = (TextView)root.findViewById(R.id.age_text); 
       mLocationTextView = (TextView) root.findViewById(R.id.location_text); 
       addView(root); 
      } 
+0

Kein Absturz/Hängen. Die Karte erscheint jedoch immer noch als Screenshot. Eine große leere Leinwand. – Tukajo