我的问题基本上是在 RecyclerView 中时看不到 CardView 的图像。
我会注意到,我在应用程序的其他部分使用此卡,它不会产生任何问题 - 此外,卡 XML 中的一些部分也与此无关,因为它们与单击动画时相关。
编辑:我尝试了相同的代码,但在onCreateViewHolder
一个仅包含图像的不同 .xml 文件中进行了膨胀,并且效果很好......因此问题缩小到card_layout.xml
不显示图像和描述。
通常我使用不同的布局管理器,但为了简单起见,我使用标准 LinearLayoutManager 上传代码,我也将一些函数留空,因为对于这个问题并不重要:
片段
public class FavoritesFragment extends Fragment {
private static final String TAG = "FavoritesFragment";
private Context mContext;
private RecyclerView mRecyclerView;
private FavoritesAdapter mFavoritesAdapter;
private ArrayList<String> testData;
public FavoritesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_favorites, container, false);
mContext = getContext();
testData = new ArrayList<>();
testData.add("0");
testData.add("1");
testData.add("2");
testData.add("3");
testData.add("4");
initRecyclerView(layout, testData);
return layout;
}
private void initRecyclerView(View layout, ArrayList<String> testData) {
mRecyclerView = (RecyclerView)layout.findViewById(R.id.recyclerViewFavorites);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(new FavoritesAdapter(testData, mContext));
mRecyclerView.addOnScrollListener(new CenterScrollListener());
}
private class FavoritesAdapter extends RecyclerView.Adapter<FavoritesAdapter.ViewHolder> {
private List<String> data;
private Context mContext;
// ViewHolder to hold the card
public class ViewHolder extends RecyclerView.ViewHolder {
private View mContainer;
private ImageView mItemImage;
private TextView mDescription;
private TextView mPrice;
private TextView mShippingDate;
public ViewHolder(View cardView) {
super(cardView);
mContainer = cardView.findViewById(R.id.card_container);
mItemImage = (ImageView)cardView.findViewById(R.id.card_item_image);
mDescription = (TextView)cardView.findViewById(R.id.card_description);
mPrice = (TextView)cardView.findViewById(R.id.card_description);
mShippingDate = (TextView)cardView.findViewById(R.id.card_shipping);
}
}
public FavoritesAdapter(List<String> data, Context context) {
mContext = context;
this.data = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.card_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return data.size();
}
}
}
片段收藏夹.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.panasonixx.asosplus.FavoritesFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewFavorites"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</FrameLayout>
card_layout.xml
<android.support.v7.widget.CardView xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/card_container"
cardview:cardCornerRadius="2dp"
cardview:cardElevation="4dp"
cardview:cardUseCompatPadding="true">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_above="@+id/card_description">
<LinearLayout
android:id="@+id/linearView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/holo_purple"
android:gravity="center_vertical"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:id="@+id/layoutButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:visibility="gone">
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:text="@string/card_reveal_buy" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:text="@string/card_reveal_share" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/card_reveal_favorite" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/card_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/image"
android:src="@drawable/test_image"
android:scaleType="centerCrop"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/price"
android:id="@+id/card_price"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_margin="2dp" />
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/shipping"
android:id="@+id/card_shipping"
android:textAlignment="viewEnd"
android:layout_alignTop="@+id/card_price"
android:layout_alignParentEnd="true"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/desc"
android:id="@+id/card_description"
android:textAlignment="center"
android:layout_above="@+id/card_price"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/card_shipping"
android:layout_marginBottom="8dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/card_image_description"
android:id="@+id/card_shipping_icon"
android:src="@drawable/ic_local_shipping"
android:layout_alignTop="@+id/card_shipping"
android:layout_toStartOf="@+id/card_shipping" />
</RelativeLayout>
</android.support.v7.widget.CardView>