使用RecyclerView Android加载列表中的所有数据
我正在使用使用RecyclerView Android加载列表中的所有数据,android,android-recyclerview,recyclerview-layout,Android,Android Recyclerview,Recyclerview Layout,我正在使用RecyclerView显示列表中的数据。 但是RecyclerView只加载用户可见的行数据 因为我的列表只有10-12个项目,我想一次加载所有项目。 如何使用RecyclerView处理此问题 RecycleServicewFragment.java public class RecyclerViewFragment extends Fragment { List<Movie> movieList = new ArrayList<>(); Recycler
RecyclerView
显示列表中的数据。
但是RecyclerView
只加载用户可见的行数据
因为我的列表
只有10-12个项目,我想一次加载所有项目。
如何使用RecyclerView
处理此问题
RecycleServicewFragment.java
public class RecyclerViewFragment extends Fragment {
List<Movie> movieList = new ArrayList<>();
RecyclerView myRecyclerView;
MyMovieAdapter myMovieAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reccycler_view_frament,container,false);
Movie movie = new Movie("ABC","1-1-1");
movieList.add(movie);
movie = new Movie("PLK","4-4-4");
movieList.add(movie);
movie = new Movie("XYZ","2-2-2");
movieList.add(movie);
movie = new Movie("ZMMD","3-3-3");
movieList.add(movie);
movie = new Movie("ABC","1-1-1");
movieList.add(movie);
movie = new Movie("PLK","4-4-4");
movieList.add(movie);
movie = new Movie("XYZ","2-2-2");
movieList.add(movie);
movie = new Movie("ZMMD","3-3-3");
movieList.add(movie);
movie = new Movie("ABC","1-1-1");
movieList.add(movie);
movie = new Movie("PLK","4-4-4");
movieList.add(movie);
movie = new Movie("XYZ","2-2-2");
movieList.add(movie);
movie = new Movie("ZMMD","3-3-3");
movieList.add(movie);
movie = new Movie("ABC","1-1-1");
movieList.add(movie);
movie = new Movie("PLK","4-4-4");
movieList.add(movie);
movie = new Movie("XYZ","2-2-2");
movieList.add(movie);
movie = new Movie("ZMMD","3-3-3");
movieList.add(movie);
movie = new Movie("ABC","1-1-1");
movieList.add(movie);
movie = new Movie("PLK","4-4-4");
movieList.add(movie);
movie = new Movie("XYZ","2-2-2");
movieList.add(movie);
movie = new Movie("ZMMD","3-3-3");
movieList.add(movie);
movie = new Movie("ABC","1-1-1");
movieList.add(movie);
movie = new Movie("PLK","4-4-4");
movieList.add(movie);
movie = new Movie("XYZ","2-2-2");
movieList.add(movie);
movie = new Movie("ZMMD","3-3-3");
movieList.add(movie);
movie = new Movie("ABC","1-1-1");
movieList.add(movie);
movie = new Movie("PLK","4-4-4");
movieList.add(movie);
movie = new Movie("XYZ","2-2-2");
movieList.add(movie);
movie = new Movie("ZMMD","3-3-3");
movieList.add(movie);
myRecyclerView = view.findViewById(R.id.recyclerView);
myMovieAdapter = new MyMovieAdapter(movieList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
myRecyclerView.setLayoutManager(layoutManager);
myRecyclerView.setItemAnimator(new DefaultItemAnimator());
myRecyclerView.setAdapter(myMovieAdapter);
return view;
}
}
public class Movie {
private String movieName;
private String releaseDate;
public Movie(String movieName, String releaseDate){
this.movieName = movieName;
this.releaseDate = releaseDate;
}
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
}
movie\u adapter.xml
<android.support.v7.widget.CardView
android:elevation="3dp"
android:background="#9372"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:weightSum="10"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:id="@+id/name"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="ABC"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/release_date"
android:layout_weight="5"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="1-1-1"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
请用此更新代码。我做的唯一一个小改动是将高度从50 dp
更改为wrap\u content
<android.support.v7.widget.CardView
android:elevation="3dp"
android:background="#9372"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:weightSum="10"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:weight="5"
android:layout_height="wrap_content"
android:id="@+id/name"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="ABC"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/release_date"
android:weight="5"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="1-1-1"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
update your card View
更新您的卡视图
recyclerView的整个理念是加载用户可见的内容,如果您想显示10-12个项目并一次全部加载,您可以使用listView(它是recyclerView的前身)。
您可以使用的另一个选项——您可以使用滚动布局,将项目动态添加到布局中,或在运行前在布局中声明它们(我建议使用滚动布局,因为不同的手机有不同的屏幕大小,如果没有滚动布局,您可能会发现在某些设备中,视图无法放入屏幕,并且无法在屏幕上显示)。
如果您不想使用滚动布局-您可以始终使用约束布局-但是如果您想在一个屏幕上显示10-12个项目,请注意,您不会对每个项目都有太大的屏幕大小(您的布局可能看起来非常压缩,用户不直观)您能为您已经尝试过的内容发布代码吗?如果是一个很长的列表,并且它有一个定义的层次,那么所有的内容都不能一次显示出来,即使列表视图也只显示用户可见的数据user@krispo.uke我已经更新了我的code@AkashPal,是否有任何方法可以加载所有数据,而不只是加载正在显示的数据我不知道为什么你不使用列表视图我想使用listView可以解决我的问题,但是我有一个问题,因为Google已经提出了RecyclerView作为listView的改进,它是处理“列表数据”的更灵活的控件还有更多的优点,那么在我们的应用程序中使用listView而不是RecylcerView会好吗?正如您所说的,recyclerView的整个想法是对listView的改进,通常,listView不能回收其项目,所以您可以一次加载所有项目(这就是您想要实现的目标),listView唯一的主要问题是,如果有大量项目,性能会受到影响,尽管如此,我真的不认为只有10个项目会有问题。ofc你能做的最好的测试就是在真实的手机上运行应用程序,自己看看。
<android.support.v7.widget.CardView
android:elevation="3dp"
android:background="#9372"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:weightSum="10"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:weight="5"
android:layout_height="wrap_content"
android:id="@+id/name"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="ABC"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/release_date"
android:weight="5"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="1-1-1"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
update your card View