如何滚动以在Recycler View上加载更多数据?

时间:2017-07-15 12:35:04

标签: java android

我想知道如何滚动以获取有关我的回收站视图的更多数据 例如我使用wordpress api从我的网站获得10post 我希望在回收器中每次加载5个并滚动更多以获得5个帖子 但是我不知道如何编码而不是看一下,给我一些正确的想法!!

News_list.java

/**
 * A simple {@link Fragment} subclass.
 */
public class News_List extends Fragment {
   private static final String URL = "https://chsknews.com/wp-json/wp/v2/posts/";
   private RecyclerView recyclerView;
   private RecyclerView.Adapter adapter;
   private List<News_Item> news_itemList;
   private SwipeRefreshLayout swipeRefreshLayout;
    public News_List() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_news__list, container, false);
    swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.MRecyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    news_itemList = new ArrayList<>();
    News_Loader();
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            News_Loader();
            Toast.makeText(getContext(),"ដំណើរការជោគជ័យក្នុងការទាញយកពត៏មានចុងក្រោយ",Toast.LENGTH_SHORT).show();
            news_itemList.clear();
        }
    });
    return  rootView;
    }

    private void News_Loader() {
        final ProgressDialog progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("Loading News");
        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        progressDialog.dismiss();
                        try {
                            //JSONObject jsonObject = new JSONObject(response);
                            JSONArray array = new JSONArray(response);
                            for (int i = 0; i < array.length(); i++){
                                JSONObject o = array.getJSONObject(i);
                             //  Log.v("ID",o.getJSONObject("content").optString("rendered"));

                            news_itemList.add(new News_Item(
                                    o.getJSONObject("title").getString("rendered"),
                                    o.getString("date"),
                                    o.getJSONObject("better_featured_image").getString("source_url"),
                                    o.getString("date"),
                                    o.getJSONObject("content").getString("rendered"))
                                );
                               // news_itemList.add(news_item);
                            }
                            adapter = new CustomAdapter(news_itemList,getContext());
                            recyclerView.setAdapter(adapter);
                            if (swipeRefreshLayout.isRefreshing())
                            {
                            swipeRefreshLayout.setRefreshing(false);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();

                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                Toast.makeText(getContext(), error.getMessage(),Toast.LENGTH_LONG).show();
                if (swipeRefreshLayout.isRefreshing())
                {
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(stringRequest);
    }

}

适配器类

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

    private List<News_Item> news_items;
    private Context context;

    public CustomAdapter(List<News_Item> news_items, Context context) {
        this.news_items = news_items;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
    final News_Item  news_item = news_items.get(position);
    holder.title.setText(Html.fromHtml(news_item.getTitle()));
    holder.news_date.setText((news_item.getNews_date()));
        Picasso.with(context)
                .load(news_item.getImage_url())
                .fit()
                .into(holder.news_img);
    holder.news_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,News_Single_Post.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("source_url",news_items.get(position).getImage_url());
            intent.putExtra("rendered", news_items.get(position).getTitle());
            intent.putExtra("date",news_items.get(position).getNews_date());
            intent.putExtra("content",news_items.get(position).getNews_content());
            context.startActivity(intent);
        }
    });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView title;
        public TextView news_date;
        public ImageView news_img;
        public ViewHolder(View itemView) {

            super(itemView);
            title = (TextView) itemView.findViewById(R.id.news_title);
            news_date = (TextView) itemView.findViewById(R.id.news_date);
            news_img = (ImageView) itemView.findViewById(R.id.news_image);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /

RewriteCond %{QUERY_STRING} ^(?:[^&]*&)*arttyp=Indianerflöten
RewriteRule ^/?shop\.php$ /basiskategorie/indianerfloeten-high-spirits [R=301,L]

# RewriteRule  ^shop.php?arttyp=Indianerflöten /basiskategorie/indianerfloeten-high-spirits [R=301,L]
# Redirect 301 /shop.php /produkte
# Redirect permanent  /shop.php?arttyp=Indianerflöten /basiskategorie/indianerfloeten-high-spirits
# RewriteRule  ^shop.php?arttyp=Indianerflöten$ /basiskategorie/indianerfloeten-high-spirits [R=301,NC,L]
# RewriteRule  ^shop.php?arttyp=Indianerflöten /basiskategorie/indianerfloeten-high-spirits [R=301,NC,L]
# RewriteRule  ^shop.php?arttyp=Instrumente\ für\ Klangtherapie$ /basiskategorie/monochorde [R=301,L]
# Redirect permanent "shop.php?arttyp=Instrumente für Klangtherapie" /basiskategorie/monochorde


RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


</IfModule>

别忘了添加

private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
{
if(dy > 0) //check for scroll down
{
 visibleItemCount = mLayoutManager.getChildCount();
 totalItemCount = mLayoutManager.getItemCount();
 pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

        if (loading) 
        {
            if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) 
            {
                loading = false;
                Log.v("...", "Last Item Wow !");
                //Do pagination.. i.e. fetch new data
            }
        }
    }
}
 });