2016-04-28 5 views
2

1つのウェブサイトのAndroidアプリケーションを開発します。私はjsonからウェブサイトの記事を読んで、RecyclerViewを10の投稿ごとに表示し、ユーザーがRecyclerViewをスクロールすると10件以上の投稿を表示して終了します。このプロジェクトで私はokHTTP v3RecyclerViewを使用します!
Jsonリンク:JSON LINK
最初の10個の記事を読み込むことができます。私はRecyclerViewをスクロールするときに次の10ポストを見せたいが、最初に10ポストをもう一度見せたい!Androidのデータから次のページデータをロードする方法

MainActivityコード:

public class Main_page extends AppCompatActivity { 

    private static final long RIPPLE_DURATION = 250; 
    private Toolbar toolbar; 
    private RelativeLayout root; 
    private ImageView menu_image; 
    private RecyclerView main_recyclerView; 
    private MainAdapter2 mAdaper; 
    private List<MainDataModel> dataModels = new ArrayList<MainDataModel>(); 
    private List<MainDataModel> dataModelsArray; 
    private Context context; 
    protected Handler handler; 
    private RelativeLayout loadLayout; 
    private LinearLayoutManager mLayoutManager; 
    private int pageCount = 1; 
    private String ServerAddress = ServerIP.getIP(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_page); 
     if (!EventBus.getDefault().isRegistered(this)) { 
      EventBus.getDefault().register(this); 
     } 
     handler = new Handler(); 
     context = getApplicationContext(); 
     loadLayout = (RelativeLayout) findViewById(R.id.main_empty_layout); 
     toolbar = (Toolbar) findViewById(R.id.main_toolbar); 
     if (toolbar != null) { 
      setSupportActionBar(toolbar); 
      getSupportActionBar().setTitle(null); 
     } 
     LoadData(pageCount); 
     mLayoutManager = new LinearLayoutManager(this); 
     // Menu 
     root = (RelativeLayout) findViewById(R.id.main_root); 
     View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.menu_layout, null); 
     root.addView(guillotineMenu); 
     menu_image = (ImageView) toolbar.findViewById(R.id.toolbar_logo); 
     new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.menu_layout_image), menu_image) 
       .setStartDelay(RIPPLE_DURATION) 
       .setActionBarViewForAnimation(toolbar) 
       .setClosedOnStart(true) 
       .build(); 
     // RecyclerView and setData 
     main_recyclerView = (RecyclerView) findViewById(R.id.main_recycler); 
     main_recyclerView.setHasFixedSize(true); 
     main_recyclerView.setLayoutManager(mLayoutManager); 

     mAdaper = new MainAdapter2(this, main_recyclerView, dataModels); 
     main_recyclerView.setAdapter(mAdaper); 

     mAdaper.setOnLoadMoreListener(new OnLoadMoreListener() { 
      @Override 
      public void onLoadMore() { 
       dataModels.add(null); 
       mAdaper.notifyItemInserted(dataModels.size() - 1); 
       LoadData(pageCount); 
      } 
     }); 
    } 

    @Subscribe 
    public void onEvent(List<MainDataModel> mainInfoModels) { 
     if (dataModels.size() > 0) { 
      dataModels.remove(dataModels.size() - 1); 
      mAdaper.notifyItemRemoved(dataModels.size()); 
      mAdaper.setLoaded(); 
     } 

     mAdaper.add(mainInfoModels); 
     mAdaper.notifyDataSetChanged(); 
     ++pageCount; 

     if (dataModels.isEmpty()) { 
      main_recyclerView.setVisibility(View.GONE); 
      loadLayout.setVisibility(View.VISIBLE); 

     } else { 
      main_recyclerView.setVisibility(View.VISIBLE); 
      loadLayout.setVisibility(View.GONE); 
     } 
    } 

    private void LoadData(int pageNumber) { 
     MainDataInfo dataInfo = new MainDataInfo(); 
     // here getMainDataInfo() should return the server response 
     dataInfo.getMainDataInfo(this, pageNumber); 
    } 
} 

AsyncTaskコード:

public class MainDataInfo { 
    private Context mContext; 
    private String ServerAddress = ServerIP.getIP(); 

    public void getMainDataInfo(Context context, int pageNumber) { 
     mContext = context; 
     new getInfo().execute(ServerAddress + "page=" + pageNumber); 
    } 

    private class getInfo extends AsyncTask<String, Void, String> { 
     EventBus bus = EventBus.getDefault(); 
     private String ou_response; 
     private List<MainDataModel> infoModels; 

     @Override 
     protected void onPreExecute() { 
      CustomProcessDialog.createAndShow(mContext); 
      infoModels = new ArrayList<>(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      OkHttpClient client = new OkHttpClient(); 

      Request request = new Request.Builder() 
        .url(ServerAddress + "page=1") 
        .cacheControl(CacheControl.FORCE_NETWORK) 
        .build(); 

      Response response; 
      try { 
       response = client.newCall(request).execute(); 
       ou_response = response.body().string(); 
       response.body().close(); 
       if (ou_response != null) { 
        try { 
         JSONObject postObj = new JSONObject(ou_response); 
         JSONArray postsArray = postObj.optJSONArray("posts"); 
         infoModels = new ArrayList<>(); 

         for (int i = 0; i <= infoModels.size(); i++) { 
          JSONObject postObject = (JSONObject) postsArray.get(i); 
          JSONObject images = postObject.optJSONObject("thumbnail_images"); 
          JSONObject imagesPair = images.optJSONObject("medium"); 

          int id = postObject.getInt("id"); 
          String title = postObject.getString("title"); 
          String content = postObject.getString("content"); 
          String thumbnail = imagesPair.getString("url"); 
          Log.d("Data", "Post id: " + id); 
          Log.d("Data", "Post title: " + title); 
          Log.d("Data", "Post title: " + thumbnail); 

          //Use the title and id as per your requirement 
          infoModels.add(new MainDataModel(id, title, content, thumbnail)); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return ou_response; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      CustomProcessDialog.dissmis(); 
      if (result != null) { 
       bus.post(infoModels); 
      } 
     } 
    } 
} 

再びショーのRecyclerViewインスタンスに最初の10ポストをスクロールしたときどのように次の10ポストを表示することができますか?

答えて

0

JSONを確認しましたが、ページ番号を増やしても結果はありません。最初に修正して、結果をあなたのアプリに表示してください。

あなたpage number 2を設定するとthis oneはあなたに10のポストを返す

、あなたが毎回最初の10のポストを見ている理由です、任意のポストを返されていないが。

希望します。

+0

エミールさん、ありがとうございます。このリンクをご覧ください:http://tellfa.com/tafrihgah/?json=get_posts&page=2それは本当ですか? –

+0

いいえ、私の友人は、ページ番号を増やしてアプリケーションにページ番号を付けてjsonを取得しています。しかし、あなたのjson apiでは、ページ番号= 1の結果しか返されません。 –

+0

この 'http://tellfa.com/tafrihgah/?json = get_posts&page = 1&limit = 10'のようなAPIを使用して、ページ番号10の代わりに10の制限値 –

0

異なるページを作成する必要があります。 1ページには、わずか10のブログデータしかありません。情報を取得するには、10番目の項目が表示されたら次のページにJSONをリクエストしてください。 アイテム表示情報を取得するには、layoutManager.findLastVisbleItem()またはlastComletlyVisibleItem()を使用します。

一度要求されたデータがダウンロードされます。 VolleyのonRequestComleted()のようなメソッドが必要です。 notifysetそこに変更。

+0

私はOkHTTPライブラリを使用しています。私は素人だから –

関連する問題