使用RecyclerView Android加载列表中存在的所有数据

时间:2019-02-23 08:05:27

标签: android android-recyclerview recycler-adapter recyclerview-layout

我正在使用RecyclerView来显示list中存在的数据。 但是RecyclerView仅加载用户可见的行数据。

由于我的list只有10到12项,因此我想一次加载所有这些项。 如何使用RecyclerView处理此问题?

RecyclerViewFragment.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;
  }
}

MyMovieAdapter.java

public class MyMovieAdapter extends RecyclerView.Adapter<MyMovieAdapter.MyMoviewHolder> {

List<Movie> movieList;

public MyMovieAdapter(List<Movie> movieList){
    this.movieList = movieList;
}

@NonNull
@Override
public MyMoviewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.movie_adapter,viewGroup,false);
    return new MyMoviewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyMoviewHolder myMoviewHolder, int i) {

    Log.d("MOVIETEST","NAME = "+movieList.get(i).getMovieName());
    myMoviewHolder.movieNameTV.setText(movieList.get(i).getMovieName());
    myMoviewHolder.movieReleaseDateTV.setText(movieList.get(i).getReleaseDate());
}

@Override
public int getItemCount() {
    return movieList.size();
}

public class MyMoviewHolder extends RecyclerView.ViewHolder{

    AppCompatTextView movieNameTV, movieReleaseDateTV;

    public MyMoviewHolder(@NonNull View itemView) {
        super(itemView);
        movieNameTV = itemView.findViewById(R.id.name);
        movieReleaseDateTV = itemView.findViewById(R.id.release_date);
    }
}

}

Movie.java

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_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>

2 个答案:

答案 0 :(得分:0)

请以此来更新代码,我所做的唯一微小更改就是将高度从50 dp更改为wrap_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

答案 1 :(得分:0)

recyclerView的整个想法是加载对用户可见的内容,如果您要显示10-12个项目并一次加载所有项目,则可以使用listView(其祖先是RecyclerView btw)。
< br /> 您可以使用的另一种选择-您可以使用滚动版式,将项目动态添加到版式中,也可以在运行时之前在版式中声明它们(我建议您使用滚动版式,因为不同的手机具有不同的功能屏幕尺寸,并且没有滚动布局,您可能会发现,在某些设备中,视图只是无法容纳在屏幕中,因此无法在屏幕上显示。

如果您不想使用滚动布局-,则可以始终使用约束布局-但是,如果您想在1个屏幕上显示10-12个项目,请注意,您不会每个项目的屏幕尺寸都很大(布局可能看起来非常压缩并且对用户不直观)