addOnScrollListener(RecyclerView)在TabLayout(片段)中不起作用

时间:2015-11-04 03:38:22

标签: android performance android-fragments

我正在开发一个应用程序,我在每个标签中有4个标签和一个recyclerview。我能够从Web服务获取数据并将其添加到recyclerview但是,我的问题是,我有500多条记录但是,我我每次都得到50条记录。我使用EndlessRecyclerOnScrollListener通过这个例子将数据添加到recyclerview

http://android-pratap.blogspot.in/2015/01/endless-recyclerview-onscrolllistener.html

我能够在活动中将新数据添加到我的recyclerview但是我无法添加标签(片段),即使addscrollListener没有触发,当我刷所有4个标签时我还有另一个问题,当我返回到第一个选项卡旧数据丢失了新数据。

在我错的地方可以帮助我吗?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootview = (ViewGroup) inflater.inflate(R.layout.newslist,container,false);

    dateValue=getArguments().getString("date");
    connection = 
    NetworkCheck.getConnectivityStatusString(getActivity());
    appPreferences = new AppPreferences(getActivity());
    newsList= new ArrayList<Model>();
    checkTextView=(TextView)rootview.findViewById(R.id.check);
    mRecyclerView =      
   (RecyclerView)rootview.findViewById(R.id.recycler_view);
    //mRecyclerView.setHasFixedSize(true);
    mAdapter = new NewsAdapter(newsList,getActivity());
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);

    new GetNewsAsyncTask().execute();
    mHandler = new Handler(Looper.getMainLooper());

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mRecyclerView.setAdapter(mAdapter);
                }
            });
        }
    });
    thread.start();

    mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
        @Override
        public void onLoadMore(int current_page) {
            Log.v("Load more", "Load more");
            loadMoreData(current_page);
        }
    });

    if (!connection.equals("No Internet Connection")) {
        Log.v("List","List"+newsList.size());
    } 
    else {
        mRecyclerView.setVisibility(View.GONE);
        checkTextView.setVisibility(View.VISIBLE);
        checkTextView.setText("Please check Internet Connectivity");
    }
    return rootview;

}
public void loadMoreData(int current_page)
{
    new GetNewsAsyncTask().execute();
    ival=ival+10;
}
public class GetNewsAsyncTask extends AsyncTask<String, String, String>  
 {
    protected void onPreExecute() {
        super.onPreExecute();

        if(ival==0)
        {
            ival = 0;
        }
    }   
    @Override
    public String doInBackground(String... strings) {
        try {

            postText();

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onPostExecute(String lenghtOfFile) {
        // do stuff after posting data

        //  progressDialog.dismiss();
        if(newsList.size()>0){
            checkTextView.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);

            if(adpaValue==0){
                mRecyclerView.setAdapter(mAdapter);
                adpaValue=1;
            }else
            {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        mAdapter.add(model);
                        Log.v("position ", " position runnable " + 
                              newsList.contains(model.getLink()));

                    }
                });
            }
        }
        else
        {
            Log.v("List","List else");
            mRecyclerView.setVisibility(View.GONE);
            checkTextView.setVisibility(View.VISIBLE);
            checkTextView.setText("No news found");
        }
    }

    // this will post our text data
    public void postText() {
        try {
            // url where the data will be posted
            String postReceiverUrl=                          Constants.newsUrl+appPreferences.getCountry()+"&category=general&skip="+ival+"&next=30&postdate="+dateValue;
            Log.v("TAG", "postURL: " + postReceiverUrl);
            String response = 
            GetwebServiceDatanew.getWebServiceData(postReceiverUrl);
            Log.v("response", "response" + response);

            try {
                JSONArray jsonArray = new JSONArray(response);
                for(int s=0;s<jsonArray.length();s++)
                {
                    JSONObject jsonObject = jsonArray.getJSONObject(s);
                    model = new Model();
                    model.setTitle(jsonObject.getString("title"));
                    model.setDescription(jsonObject.getString("description"));
                    model.setLink(jsonObject.getString("link"));
                    newsList.add(model);
                    Log.v("result","result"+jsonObject.getString("link"));
                    Log.v("result","result"+newsList.size());
                }
                ival=ival+10;
            }catch (Exception e)
            {
                e.printStackTrace();
            }

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

}

这是我的第一个标签的代码,我的所有标签都有相同的代码。

0 个答案:

没有答案