我遇到如下问题。
我有3个cardView,但最初只能看到其中一个。这样。
如果我向下滚动一点,我可以看到另一个。
多一点,
最后
似乎有一个像这样的小盒子。
我希望以适当的边距列出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;
}
p.s。我已经尝试将layout_height更改为“ match parent”,但是它只影响片段的大小。
感谢您阅读! 如果您帮我,我会很高兴!
编辑:这是我的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>