ViewPager Fragment中的RecyclerView不显示

时间:2015-09-03 14:53:58

标签: android android-viewpager android-recyclerview

所以我有一个片段,它有一个带tablayout的viewpager,它由两个标签组成 - 两个片段。

问题是,recyclerview显示为空,我不知道为什么。,

Tab Fragment LAyout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

标签片段:

List<OrderListItem> orderList = new ArrayList<>();
        orderList.add(new OrderListItem(333, "ABCDE", new Date(), new Date(), false, true));

        adapter = new OrderListAdapter(orderList, this.getActivity());
        layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
        myRecyclerView.setLayoutManager(layoutManager);
        myRecyclerView.setAdapter(adapter);

适配器:

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder>{

    private List<OrderListItem> orderList;
    @LayoutRes
    private final int layoutRes;

    private Context context;
    private final LayoutInflater inflater;

    public OrderListAdapter(List<OrderListItem> orderList, Context context){
        this.orderList = orderList;
        this.layoutRes = R.layout.order_list_item_layout;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(layoutRes, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final OrderListItem item = orderList.get(position);    
    }

    public void setItems(List<OrderListItem> orderList){
        this.orderList.clear();
        this.orderList.addAll(orderList);
        notifyDataSetChanged();
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);

        }
    }
}

RecycleView Item一些色彩缤纷的布局内幕,所以我知道子布局是否存在,这不是。任何想法为什么Recyclerview是空的?

edit1:我知道Recyclerview在那里,因为它在一个lolipop手机中,如果我在回收器的地方做一个动作,它会向我显示波纹顶部和底部滚动边框。但是子布局是空的并且是空白的,并且应该像我在子布局中指定的一样丰富多彩。

eit2。刚刚使用了一个带有simpleAdapter的listview,它正在显示。 rv

肯定会出现问题

edit3:行布局(我应该清楚地看到一个带有颜色的空文本视图,除了没有设置任何值。)

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/black"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/material_deep_teal_200"
        android:text="New Text"
        android:id="@+id/textView" />
</LinearLayout>

2 个答案:

答案 0 :(得分:3)

变化:

android:layout_width="match_parent"
android:layout_height="match_parent"

android:layout_width="50dp"  // set yourself
android:layout_height="50dp" // set yourself

答案 1 :(得分:2)

OrderListItem属性设置为ui的逻辑在哪里? 您首先要设置ViewHolder组件:

public static class ViewHolder extends RecyclerView.ViewHolder {
    TextView mTitleTv;
    public ViewHolder(View itemView) {
        super(itemView);
        mTitle = itemView.findViewById(R.id.textview_id_in_your_xml_file);
    }
}

然后在onBindViewHolder

中设置它们
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final OrderListItem item = orderList.get(position);   
    holder.mTitleTv.setText(item.getReplaceThisWithAStringProperty);
}

修改

如果必须使用默认的LinearLayoutManager属性,请使用以下构造函数:

layoutManager = new LinearLayoutManager(getActivity());

而不是:

layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);

为RecyclerView添加固定大小的属性:

recyclerView.setHasFixedSize = true;