0

我的问题基本上是在 RecyclerView 中时看不到 CardView 的图像。

我会注意到,我在应用程序的其他部分使用此卡,它不会产生任何问题 - 此外,卡 XML 中的一些部分也与此无关,因为它们与单击动画时相关。

编辑:我尝试了相同的代码,但在onCreateViewHolder一个仅包含图像的不同 .xml 文件中进行了膨胀,并且效果很好......因此问题缩小到card_layout.xml不显示图像和描述。

我要做的是在 RecyclerView 中显示这张卡: 在此处输入图像描述

我最终得到的是: 在此处输入图像描述

通常我使用不同的布局管理器,但为了简单起见,我使用标准 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>
4

3 回答 3

0

您没有将视图绑定到onBindViewHolder方法中的行,像这样使用它:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
    final ViewHolder myHolder = (ViewHolder ) holder;
    Favourite fav=Favourites.get(position);
    holder.mItemImage.setImageBitmap etc ...
于 2016-08-25T09:14:43.217 回答
0

如果我没记错的话,你忘了在ImageView. IE mItemImage.setImageResource(R.drawable.apple);

参考 :

如何在android的imageview中设置图像?

于 2016-08-25T09:14:55.440 回答
0

有几件事可能会发生。

首先,该图像的大小是多少?如果它很大,则可能无法正确加载位图。如果它在应用程序的其他部分加载正常,我怀疑这是问题,但绝对是一个考虑因素。

你的布局有一种奇怪的设置。建议尽可能避免使用嵌套布局,因为它们会影响性能。你提到你因为动画而省略了一些东西,但我强烈建议你做一些改变。如果你能给我一个布局应该是什么样子的全貌,我可以调整这个;但这是一个粗略的想法:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardCornerRadius="2dp"
cardview:cardElevation="4dp"
cardview:cardUseCompatPadding="true">

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <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="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="share" />

        <Button
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="reveal" />

    </LinearLayout>

    <ImageView
        android:id="@+id/card_item_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:adjustViewBounds="true"
        android:contentDescription="image"
        android:scaleType="centerCrop"
        android:layout_above="@+id/card_price"
        android:src="@drawable/ic_dashboard_black_24dp" />

    <TextView
        android:id="@+id/card_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_margin="2dp"
        android:text="price"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/card_shipping"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/card_price"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:text="shipping"
        android:textAlignment="viewEnd"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/card_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/card_price"
        android:layout_alignEnd="@+id/card_shipping"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="8dp"
        android:text="desc"
        android:textAlignment="center"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@+id/card_shipping_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/card_shipping"
        android:layout_toStartOf="@+id/card_shipping"
        android:contentDescription="desc"
        android:src="@drawable/ic_event_black_24dp" />

</RelativeLayout>

很有可能,你应该可以用按钮摆脱那个 LinearLayout,但我真的不知道你想要它去哪里。

让我知道这是否有帮助。

于 2016-08-25T17:42:50.743 回答