从recyclerview中的(未知项目)的ArrayList一次加载20个项目和更多onScroll

时间:2015-07-20 05:04:43

标签: java android-fragments arraylist jsoup android-recyclerview

我正在使用Jsoup库从网站中提取数据。我从网页中提取数据并存储在arraylist中,如下所示。

url =“http://myagdikali.com/category/sport-news/page/”;

private class NewsExtractor extends AsyncTask<Void, Void, Void>
{
String title;

    @Override
    protected Void doInBackground(Void... params) { 
    Document doc = null;

            while (status == OK) {  
                currentURL = url + String.valueOf(page);
                try {

                    response = Jsoup.connect(currentURL)
                            .userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
                            .execute();
                    status = response.statusCode();
                    if (status == OK) {
                        doc = response.parse();

                        Elements Titles = doc.select("a[rel=bookmark]");

                        for (org.jsoup.nodes.Element title : Titles) {

                            String src = title.text();
                            Log.d("NEWS",src);
                            mTitles.add(src);

  }
                    }

                    }catch(Exception e){
                        e.printStackTrace();

                    }
      page++;
     }
     return null

所以我想做的是在recyclerview中一次显示前20个项目,然后滚动加载更多。我对此并不太了解。我试过但失败了,抱歉我没有包含我尝试的代码(我删除了它)。

现在我的OnCreateView和OnPostExecute看起来像这样:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_all, container, false);
    int i = getArguments().getInt(NEWS);
    String topics = getResources().getStringArray(R.array.topics)[i];

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyNews);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);

    myRecyclerViewAdapter = new MyRecyclerViewAdapter(getActivity(),mTitles,mDates,mLinks,mImages);
    recyclerView.setAdapter(myRecyclerViewAdapter);

    recyclerView.setHasFixedSize(true);
     }
    }

.............................

    @Override
    protected void onPostExecute(Void aVoid) {
    TextView text;
        text = (TextView) getActivity().findViewById(R.id.text11);
        text.setText("foo bar");
        myRecyclerViewAdapter = new MyRecyclerViewAdapter(getActivity(),mTitles,mDates,mLinks,mImages);
        recyclerView.setAdapter(myRecyclerViewAdapter);


    }
}

2 个答案:

答案 0 :(得分:0)

尝试更改onPostExecute,您正在从AsyncTask更改UI

@Override
protected void onPostExecute(Void aVoid) {

getActivity().runOnUiThread(new Runnable() {
         @Override
         public void run() {

    TextView text;
    text = (TextView) getActivity().findViewById(R.id.text11);
    text.setText("Fuck you");
    myRecyclerViewAdapter = new  MyRecyclerViewAdapter(getActivity(),mTitles,mDates,mLinks,mImages);
    recyclerView.setAdapter(myRecyclerViewAdapter);
    myRecyclerViewAdapter.notifyDataSetChanged();

        }
    });

}

答案 1 :(得分:0)

Niek Haarman在Github上有一个您可能想要查看的图书馆:https://github.com/nhaarman/ListViewAnimations

它不仅允许您为列表视图结果制作动画,还可以处理滚动时显示更多项目。