我有一個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);
}
}
});
然而,當我做這樣我得到這個結果:
注意兩個卡工作不正常膨脹但造成這種奇怪的空白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>
我沒有收到任何錯誤或崩潰。 – Tukajo
什麼顏色值是text_inverse_secondary?我希望它和CAD視圖的背景顏色不一樣。我看到您的卡片視圖顯示右上方的信息圖標,其中只顯示不顯示的文本。您是否還可以嘗試暫時將Cardview線性佈局設置爲不同的顏色以查看它們是否膨脹? – random
@random改變顏色並沒有解決任何可悲的事情。 – Tukajo