简体   繁体   中英

What is the Best way to handle a lot of data on a Recycleview

I need to load a long list of data (5000-1000) on a RecycleView , and each items of the RecycleView has many details, so 5 TextView , 1 ImageView and a ProgressBar . All the data are stored on the SQLite DB . Is it better to load an ArrayList<Object> which contains all the data and them set it to the adapter or is it better to load the data on the adapter? Please consider I have other thing to do on the adapter like retriving the images (with Glide ). Any suggestion or consideration would be helpful!

Thank you

You can try to show only a portion of the data and use a Recycler View's OnScroll Listener to load and show more when the user has reached the end of the recycler view (basically, a pagination). This way, you won't have to pay the full payload of loading all data at once.

Hope this helps!

If you're using recycler view then I guess it's actually the best method (yet to me) for loading large lists...I think both method (store Arraylist and sent data to adapter) works in some cases, but recycler view destroys data that have been scrolled. But I think the best way and most efficient used by many developers is to set a limit to amount of data displayed on screen at once then use on scroll listener to load more, then recycler view does it jobs too!

Check it out here very perfect

Android Endless List

You can try cursorRecyclarViewAdapter

https://gist.github.com/skyfishjy/443b7448f59be978bc59

public class MyRecyclerAdapter extends Adapter<MyRecyclerAdapter.ViewHolder> {

// Because RecyclerView.Adapter in its current form doesn't natively 
// support cursors, we wrap a CursorAdapter that will do all the job
// for us.
CursorAdapter mCursorAdapter;

Context mContext;

public MyRecyclerAdapter(Context context, Cursor c) {

    mContext = context;

    mCursorAdapter = new CursorAdapter(mContext, c, 0) {

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // Inflate the view here
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // Binding operations
        }
    };
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    View v1;

    public ViewHolder(View itemView) {
        super(itemView);
        v1 = itemView.findViewById(R.id.v1);
    }
}

@Override
public int getItemCount() {
    return mCursorAdapter.getCount();
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // Passing the binding operation to cursor loader
    mCursorAdapter.getCursor().moveToPosition(position); //EDITED: added this line as suggested in the comments below, thanks :)
    mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // Passing the inflater job to the cursor-adapter
    View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
    return new ViewHolder(v);
}
}

you can achieve this by Android Room & Paging component i have try load data record count>10m

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setViewModel(new ViewModelProvider(this).get(ViewModel.class));
        binding.setLifecycleOwner(this);
    }


    public static class ViewModel extends AndroidViewModel {
        public final List<Contact> contactList = new ArrayList<>();
        private final ContactDatabase database = Room.databaseBuilder(getApplication(), ContactDatabase.class, "contact.db")
                .fallbackToDestructiveMigration()
                .allowMainThreadQueries()
                .build();
        public final LiveData<PagedList<Contact>> pagedListLiveData = new LivePagedListBuilder<>(database.getContactDao().getAllContact3(), 1000).build();

        public ViewModel(@NonNull Application application) {
            super(application);
        }
    }
}

Room Database Entity class

@Entity
public class Contact {

    @PrimaryKey(autoGenerate = true)
    public int _id;

    public int _accountId;

    public int photo_id;

    public String display_name;
    public String display_name_alt;
    public String display_name_source;

    public int starred;
    public int pinned;

    public String phonetic_name;
    public String phonetic_name_style;

    public int deleted;
    public int dirty;
}

Room Database Dao

@Dao
public interface ContactDao {

    @Query("SELECT * FROM contact")
    List<Contact> getAllContact();

    @Query("SELECT * FROM contact")
    LiveData<List<Contact>> getAllContact2();

    @Insert
    void insertAll(List<Contact> contacts);

    @Query("SELECT * FROM contact")
    DataSource.Factory<Integer, Contact> getAllContact3();

    @Query("SELECT * FROM contact")
    PositionalDataSource<Contact> getAllContact4();

}

Layout

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="viewModel"
            type="com.xxx.hugedatabaserecordtest.MainActivity.ViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:itemTemplate="@{@layout/contact_list_item_template}"
            app:items="@{viewModel.pagedListLiveData}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM