2017-03-17 13 views
1

Mein FragmentRecycler Adapter rufen onCreateViewHolder nicht

public class PostFragment extends BaseFragment implements PostView { 
@Inject 
PostPresenter presenter; 

private RecyclerView recyclerView; 
PostAdapter adapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v = inflater.inflate(R.layout.fragment_post, container, false); 

    recyclerView = (RecyclerView) v.findViewById(R.id.postFragmentRecycler); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 

    String s = "word"; 
    ArrayList<String> array = new ArrayList<>(); 
    for(int i = 0; i != 20; i++) { 
     array.add(s); 
    } 

    adapter = new PostAdapter(array); 
    recyclerView.setAdapter(adapter); 

    return v; 
} 

Adapter

public class PostAdapter extends RecyclerView.Adapter { 

private ArrayList<String> arrayList; 

public PostAdapter(ArrayList<String> arrayList) { 
    Log.d("ADAPTER", "constructor"); 
    this.arrayList = arrayList; 
} 

class MyViewHolder extends RecyclerView.ViewHolder { 

    public View view; 

    MyViewHolder(View itemView) { 
     super(itemView); 
     this.view = itemView; 
    } 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    Log.d("ADAPTER", "CREATE"); 
    View view = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.post_item, parent, false); 
    return new MyViewHolder(view); 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    Log.d("ADAPTER", "bind"); 
} 

@Override 
public int getItemCount() { 
    Log.d("ADAPTER", "size = " + String.valueOf(arrayList.size())); 
    return arrayList.size(); 
} 

}

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="exp.glorio.view.fragments.PostFragment"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/postFragmentRecycler" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</android.support.v7.widget.RecyclerView> 

post_item

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

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:layout_marginTop="10dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:visibility="visible"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_weight="1" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/postGroupLogo" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_marginLeft="20dp" 
       android:layout_gravity="center"/> 

      <TextView 
       android:id="@+id/postGroupName" 
       android:layout_width="180dp" 
       android:layout_height="30dp" 
       android:textColor="@color/colorText" 
       android:textSize="12sp" 
       android:layout_marginLeft="30dp" 
       android:layout_gravity="center" 
       /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/postText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <LinearLayout 
      android:id="@+id/postAttachmentsContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

     </LinearLayout> 
    </LinearLayout> 

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

und log:

03-17 17:11:23.753 30538-30538/exp.glorio D/ADAPTER: constructor 
03-17 17:11:23.903 30538-30538/exp.glorio D/ADAPTER: size = 20 
03-17 17:11:23.903 30538-30538/exp.glorio D/ADAPTER: size = 20 
03-17 17:11:23.953 30538-30538/exp.glorio D/ADAPTER: size = 20 
03-17 17:11:23.953 30538-30538/exp.glorio D/ADAPTER: size = 20 

ich viele andere Adapter in dieser App haben und sie arbeiten. In diesem Fall, wie Sie sehen können, vergesse ich nicht, LayoutManager und Recycler Ansicht nicht unter ScrollView in XML hinzuzufügen. Konstruktor und getItemCount funktioniert (warum 4 mal?), Aber onBindViewHolder und onCreateViewHolder nein.

+1

Problem ist in Ihrer post_item.xml. Wo auch immer Ihre Höhe kein fester Wert ist, sollte es sich um einen Wrap-Inhalt handeln. Ändern Sie Ihre gesamte übergeordnete Höhe in den Inhalt von windo. –

+0

Ich vermute, Sie haben eine der Benachrichtigungsmethoden auf dem Adapter aufgerufen? –

Antwort

1

Das Problem lag im Root-Layout der Aktivität. Es war android.support.constraint.ConstraintLayout. In Kombination mit dem Parameter layout_height "match_parent" des Fragmentcontainers gibt es dieses Problem. Wenn ConstraintLayout zu einem anderen ändern, wird es ordnungsgemäß funktionieren.

Verwandte Themen