RecyclerView显示除第一项以外的所有内容

时间:2016-04-26 01:42:12

标签: android android-recyclerview

我有一个RecyclerView,其中每个项目都是一个包含两个EditTexts和一个ImageButton的LinearLayout。 RecyclerView显示除我的第一个数据集之外的所有项目。

谁能告诉我发生了什么?

public class EditListAdapter extends RecyclerView.Adapter<EditListAdapter.ListViewHolder> {

private ArrayList mListCardset;

public static class ListViewHolder extends RecyclerView.ViewHolder{
    public EditText frontEditText;
    public EditText backEditText;
    public ImageButton editActionButton;

    public ListViewHolder(View view) {
        super(view);
        frontEditText = (EditText)view.findViewById(R.id.front_edit_text);
        backEditText = (EditText)view.findViewById(R.id.back_edit_text);
        editActionButton = (ImageButton) view.findViewById(R.id.edit_action_button);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public EditListAdapter(ArrayList aList) {
    mListCardset = aList;
}

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

    ListViewHolder vh = new ListViewHolder((LinearLayout)v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ListViewHolder listHolder, int pos) {

    String[] eListItem = (String[]) mListCardset.get(pos); //get individual card @position
    listHolder.frontEditText.setText(eListItem[0]); 
    listHolder.backEditText.setText(eListItem[1]); 

}


private String[] strArrayListHelper(String frontString, String backString){
    String[] returnStrings = new String[2];
    returnStrings[0] = frontString;
    returnStrings[1] = backString;

    return returnStrings;
}

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

这只会显示第2至第5项:

 mRecyclerView = (RecyclerView)findViewById(R.id.list_edit_recycler_view);

    ArrayList testListCards = new ArrayList<String[]>();
    testListCards.add(strArrayListHelper("Test 1","Test 1"));
    testListCards.add(strArrayListHelper("Test 2","Test 2"));
    testListCards.add(strArrayListHelper("Test 3","Test 3"));
    testListCards.add(strArrayListHelper("Test 4","Test 4"));
    testListCards.add(strArrayListHelper("Test 5","Test 5"));

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(EditActivity.this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter
    mAdapter = new EditListAdapter(testListCards);
    mRecyclerView.setAdapter(mAdapter);

2 个答案:

答案 0 :(得分:4)

也许您的Toolbar会隐藏RecyclerView的第一项。

尝试使用此属性

android:layout_marginTop="?attr/actionBarSize"

RecyclerView xml。

希望有所帮助!

答案 1 :(得分:1)

我的布局文件中缺少app:layout_behavior个属性。

如果其他人遇到类似问题,我会在内容布局中添加app:layout_behavior="@string/appbar_scrolling_view_behavior",以便appbar将内容推送到其下方而不是重叠。