标签:添加 .com 优化 XML 开启 res context apt 分页加载
package zhangphil.demo; import android.arch.persistence.room.Room; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.util.AsyncListUtil; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity { private final String TAG = "输出"; private RecyclerView.Adapter mAdapter; private LinearLayoutManager mLinearLayoutManager; private AsyncListUtil<User> mAsyncListUtil; private final int LIMIT = 10; private UserDatabase mUserDatabase; private UserDao mUserDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUserDatabase = Room.databaseBuilder(getApplicationContext(), UserDatabase.class, "users").build(); mUserDao = mUserDatabase.getUserDao(); setContentView(R.layout.activity_main); RecyclerView mRecyclerView = findViewById(R.id.recycler_view); mLinearLayoutManager = new LinearLayoutManager(this); mLinearLayoutManager.setOrientation(LinearLayout.VERTICAL); mRecyclerView.setLayoutManager(mLinearLayoutManager); mAdapter = new MyAdapter(); mRecyclerView.setAdapter(mAdapter); MyViewCallback mViewCallback = new MyViewCallback(); MyDataCallback mDataCallback = new MyDataCallback(); mAsyncListUtil = new AsyncListUtil<>(User.class, LIMIT, mDataCallback, mViewCallback); mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); Log.d(TAG + "onScrollStateChanged", "onRangeChanged"); mAsyncListUtil.onRangeChanged(); } }); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG + "刷新", "refresh"); mAsyncListUtil.refresh(); } }); findViewById(R.id.add_data).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { writeDatabase(); } }).start(); } }); //主动刷新数据。 new Thread(new Runnable() { @Override public void run() { SystemClock.sleep(500); mAsyncListUtil.refresh(); } }).start(); } private class MyDataCallback extends AsyncListUtil.DataCallback<User> { @Override public int refreshData() { //更新数据的元素个数。 Log.d(TAG + "refreshData", Integer.MAX_VALUE + ""); return Integer.MAX_VALUE; } /** * 在这里完成耗时的数据加载的耗时任务。 * * @param data * @param startPosition * @param itemCount */ @Override public void fillData(User[] data, int startPosition, int itemCount) { Log.d(TAG + "fillData", startPosition + " , " + itemCount); List<User> list = mUserDao.getUserWhereUserIdBigThan(startPosition, itemCount); for (int i = 0; i < list.size(); i++) { data[i] = list.get(i); } } } private class MyViewCallback extends AsyncListUtil.ViewCallback { @Override public void getItemRangeInto(int[] outRange) { outRange[0] = mLinearLayoutManager.findFirstVisibleItemPosition(); outRange[1] = mLinearLayoutManager.findLastVisibleItemPosition(); Log.d(TAG + "getItemRangeInto", outRange[0] + " ~ " + outRange[1]); } @Override public void onDataRefresh() { mAdapter.notifyItemRangeChanged(mLinearLayoutManager.findFirstVisibleItemPosition(), LIMIT); Log.d(TAG + "onDataRefresh", mLinearLayoutManager.findFirstVisibleItemPosition() + "," + mLinearLayoutManager.findLastVisibleItemPosition()); } @Override public void onItemLoaded(int position) { mAdapter.notifyItemChanged(position); Log.d(TAG + "onItemLoaded", String.valueOf(position)); } } private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { public MyAdapter() { super(); } @Override public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_layout, viewGroup, false); MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(MyViewHolder viewHolder, int i) { User u = mAsyncListUtil.getItem(i); viewHolder.setData(u); } @Override public int getItemCount() { return mAsyncListUtil.getItemCount(); } } private class MyViewHolder extends RecyclerView.ViewHolder { public TextView userId; public TextView userName; public TextView userAge; public MyViewHolder(View itemView) { super(itemView); userId = itemView.findViewById(R.id.user_id); userName = itemView.findViewById(R.id.user_name); userAge = itemView.findViewById(R.id.user_age); } public void setData(User u) { if (u != null) { userId.setText("id:" + String.valueOf(u.userId)); userName.setText("姓名:" + String.valueOf(u.name)); userAge.setText("年龄:" + String.valueOf(u.age)); } } } private void writeDatabase() { Log.d(TAG + "writeDatabase", "开始写入数据..."); for (int i = 0; i < 100; i++) { User user = new User(); user.name = "张" + i; user.age = (int) (Math.random() * 100); user.updateTime = System.currentTimeMillis(); mUserDao.insertUser(user); } Log.d(TAG + "writeDatabase", "写入数据库完毕."); } @Override protected void onDestroy() { super.onDestroy(); mUserDatabase.close(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更新" /> <Button android:id="@+id/add_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加数据" /> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="20dp"> <TextView android:id="@+id/user_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_red_light" /> <TextView android:id="@+id/user_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/holo_orange_light" /> <TextView android:id="@+id/user_age" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/holo_blue_light" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@android:color/darker_gray" /> </LinearLayout>
package zhangphil.demo; import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; /** * Created by Phil on 2017/11/22. */ @Entity(tableName = "user_table") public class User { @PrimaryKey(autoGenerate = true) public int userId; @ColumnInfo(name = "userName") public String name; @ColumnInfo(name = "userAge") public int age = -1; @ColumnInfo(name = "updateTime") public long updateTime = -1; }
package zhangphil.demo; import android.arch.persistence.room.Dao; import android.arch.persistence.room.Delete; import android.arch.persistence.room.Insert; import android.arch.persistence.room.OnConflictStrategy; import android.arch.persistence.room.Query; import android.arch.persistence.room.Update; import java.util.List; /** * Created by Phil on 2017/11/22. */ @Dao public interface UserDao { @Query("SELECT * FROM user_table") public List<User> getAllUsers(); @Query("SELECT * FROM user_table WHERE userId >:uid ORDER BY userId ASC LIMIT :limit") public List<User> getUserWhereUserIdBigThan(int uid, int limit); @Query("SELECT * FROM user_table WHERE userId =:uid") public List<User> getUserWhereUserIdEqual(int uid); @Query("SELECT * FROM user_table WHERE userId BETWEEN :minId AND :maxId ORDER BY userId ASC") public List<User> getUserIdBetween(int minId, int maxId); @Insert(onConflict = OnConflictStrategy.REPLACE) public void insertUser(User... users); @Update public void updateUser(User... users); @Delete public void deleteUser(User... users); }
package zhangphil.demo; import android.arch.persistence.room.Database; import android.arch.persistence.room.RoomDatabase; /** * Created by Phil on 2017/11/22. */ @Database(entities = {User.class}, version = 1, exportSchema = false) public abstract class UserDatabase extends RoomDatabase { public abstract UserDao getUserDao(); }
Android Room联合AsyncListUtil实现RecyclerView分页加载ORM数据
标签:添加 .com 优化 XML 开启 res context apt 分页加载
原文地址:http://blog.csdn.net/zhangphil/article/details/78661838