【问题标题】:In recyclerView, it seems like one cardView takes whole list view [closed]在 recyclerView 中,似乎一个 cardView 需要整个列表视图 [关闭]
【发布时间】:2019-12-16 14:27:25
【问题描述】:

我遇到了如下问题。

我有 3 个 cardView,但一开始我只能看到其中一个。像这样。

enter image description here

如果我向下滚动一点,我可以看到另一个。

enter image description here

再多一点,

enter image description here

最后,

enter image description here

好像有一个这样的小盒子。

enter image description here

我希望以适当的边距列出 cardView。 在我愚蠢的想法中,我在RecyclerView中犯了错误

这是我的 fragment_item_list_view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_item_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.androidaudiorecorder.ItemFragment">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:listitem="@layout/fragment_item"/>

这是我的 RecyclerViewAdapter 课程

    public RecordingsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.
            from(parent.getContext()).
            inflate(R.layout.fragment_item, parent, false);

    mContext = parent.getContext();

    return new RecordingsViewHolder(itemView);
}


public static class RecordingsViewHolder extends RecyclerView.ViewHolder {
    protected TextView vName;
    protected TextView vLength;
    protected TextView vDateAdded;
    protected View cardView;

    public RecordingsViewHolder(View v) {
        super(v);
        vName = (TextView) v.findViewById(R.id.file_name_text);
        vLength = (TextView) v.findViewById(R.id.file_length_text);
        vDateAdded = (TextView) v.findViewById(R.id.file_date_added_text);
        cardView = v.findViewById(R.id.fragment_item);
    }
}

这是我的 ItemFragment 课程

public class ItemFragment extends Fragment {

private ItemRecyclerViewAdapter mItemRecyclerViewAdapter;

// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;

private OnListFragmentInteractionListener mListener;

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public ItemFragment() {
}

// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static ItemFragment newInstance(int columnCount) {
    ItemFragment fragment = new ItemFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        observer.startWatching();
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_item_list, container, false);

    // Set the adapter
    RecyclerView mRecyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);

    //newest to oldest order
    llm.setReverseLayout(true);
    llm.setStackFromEnd(true);

    mRecyclerView.setLayoutManager(llm);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    mItemRecyclerViewAdapter = new ItemRecyclerViewAdapter(getActivity(), llm);
    mRecyclerView.setAdapter(mItemRecyclerViewAdapter);
    return v;
}



@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnListFragmentInteractionListener) {
        mListener = (OnListFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnListFragmentInteractionListener");
    }
}


@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

附言我已经尝试将 layout_height 更改为“匹配父级”,但它只影响片段的大小..

感谢您阅读! 如果你能帮我一把,我会很高兴的!

编辑:这是我的 fragment_item.xml 代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fragment_item"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:foreground="?android:attr/selectableItemBackground"
        android:transitionName="open_mediaplayer"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="3dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_android_black_24dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="7dp"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_gravity="center_vertical">

                <TextView
                    android:id="@+id/file_name_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="file_name"
                    android:textSize="15sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/file_length_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="00:00"
                    android:textSize="12sp"
                    android:fontFamily="sans-serif-condensed"
                    android:layout_marginTop="7dp"/>

                <TextView
                    android:id="@+id/file_date_added_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="mmm dd yyyy - hh:mm a"
                    android:textSize="12sp"
                    />
            </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

【问题讨论】:

  • 为什么你的回收站视图高度是wrap_content?你也可以显示 R.layout.fragment_item 吗?
  • 在您的R.layout.fragment_item 中,父视图的高度必须为fill_parent / match_parent。将其更改为 wrap_content
  • 似乎 fragment_item 中的项目之间存在一些边距
  • 感谢您的回答,但还是一样。我上传了我的 fragment_view.xml 代码。请检查一下!

标签: android android-recyclerview android-cardview


【解决方案1】:

将项目的宽度更改为“match_parent”,将高度更改为“wrap_content”

【讨论】:

    猜你喜欢
    • 2018-09-16
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2016-10-07
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多