2

I am using RecyclerView to display the data present in a list. But RecyclerView loads only the rows data which are visible to the user.

As my list has only 10-12 items and I want to load all of them at once. How to deal with this using 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>
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Kavita Patil
  • 1,784
  • 1
  • 17
  • 30

2 Answers2

0

Please update the code with this the only minor change i did was changes height from 50 dp to 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
Akash Pal
  • 1,055
  • 8
  • 15
0

The whole idea of recyclerView is to load what's visible to the user, if you want to display 10-12 items and load them all at once you can use listView(its the ancestor to RecyclerView btw).

Another option that you can use - you can have scroll layout and add your items dynamically into the layout or declare them in your layout before the runtime(I am suggesting to use scroll layout because different phones have a different screen size, and without the scroll layout you may find out that in some devices the views just can't fit into the screen and they will not show on it).

And if you don't want to use scroll layout - you can always use constraint layout - but then if you want to show 10-12 items on 1 screen do notice that you won't have a lot of screen size for every item(you layout may look very compressed and not intuitive to the user)

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • I think the use of listView will solve my problem, but I have one question as Google has come up with RecyclerView as a ListView improvement, which is more flexible control for handling "list data" and many more advantages, so will it be fine to use listView instead of RecylcerView in our application? – Kavita Patil Feb 23 '19 at 10:20
  • 1
    Well - the whole idea of recyclerView as you said is to be an improvement for listView, generally, listView cant Recycle its items so you load them all at once (and this is what you wanted to achieve), the only main problem with listView is that with a large number of items the performance will be affected, with that said I really don't think that there should be a problem with only 10 items.ofc the best testing you can do is to run the app on real phones and see for yourself. – Tamir Abutbul Feb 23 '19 at 10:32