RecyclerView空

时间:2016-05-30 02:46:04

标签: android android-recyclerview

我的回收者视图是空的,我可以告诉它,因为我可以向左和向右滚动(屏幕边缘的指示器告诉我)但我的个人列表项目没有显示。

这是我的主要活动代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_split_order);
    android.support.v7.app.ActionBar mActionBar = getSupportActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(true);

    setTitle("Split Order");

    //Code to get Screen Width and Height
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height                  = displaymetrics.heightPixels;
    int width                   = displaymetrics.widthPixels;
    int onequaterscreen         = width / 4;
    int onefifthheight          = height/ 5;


    recList = (RecyclerView) findViewById(R.id.rv_bill_list);
    recList.setHasFixedSize(true);
    createData();
    adapter = new BillListAdapter(this,data);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.HORIZONTAL);
    recList.setLayoutManager(llm);
    recList.setAdapter(adapter);

}

public void createData(){

    data = new ArrayList<>();
    BillItem dummy = new BillItem();
    dummy.setDiscountAmt(0.0);
    dummy.setOrderNum(44);
    dummy.setSubtotal(0.0);
    data.add(dummy);
}

适配器

private LayoutInflater inflator;
List<BillItem> billItemList = Collections.emptyList();

public BillListAdapter(Context context,List<BillItem> billItemList) {

    inflator = LayoutInflater.from(context);
    this.billItemList = billItemList;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflator.inflate(R.layout.bill_list_item,parent,false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    BillItem current = billItemList.get(position);
    holder.tv_bill_subtotal.setText(String.valueOf(current.subtotal));
    holder.tv_bill_discount_amt.setText(String.valueOf(current.discountAmt));
    holder.tv_bill_num.setText(String.valueOf(current.orderNum));

}

@Override
public int getItemCount() {
    return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{

    TextView tv_bill_subtotal;
    TextView tv_bill_discount_amt;
    TextView tv_bill_num;
    RelativeLayout rl_bill_discount;
    ListView lv_orderItemList;

    public MyViewHolder(View itemView) {
        super(itemView);
        tv_bill_num          = (TextView)        itemView.findViewById(R.id.tv_bill_num);
        tv_bill_subtotal     = (TextView)        itemView.findViewById(R.id.tv_bill_subtotal);
        rl_bill_discount     = (RelativeLayout)  itemView.findViewById(R.id.rl_bill_discount);
        lv_orderItemList     = (ListView)        itemView.findViewById(R.id.lv_orderitemlist);
        tv_bill_discount_amt = (TextView)        itemView.findViewById(R.id.tv_bill_discount_amt);
    }
}

布局文件是

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

<RelativeLayout
    android:id="@+id/rl_bill_sidemenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="false"
    android:layout_alignParentLeft="false"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@color/colorWhiteBrighter">

    <TextView
        android:paddingLeft="@dimen/sidemenu_horizontal_margin"
        android:paddingRight="@dimen/sidemenu_horizontal_margin"
        android:paddingTop="@dimen/sidemenu_vertical_margin"
        android:id="@+id/tv_bill_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ordermenunum"
        android:textSize="20sp"
        />


    <!--
    Order List Header
    -->
    <RelativeLayout
        android:id="@+id/rl_billitemlistheader"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="@dimen/sidemenu_horizontal_margin"
        android:paddingRight="@dimen/sidemenu_horizontal_margin"
        android:layout_marginTop="@dimen/sidemenu_vertical_margin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_bill_num"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/colorWhiteHighlight">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ordermenuqty"
            android:textSize="14sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:text="@string/ordermenuname"
            android:textSize="14sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="@string/ordermenuprice"
            android:textSize="14sp"/>


    </RelativeLayout>

    <ListView
        android:layout_below="@id/rl_billitemlistheader"
        android:id="@+id/lv_orderitemlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/rl_subtotal">

    </ListView>




    <!--
    Pay Button
    -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:padding="@dimen/sidemenu_vertical_margin"
        android:background="@drawable/greenbutton_selector"
        android:id="@+id/rl_bill_pay"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            android:textColor="@color/colorPrimaryDark"
            android:text=">>"/>

        <TextView

            android:id="@+id/tv_total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            android:textColor="@color/colorWhiteBrighter"
            android:text="@string/zerodollars"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />


    </RelativeLayout>

    <!--
     Subtotal Field
    -->
    <RelativeLayout
        android:id="@+id/rl_subtotal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        android:paddingLeft="@dimen/sidemenu_horizontal_margin"
        android:paddingRight="@dimen/sidemenu_horizontal_margin"
        android:background="@drawable/submenu_selector"
        android:layout_above="@+id/rl_bill_discount">

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="Subtotal:"/>

        <TextView
            android:id="@+id/tv_bill_subtotal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="@string/zerodollars"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
    <View android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/colorWhiteDarker"
        android:layout_below="@+id/rl_subtotal"
        />

    <!--
       Discount Field
    -->
    <RelativeLayout
        android:id="@+id/rl_bill_discount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        android:paddingLeft="@dimen/sidemenu_horizontal_margin"
        android:paddingRight="@dimen/sidemenu_horizontal_margin"
        android:background="@drawable/submenu_selector"
        android:layout_above="@+id/rl_bill_pay">

        <TextView

            android:id="@+id/tv_discount_lbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="Discount($):"/>

        <TextView
            android:id="@+id/tv_bill_discount_amt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="@string/zerodollars"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>
    <View android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/colorWhiteDarker"
        android:layout_below="@+id/rl_subtotal"
        />


</RelativeLayout>

我使用调试器检查值,没有任何内容为null。只是没有膨胀视图我猜是因为没有列表项。

2 个答案:

答案 0 :(得分:1)

getItemCount方法返回0

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

返回传递给适配器类的Arraylist的大小

答案 1 :(得分:0)

你应该像这样覆盖适配器的getItemCount()

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