2017-09-10 22 views
-1

我在我的活動的一個片段中創建了一個recyclerView。我將它綁定到一個數據集。我已經設置了LinearLayoutManager和RecyclerView適配器。我的數據集也正確創建,我的Viewholder也是如此。沒有編譯警告,我的Logcat中沒有例外。我檢查了片段和單個項目的XML佈局,並且都將所有項目設置爲可見。Recylerview在Android應用程序中不可見

但我的recyclerView沒有顯示在片段中。

導致recyclerView不可見的常見疏忽是什麼?感謝有關此事的任何指導。

相關方法初始化我recylerView低於:

private void loadMyPlacardView(View view){ 
     List<Post> postList = new ArrayList<>(); 
     RecyclerView placardsRecyclerView = view.findViewById(R.id.fp_placards_recyclerView); 
     LinearLayoutManager postsLayoutManager = new LinearLayoutManager(view.getContext()); 
     placardsRecyclerView.setHasFixedSize(true); 
     placardsRecyclerView.setLayoutManager(postsLayoutManager); 
     DataSnapshot dataSnapshot = FirebaseStorageUtils.postDataSnapshot; 
     for (DataSnapshot singleSnapShot: dataSnapshot.getChildren()) { 
      Post thisPost = singleSnapShot.getValue(Post.class); 
      postList.add(thisPost); 
     } 
     PlacardsRecyclerViewAdapter placardsRecyclerViewAdapter = new PlacardsRecyclerViewAdapter(postList); 
     placardsRecyclerView.setAdapter(placardsRecyclerViewAdapter); 
    } 

代碼爲RecyclerviewAdapter在這裏

public class PlacardsRecyclerViewAdapter 
extends RecyclerView.Adapter<PlacardHolder> { 
private static final String TAG = "PlacardsViewAdapter"; 
private List<Post> listofPosts; 

public PlacardsRecyclerViewAdapter(List <Post> list) { 
    listofPosts = list; 
} 
@Override 
    public PlacardHolder onCreateViewHolder(ViewGroup parent, int viewType){ 
View view = LayoutInflater.from(parent.getContext()) 
         .inflate(R.layout.placard_item, parent, false); 
       return new PlacardHolder(view); 
      } 
@Override 
public void onBindViewHolder(PlacardHolder placard, int position) { 
    Post currentPost = listofPosts.get(position); 
    String username = currentPost.getUserName(); 
    placard.userName.setText(username); 
} 

@Override 
public int getItemCount() { 
    return listofPosts.size(); 
    } 
} 

代碼Recyclerviewholder作爲類PlacardHolder.java低於

public class PlacardHolder extends RecyclerView.ViewHolder { 
private static final String TAG = "PlacardsHolder"; 

public TextView userName; 

public PlacardHolder(View view){ 
    super(view); 
     this.userName = view.findViewById(R.id.pi_profile_name); 
} 
} 

XML對於招貼項目是placard_item.xml,低於

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:id="@+id/pi_profile_name" 
     android:text="@string/default_username" 
     android:textAlignment="center" 
     android:textSize="25sp" 
     android:textStyle="bold" 
     android:layout_weight="7"/></android.support.constraint.ConstraintLayout> 

和代碼fragment_placard.xml是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="testing recyclerview"/> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/fp_placards_recyclerView" 
     android:scrollbars="vertical" 
     android:visibility="visible" 
     android:layout_margin="10dp"> 
    </android.support.v7.widget.RecyclerView> 
</LinearLayout> 

從fragment_placard.xml TextView的加載罰款,但不是recyclerview。

+1

如果你可以在你的問題中加入一些代碼,它會容易得多。 – ali73

+0

@ ali73 - 添加了代碼。請檢查並幫助 –

+0

您是否使用Firebase實時數據庫? – Shruti

回答

1

好吧!它大約是Firebase。將數據添加到數據集後,您不會通知您的適配器。做

if(placardsRecyclerViewAdapter != null) 
    placardsRecyclerViewAdapter.notifyDataSetChanged(); 

postList添加數據後。

更新

完整結構:

private ValueEventListener postListener = null; 

private void loadMyPlacardView(View view) { 

    List<Post> postList = new ArrayList<>(); 
    RecyclerView placardsRecyclerView = view.findViewById(R.id.fp_placards_recyclerView); 
    LinearLayoutManager postsLayoutManager = new LinearLayoutManager(view.getContext()); 
    placardsRecyclerView.setHasFixedSize(true); 
    placardsRecyclerView.setLayoutManager(postsLayoutManager); 
    PlacardsRecyclerViewAdapter placardsRecyclerViewAdapter = new PlacardsRecyclerViewAdapter(postList); 
    placardsRecyclerView.setAdapter(placardsRecyclerViewAdapter); 

    //Fetching data from Firebase 
    postListener = new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 

       if (dataSnapshot.hasChildren()) { 
        for (DataSnapshot ds : dataSnapshot.getChildren()) { 
         Post thisPost = ds.getValue(Post.class); 
         postList.add(thisPost); 
        } 
        if(placardsRecyclerViewAdapter != null) 
         placardsRecyclerViewAdapter.notifyDataSetChanged(); //Now postList has data so notify adapter. 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       // Getting Post failed, log a message 
       Log.e("DB error", "loadPost:onCancelled", databaseError.toException()); 
      } 
    }; 
    databaseReference.addListenerForSingleValueEvent(valueEventListener); 
} 
+0

我認爲當我們在創建列表視圖後對'postList'進行更改時,我們需要'notifyDataSetChanged()'它AFIK – phpdroid

+0

@phpdroid True!但他從Firebase DB獲取異步數據。 – Shruti

0

我找到了答案,我的問題。有2個錯誤。第一個問題是在我宣佈Constraintview如下placard_item.xml:

android:layout_height ="match parent" 

因爲這個佈局佔用了屏幕的整個空間。第二個錯誤是,我已經定義的TextView如下

android:layout_width = "0dp" 

這是造成的TextView在運行時recyclerview內消失,雖然它顯示在我的XML中的「設計」視圖非常清楚AndroidStudio IDE。我在這裏發佈答案,以防其他人面臨同樣的問題。在使用Recyclerview時,「ConstraintView」通常很難處理。所以回到更簡單的LinearLayout。

相關問題