{"id":9609,"date":"2024-12-13T23:14:14","date_gmt":"2024-12-13T14:14:14","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9609"},"modified":"2024-12-13T10:58:49","modified_gmt":"2024-12-13T01:58:49","slug":"%ec%95%88%eb%93%9c%eb%a1%9c%ec%9d%b4%eb%93%9c-%ec%9e%90%eb%b0%94-%ed%8e%98%ec%9d%b4%ec%a7%95-%ea%b5%ac%ed%98%84","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9609","title":{"rendered":"\uc548\ub4dc\ub85c\uc774\ub4dc &#8211; \uc790\ubc14 \ud398\uc774\uc9d5 \uad6c\ud604"},"content":{"rendered":"<h1>\uc548\ub4dc\ub85c\uc774\ub4dc &#8211; \uc790\ubc14 \ud398\uc774\uc9d5 \uad6c\ud604<\/h1>\n<p>\uc548\ub4dc\ub85c\uc774\ub4dc \uc790\ubc14 \uc571\uc5d0\uc11c \ud398\uc774\uc9d5\uc744 \uad6c\ud604\ud558\ub294 \ubc29\ubc95\uc744 \uc124\uba85\ud569\ub2c8\ub2e4.<br \/>\n10\ub144 \uc804\ubd80\ud130 \uc788\uc5c8\ub358 \uad6c\ub2e5\ub2e4\ub9ac \uae30\ubc95\uc774\uc9c0\ub9cc, \uc544\uc9c1 \uc4f8\ub9cc\ud569\ub2c8\ub2e4.<\/p>\n<h2>Entity<\/h2>\n<p>Item.java<\/p>\n<pre><code class=\"language-java\">@Entity(tableName = &quot;items&quot;)\npublic class Item {\n    @PrimaryKey(autoGenerate = true)\n    private int id;\n    private String title;\n    private String description;\n\n    \/\/ Getters and Setters\n    public int getId() { return id; }\n    public void setId(int id) { this.id = id; }\n    public String getTitle() { return title; }\n    public void setTitle(String title) { this.title = title; }\n    public String getDescription() { return description; }\n    public void setDescription(String description) { this.description = description; }\n}<\/code><\/pre>\n<h2>Dao<\/h2>\n<p>ItemDao.java<\/p>\n<pre><code class=\"language-java\">@Dao\npublic interface ItemDao {\n    @Query(&quot;SELECT * FROM items ORDER BY id LIMIT :limit OFFSET :offset&quot;)\n    List&lt;Item&gt; getItemsWithPaging(int limit, int offset);\n\n    @Query(&quot;SELECT COUNT(*) FROM items&quot;)\n    int getTotalCount();\n\n    @Insert\n    void insert(Item item);\n}<\/code><\/pre>\n<h2>Adapter<\/h2>\n<p>ItemAdapter.java<\/p>\n<pre><code class=\"language-java\">public class ItemAdapter extends RecyclerView.Adapter&lt;ItemAdapter.ItemViewHolder&gt; {\n    private List&lt;Item&gt; items = new ArrayList&lt;&gt;();\n\n    @NonNull\n    @Override\n    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {\n        View view = LayoutInflater.from(parent.getContext())\n                .inflate(R.layout.item_layout, parent, false);\n        return new ItemViewHolder(view);\n    }\n\n    @Override\n    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {\n        Item item = items.get(position);\n        holder.titleTextView.setText(item.getTitle());\n        holder.descriptionTextView.setText(item.getDescription());\n    }\n\n    @Override\n    public int getItemCount() {\n        return items.size();\n    }\n\n    public void addItems(List&lt;Item&gt; newItems) {\n        int startPosition = items.size();\n        items.addAll(newItems);\n        notifyItemRangeInserted(startPosition, newItems.size());\n    }\n\n    public void clearItems() {\n        items.clear();\n        notifyDataSetChanged();\n    }\n\n    static class ItemViewHolder extends RecyclerView.ViewHolder {\n        TextView titleTextView;\n        TextView descriptionTextView;\n\n        ItemViewHolder(View itemView) {\n            super(itemView);\n            titleTextView = itemView.findViewById(R.id.titleTextView);\n            descriptionTextView = itemView.findViewById(R.id.descriptionTextView);\n        }\n    }\n}<\/code><\/pre>\n<h2>MainActivity<\/h2>\n<p>MainActivity.java<\/p>\n<pre><code class=\"language-java\">public class MainActivity extends AppCompatActivity {\n    private ItemAdapter adapter;\n    private ItemDao itemDao;\n    private static final int PAGE_SIZE = 20;\n    private int currentPage = 0;\n    private boolean isLoading = false;\n    private boolean isLastPage = false;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        RecyclerView recyclerView = findViewById(R.id.recyclerView);\n        adapter = new ItemAdapter();\n        recyclerView.setAdapter(adapter);\n        LinearLayoutManager layoutManager = new LinearLayoutManager(this);\n        recyclerView.setLayoutManager(layoutManager);\n\n        \/\/ Room \ub370\uc774\ud130\ubca0\uc774\uc2a4 \ucd08\uae30\ud654\n        AppDatabase db = Room.databaseBuilder(getApplicationContext(),\n                AppDatabase.class, &quot;database-name&quot;).build();\n        itemDao = db.itemDao();\n\n        \/\/ \uc2a4\ud06c\ub864 \ub9ac\uc2a4\ub108 \uc124\uc815\n        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {\n            @Override\n            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {\n                super.onScrolled(recyclerView, dx, dy);\n\n                int visibleItemCount = layoutManager.getChildCount();\n                int totalItemCount = layoutManager.getItemCount();\n                int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();\n\n                if (!isLoading &amp;&amp; !isLastPage) {\n                    if ((visibleItemCount + firstVisibleItemPosition) &gt;= totalItemCount\n                            &amp;&amp; firstVisibleItemPosition &gt;= 0) {\n                        loadMoreItems();\n                    }\n                }\n            }\n        });\n\n        \/\/ \ucd08\uae30 \ub370\uc774\ud130 \ub85c\ub4dc\n        loadMoreItems();\n    }\n\n    private void loadMoreItems() {\n        isLoading = true;\n\n        new Thread(() -&gt; {\n            int offset = currentPage * PAGE_SIZE;\n            List&lt;Item&gt; newItems = itemDao.getItemsWithPaging(PAGE_SIZE, offset);\n            int totalCount = itemDao.getTotalCount();\n\n            runOnUiThread(() -&gt; {\n                if (newItems.size() &lt; PAGE_SIZE) {\n                    isLastPage = true;\n                }\n                adapter.addItems(newItems);\n                currentPage++;\n                isLoading = false;\n            });\n        }).start();\n    }\n}<\/code><\/pre>\n<h2>xml<\/h2>\n<p>activity_main.xml<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout \n    xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;&gt;\n\n    &lt;androidx.recyclerview.widget.RecyclerView\n        android:id=&quot;@+id\/recyclerView&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;match_parent&quot; \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;<\/code><\/pre>\n<p>item_layout.xml<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;LinearLayout\n    xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;wrap_content&quot;\n    android:orientation=&quot;vertical&quot;\n    android:padding=&quot;16dp&quot;&gt;\n\n    &lt;TextView\n        android:id=&quot;@+id\/titleTextView&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:textSize=&quot;16sp&quot;\n        android:textStyle=&quot;bold&quot; \/&gt;\n\n    &lt;TextView\n        android:id=&quot;@+id\/descriptionTextView&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_marginTop=&quot;8dp&quot; \/&gt;\n\n&lt;\/LinearLayout&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uc548\ub4dc\ub85c\uc774\ub4dc &#8211; \uc790\ubc14 \ud398\uc774\uc9d5 \uad6c\ud604 \uc548\ub4dc\ub85c\uc774\ub4dc \uc790\ubc14 \uc571\uc5d0\uc11c \ud398\uc774\uc9d5\uc744 \uad6c\ud604\ud558\ub294 \ubc29\ubc95\uc744 \uc124\uba85\ud569\ub2c8\ub2e4. 10\ub144 \uc804\ubd80\ud130 \uc788\uc5c8\ub358 \uad6c\ub2e5\ub2e4\ub9ac \uae30\ubc95\uc774\uc9c0\ub9cc, \uc544\uc9c1 \uc4f8\ub9cc\ud569\ub2c8\ub2e4. Entity Item.java @Entity(tableName = &quot;items&quot;) public class Item { @PrimaryKey(autoGenerate = true) private int id; private String title; private String description; \/\/ Getters and Setters public int getId() { return id; } public void setId(int id)\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9609\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-9609","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9609"}],"version-history":[{"count":1,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9609\/revisions"}],"predecessor-version":[{"id":9610,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9609\/revisions\/9610"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}