2016-03-29 51 views
3

我有一个Fragment,其中包含两个CardView s。这些CardView s的一类管理我在此设置自己:Cardview内容似乎没有正确加载或充气,没有错误

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 

} 

他们被添加到Fragment这里:

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); 
     } 
    } 
}); 

然而,当我做这样我得到这个结果:

LayoutInflation problem

注意两个卡工作不正常膨胀但造成这种奇怪的空白TEMPL吃了。这是该卡的XML文件:

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

这里是包含卡的片段中的XML:

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

我没有收到任何错误或崩溃。 – Tukajo

+0

什么颜色值是text_inverse_secondary?我希望它和CAD视图的背景颜色不一样。我看到您的卡片视图显示右上方的信息图标,其中只显示不显示的文本。您是否还可以尝试暂时将Cardview线性布局设置为不同的颜色以查看它们是否膨胀? – random

+0

@random改变颜色并没有解决任何可悲的事情。 – Tukajo

回答

1

使用recyclerView处理卡,而不是静态的个人意见。使用ViewType来区分卡片。

-1

问题是你膨胀你的看法,但没有将它添加到卡片视图! 这样做是为了解决它:

在你的初始化功能

,在末尾加上根布局,你膨胀到卡

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

这会导致应用程序挂起? – Tukajo

1

试试这个代码,我在充气用假的attachToView方法和膨胀后称为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

没有碰撞/悬挂。然而,该卡仍然显示为之前的屏幕截图。一个大空白的画布。 – Tukajo