7

我遵循recyclerview指南併爲我製作的應用程序構建了一個,但由於某種原因它不滾動到底部。我將它與谷歌代碼片段以及其他在線代碼片段進行了比較,並且看不出其中的差異。我發佈了一張圖片和我正在使用的代碼。我使用標籤,因此recyclerview被填充到一個片段中。RecyclerView不滾動到底部

什麼應用程序看起來像:

http://imgur.com/H5uOLFR

適配器類:

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(); 
} 

} 

片段類:

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) { 

} 
} 

爲recyclerview的XML佈局:

<?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> 

的XML佈局爲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

請標註你的代碼尊重文件以便於比較。我主要關注包含你的片段的佈局。 – JoxTraex

+0

對不起,我不知道他們存在問題,而是我是如何製作片段本身的。 –

回答

2

感謝大家誰回答,但原來的問題是RecyclerView我的版本正在編譯。

以前我是編寫本

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

但是我改成了這一點,它的工作

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

貸@roi divon的答案:CoordinatorLayout with RecyclerView & CollapsingToolbarLayout

+0

也許你應該正確地標記它,因爲當我看你的答案時,其兩側相同 – JoxTraex

-1

也許這些行添加到recyclerView會做:

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

這是我的recyclerView這是工作:

<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

這不起作用 –

+0

@Mehul Goel我recyclerView是在相對佈局。你會嘗試嗎? –

+0

@MehulGoel並做其他事情。首先設置適配器,然後設置recyclerview的layoutManager。看看是否有效 –

-1

我不確定,但我認爲問題可能是Framelayout您可以試用Linearlayout,而不是Framelayout在你的XML佈局爲recyclerview項目

+0

感謝您的回覆,但這不是問題所在。我附上了一個回購以下谷歌的代碼示例。他們也使用framelayouts: https://github.com/udacity/Advanced_Android_Development/tree/6。16_Starting_RecyclerView_Finish –

6

您可以使用這些線路滾動recyclerview到:

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

如果我們按照這些步驟,那麼它的工作正常 –

+0

救生員:D ..... –