草庐IT

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

coder 2024-07-02 原文

我有一个包含两个 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);
        }
    }
});

但是,当我这样做时,结果是:

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

有关android - Cardview 内容似乎没有正确加载或充气,没有错误的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  4. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  5. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  6. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  7. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  8. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  9. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  10. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

随机推荐