7

J'ai suivi les directives recyclerview et en ai construit une pour l'application que je suis en train de faire, mais elle ne défile pas vers le bas pour une raison quelconque. Je l'ai comparé avec des extraits de code google, ainsi que d'autres extraits de code en ligne et ne peux pas voir la différence. J'ai posté une photo et le code que j'utilise. J'utilise des onglets, donc le recyclerview est peuplé d'un fragment.RecyclerAffiche ne pas faire défiler vers le bas

Qu'est-ce que l'application ressemble à:

http://imgur.com/H5uOLFR

la classe adaptateur:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
private List<Group> groups; 

// Provide a reference to the views for each data item 
// Complex data items may need more than one view per item, and 
// you provide access to all the views for a data item in a view holder 
public class ViewHolder extends RecyclerView.ViewHolder { 
    // each data item is just a string in this case 
    public TextView groupName; 
    public TextView groupDate; 
    public TextView groupLocation; 
    public TextView className; 

    public ViewHolder(View v) { 
     super(v); 
     groupName = (TextView) v.findViewById(R.id.groupName); 
     groupDate = (TextView) v.findViewById(R.id.groupDate); 
     groupLocation = (TextView) v.findViewById(R.id.groupLocation); 
     className = (TextView) v.findViewById(R.id.className); 
    } 
} 

/* 
* TODO: finish this method 
*/ 
public void add(int position, String item) { 

    notifyItemInserted(position); 
} 

public void remove(String item) { 
    int position = groups.indexOf(item); 
    groups.remove(position); 
    notifyItemRemoved(position); 
} 

// Provide a suitable constructor (depends on the kind of dataset) 
public MyAdapter(List<Group> groupsList) { 
    groups = groupsList; 
    Log.d("TEST", "Number of Groups: " + 
      Integer.toString(groups.size())); 
} 

// Create new views (invoked by the layout manager) 
@Override 
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
               int viewType) { 
    // create a new view 
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.group_view, parent, false); 
    // set the view's size, margins, paddings and layout parameters 
    ViewHolder vh = new ViewHolder(v); 
    return vh; 
} 

// Replace the contents of a view (invoked by the layout manager) 
@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    // - get element from your dataset at this position 
    // - replace the contents of the view with that element 
    final Group group = groups.get(position); 
//  holder.groupName.setText(group.getName()); 
    holder.groupName.setText(group.getName()); 
    holder.groupDate.setText(group.getFormattedDate()); 
    holder.groupLocation.setText(group.getLocation()); 
    holder.className.setText(group.getParent().getName()); 
} 

// Return the size of your dataset (invoked by the layout manager) 
@Override 
public int getItemCount() { 
    return groups.size(); 
} 

} 

La classe Fragment:

public class groupsFragment extends Fragment implements GroupLeaver, GroupRetriever { 
private RecyclerView rv; 
private List<Group> groups; 
private ProgressDialog progressDialog; 

@Override 
public void onCreate(Bundle savedInstance){ 
    super.onCreate(savedInstance); 
    Log.d("TEST", "Entered onCreate"); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    AppMain.getController().retrieveGroups(groupsFragment.this); 
    Log.d("TEST", "Entered onCreateView"); 
    View rootView = inflater.inflate(R.layout.groups_fragment, container, false); 

    rv = (RecyclerView) rootView.findViewById(R.id.recyclerView); 

    rv.setLayoutManager(new LinearLayoutManager(getActivity())); 

    Log.d("TEST", "Size of LIST: " + Integer.toString(groups.size())); 
    MyAdapter adapter = new MyAdapter(groups); 
    rv.setAdapter(adapter); 

    return rootView; 
} 

@Override 
public void onMyGroupsFound(List<Group> groups) { 
    Log.d("TEST", "Entered onMyGroupsFound"); 
    Logg.info(this.getClass(), "Found %d groups for member %s", groups.size(), User.getCurrentUser().getDisplayName()); 
    this.groups = groups; 
} 

@Override 
public void onGroupLeft(Group oldGroup) { 

} 

@Override 
public void onGroupLeftFailed(Group group, ParseException e) { 

} 
} 

La mise en page XML pour le recyclerview:

<?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:orientation="vertical" 
> 

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

</FrameLayout> 

La mise en page XML pour les articles recyclerview:

<?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="wrap_content" 
> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="16dp" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="3" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/groupName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Group Name" 
      /> 

     <TextView 
      android:id="@+id/groupDate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Group Date" 
      /> 

     <TextView 
      android:id="@+id/groupLocation" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Group Location" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/className" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:gravity="right" 
      android:text="Class Name" 
      /> 
    </LinearLayout> 

</LinearLayout> 
</FrameLayout> 
+0

S'il vous plaît étiqueter votre code avec respect des fichiers pour une comparaison plus facile. Je m'intéresse principalement à la mise en page qui contient votre fragment. – JoxTraex

+0

Je suis désolé je n'avais aucune idée que leur problème résidait là, mais plutôt comment je faisais le fragment lui-même. –

Répondre

2

Merci à tous ceux qui ont répondu, mais se révèle le problème était la version de RecyclerView I compilait.

Auparavant, je compilait ce

compile 'com.android.support:recyclerview-v7:22.0.0' 

Mais je l'ai changé à cela et cela a fonctionné

compile 'com.android.support:recyclerview-v7:22.2.0' 

Crédits à @roi Divon pour la réponse: CoordinatorLayout with RecyclerView & CollapsingToolbarLayout

+0

peut-être vous devriez l'étiqueter correctement parce que quand je regarde votre réponse, c'est la même des deux côtés – JoxTraex

-1

Peut-être que l'ajout de ces lignes à la recyclerView fera:

android:scrollbarAlwaysDrawVerticalTrack="true" 
android:scrollbars="vertical" 

Ceci est mon recyclerView qui travaille:

<android.support.v7.widget.RecyclerView 
      android:id="@+id/menuRecycler" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbarAlwaysDrawVerticalTrack="true" 
      android:scrollbars="vertical"/> 
+0

Cela n'a pas fonctionné malheureusement –

+0

@Mehul Goel Mon recyclerView est dans un relativelayout. Voulez-vous l'essayer? –

+0

@MehulGoel Et faire autre chose. Commencez par définir l'adaptateur, puis définissez le layoutManager du recyclerview. voir si cela fonctionne –

-1

Je ne suis pas sûr mais je pense que le problème pourrait être Framelayout Vous pouvez essayer avec Linearlayout au lieu de Framelayout dans votre mise en page XML pour les articles recyclerview

+0

Merci d'avoir répondu, mais ce n'est pas le problème. J'ai joint un rapport ci-dessous avec l'exemple de code de google. Ils utilisent également des framelayouts: https://github.com/udacity/Advanced_Android_Development/tree/6.16_Starting_RecyclerView_Finish –

6

Vous pouvez utiliser ces lignes pour faire défiler recyclerview à:

list.add(0, group); 
adapter.notifyItemInserted(0); 
recyclerview.scrollToPosition(0); 
+0

Si nous suivons ces étapes, alors il fonctionne bien –

+0

Life Saver: D ..... –