2016-07-01 9 views
0

私はRecyclerViewに表示したいサーバーからデータを取得します。まず、データを取得するURLと、このデータを配置する位置を含むリストをRecyclerViewの中に入れます。 データを取得するとすぐにデータを表示し、すべてのデータが取得されるまで待つ必要はありません。 たとえば、 "url:www.url.com/data.php、postition:3"と表示されているリスト項目の場合、サーバーからこのデータを取得し、このデータを使用してRecyclerViewの3行目を入力したいとします。 。まだ第一要素と第二要素がなくても。RecyclerView:位置にアイテムを追加

今私はこれを好きです:私は何個のポジションを取得しなければならないのですか?numSectionsの要素がすべてnullのAdapterを作成します。これと同じように:私は1つのURLのコンテンツを取得した場合

ArrayList<ContentPreviewSectionModel> emptySections = new ArrayList<>(); 
while(emptySections.size() < numSections) emptySections.add(null); 
RecyclerView contentPreviewList = (RecyclerView) findViewById(R.id.content_preview_list); 
contentPreviewListAdapter = new RecyclerViewDataAdapter(this, this, emptySections); 
contentPreviewList.setAdapter(contentPreviewListAdapter); 

、私はそうのようなRecyclerViewを更新:

@Override 
    public void setSectionContentRetrieved(int position, ContentPreviewSectionModel content,) { 
     contentPreviewListAdapter.addItemAtPosition(content, position); 
     contentPreviewListAdapter.notifyDataSetChanged(); 
    } 

addItemAtPositionは、単にアダプタデータ構造上のarrayList.set(position, content)です。

これが私が欲しいものを達成する最も簡単な方法であると思っていましたか?ありがとう。

あなたが使用する必要があります contentPreviewListAdapter.notifyDataSetChanged()

を使用しての

+0

はい、おそらくこれを行う最も簡単な方法です。 – rsella

+0

は、whileサイクルの代わりに 'new ArrayList <>(Collections。 nCopies(numSections、null)'を使用します。 – j2ko

答えて

2

代わり
contentPreviewListAdapter.notifyItemInserted(position);

(あなたが所望の位置に項目を追加したい場合は)

と使用:
contentPreviewListAdapter.notifyItemInserted(emptySections.size() - 1);

(あなたがenに商品を追加したいときD)

注: addItemAtPosition()を呼び出した後にこれを行うと、これはあなたの問題を解決しますnotifyDataSetChanged()

希望を呼び出すことはありません。

0

yourAdapter.notifyItemInserted(position)としてください。positionは、recyclerviewに商品を挿入する場所です。

関連する問題