android - Cardview 内容似乎没有正确加载或充气,没有错误

标签 android xml android-fragments android-cardview

我有一个包含两个 CardViewFragment。这些 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

请注意,这两张卡片没有正常充气,但会产生这个奇怪的空白模板。这是卡片的 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>

这是包含卡片的 fragment 的 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>

最佳答案

使用 recyclerView 来处理卡片而不是静态的单独 View 。使用 ViewType 区分卡片。

关于android - Cardview 内容似乎没有正确加载或充气,没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274376/

相关文章:

java - onConfigurationChanged() 没有为keyboardHidden 触发

android - Kotlin fragment 类型不匹配

android - 如何获取 viewPager 的选定选项卡?

android - 如何使用 iTextLibrary 在动态 pdf 中添加页眉和页脚?

c# - 如何将数据从 Windows 窗体保存到 XML 文件?

xml - 如何在xslt中提取这种格式

android - 使用 Fragments 和 MVP 模式的正确方法

android - eclipse xml 编辑器显示滚动屏幕图形布局

android - Android 中的应用内结算 - 问题

java - 谷歌地图, map 正在加载,但未完全加载