Recyclerview未全屏显示
Recyclerview未全屏显示。以下是布局文件和我写的代码。执行后,内容显示如下图所示。高度只是突出部分。我希望内容是全屏。 我剩下的内容在这个可滚动的高亮区域内。我希望内容以全屏显示。任何帮助都会有所帮助。Recyclerview未全屏显示
版式文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/activity_search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true">
<TextView
android:id="@+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/genre"
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
-
代码:
进口android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List;
公共类setSearchData延伸RecyclerView.Adapter {
private List<SearchDisplayContents> moviesList; public class MyViewHolder extends RecyclerView.ViewHolder { public TextView title, year, genre; public MyViewHolder(View view) { super(view); title = (TextView) view.findViewById(R.id.title); genre = (TextView) view.findViewById(R.id.genre); year = (TextView) view.findViewById(R.id.year); } } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.activity_searchresults, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { SearchDisplayContents movie = moviesList.get(position); holder.title.setText(movie.getTitle()); holder.genre.setText(movie.getGenre()); holder.year.setText(movie.getYear()); } @Override public int getItemCount() { return moviesList.size(); }
}
调用功能的代码:
列表movieList =新的ArrayList <>(); RecyclerView recyclerView =(RecyclerView)findViewById(R.id.recycler_view);
setSearchData mAdapter = new setSearchData(movieList); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(mAdapter); SearchDisplayContents movie = new SearchDisplayContents("Mad Max: Fury Road", "Action & Adventure", "2015"); movieList.add(movie); movie = new SearchDisplayContents("Inside Out", "Animation, Kids & Family", "2015"); movieList.add(movie); mAdapter.notifyDataSetChanged();
你应该为你的viewholder使用不同的布局。把你有的RelativeLayout剪下来,放在不同的xml文件中,然后传给onCreateViewHolder。
我也试过,如果它是match_parent,屏幕获得全视图但没有内容显示 – Nikhil
您应该为您的视图使用不同的布局。把你有的RelativeLayout剪下来,放在不同的xml文件中,然后传给onCreateViewHolder。
对不起,我编辑了您的答案,并且无法撤消它。虽然有人接受,但不知道为什么 – xklakoux
看看你的代码 - 它看起来像预览屏幕截图中的内容与你已经静态定义的RelativeLayout中的内容相同。
您的方法存在问题。它是在RecyclerView.Adapter中引用RelativeLayout(在RecylerView的同一个xml层次结构中)作为CellViewHolder。我个人的方法是为单元格视图创建一个单独的xml布局。
按照本指南,这是非常详细: https://guides.codepath.com/android/using-the-recyclerview
上述编码已经什么工作使用不同的布局意味着一个Recycleview和其他在名单
main_layout.xml
内容<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/uslist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="@dimen/_49sdp"
android:divider="@android:color/transparent" />
</RelativeLayout>
list_content。XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true">
<TextView
android:id="@+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/genre"
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
Adapter.java
public class yourAdapter extends RecyclerView.Adapter<yourAdapter .SimpleViewHolder> {
ArrayList<UsageRPDetails> mylist;
private Context mContext;
public yourAdapter (Context context, ArrayList<yourArray or model> checklist) {
mContext = context;
mylist = checklist;
}
@Override
public yourAdapter .SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_content, parent, false);
return new yourAdapter .SimpleViewHolder(view);
}
@Override
public void onBindViewHolder(final yourAdapter .SimpleViewHolder holder, final int position) {
holder.title.setText();
holder.year.setText();
holder.genre.setText();
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public int getItemCount() {
return mylist.size();
}
@Override
public int getItemViewType(int i) {
return 0;
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.title)
TextView title;
@Bind(R.id.genre)
TextView genre;
@Bind(R.id.year)
TextView year;
public SimpleViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.title);
genre= (TextView) itemView.findViewById(R.id.genre);
genre= (TextView) itemView.findViewById(R.id.genre);
}
}
}
还写活动的Java文件调用此适配器类,并调用recycleview这项活动
请出示整个布局文件 – xklakoux
布局文件已完成。请检查 – Nikhil
请告诉我们:activity_searchresults.xml – xklakoux