2014-11-21 3 views
8

RecyclerView에 항목이 표시되지 않습니다. 처음에는 비어 있지만 나중에 항목을 추가하고 notifyDatasetChanged()를 호출합니다. getItemCount()가 호출되어 25를 반환하면 onBindViewHolder가 3 번 호출되고 뷰가 올바르게 설정됩니다.RecyclerView가 표시되지 않습니다.

public AlbumListAdapter(FacebookActivity activity, long pageId, OnItemClickListener listener){ 
    this.listener = listener; 
    this.inflater = LayoutInflater.from(activity); 
    this.service = new PagedGraphService<Album>(String.format("https://graph.facebook.com/%d/albums", pageId), 
      new PagedGraphService.ServiceUpdateListener() { 
     @Override 
     public void dataUpdated() { 
      notifyDataSetChanged(); 
     } 

     @Override 
     public void endReached() { 

     } 
    }, 
    new TypeToken<PagedGraphService.GraphResponse<Album>>(){}.getType()); 
} 

@Override 
public AlbumHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    return new AlbumHolder(inflater.inflate(R.layout.photo_album_list_card, viewGroup, false)); 
} 

@Override 
public void onBindViewHolder(AlbumHolder viewHolder, int i) { 
    viewHolder.setAlbum(service.get(i)); 
} 

@Override 
public int getItemCount() { 
    return service.getLoadedCount(); 
} 

요점은? 이 서비스는 앨범 목록을 보유하고 dataUpdated() 및 endReached()를 호출하여 변경 사항을 어댑터에 알립니다.

더 코드 :

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    RecyclerView view = (RecyclerView) inflater.inflate(R.layout.recycler, container, false); 
    view.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)); 
    view.setHasFixedSize(false); 
    AlbumListAdapter adapter = new AlbumListAdapter(activity, FacebookActivity.PSM_PAGE_ID, this); 
    view.setAdapter(adapter); 
    return view; 
} 

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</android.support.v7.widget.RecyclerView> 

더욱 코드 :

활동 레이아웃

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

<include layout="@layout/toolbar"/> 

<FrameLayout 
    android:id="@+id/content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

</LinearLayout> 

목록 항목 레이아웃 :

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<TextView 
    android:id="@+id/album_list_title" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

</android.support.v7.widget.CardView> 

목록 레이아웃 :

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.RecyclerView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

</android.support.v7.widget.RecyclerView> 

16,은 지금 몇 시간에 대한 코드를 파고있다 그리고 나는 내가 바보입니다

+0

a) 코드를 표시해야합니다. b) RecyclerView에서'notifyDatasetChanged()'를 호출하는 것이 효율적이지 않으므로 멋지지 않습니다. – reVerse

+0

데이터를 받으면 어댑터의 notifyDataSetChanged가 호출됩니다. – DariusL

+0

그건 기본적으로 내가 뭘 - 인용 [Docs] (https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged%28%29) :'만약 당신이 가능하면 어댑터를 작성하여보다 구체적인 변경 이벤트를 사용하는 것이 더 효율적입니다. 최후의 수단으로 notifyDataSetChanged()를 의지하십시오. ' – reVerse

답변

21

작동하지 않는 이유를 모르겠어요.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

<include layout="@layout/toolbar"/> 

<FrameLayout 
    android:id="@+id/content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

</LinearLayout> 

LinearLayout의 기본 방향은 수평이고, 내 콘텐츠 화면을 떠났다 있도록 도구 모음의 폭은 match_parent입니다. 이것은 아마도 안드로이드에서 가장 인기있는 실수 중 하나 일 것이기 때문에 경고를받는 이유가 있지만 include은 그것을 숨겨야합니다.

+1

나는 당신을 사랑합니다. 나는 이것에 1 시간을 보냈다. 나는 심지어 나 자신에게 차 (Lol)의 컵을 만드는 것을 끝내었다. –

+0

LinearLayout의 기본 방향은 수평입니다. 레이아웃이 수직적이라고 믿는 것은 꽤 쉽습니다. 여러 아이를 추가하고 왜 첫 번째 아이 만 볼 수 있는지 궁금합니다 (이후 아이가 화면 오른쪽에서 벗어나는 경우). 이 보풀트 규칙은 암시 적 방향 및 여러 자식이있는 LinearLayout이 사용될 때마다 경고하여이 문제를 정확히 찾아줍니다. 또한 id 특성을 정의하는 orientation 특성이없는 빈 LinearLayouts를 확인합니다. 이것은, 아이가 동적으로 LinearLayout에 추가되는 시나리오를 캐치합니다. –

관련 문제