如何在片段中使用recyclerview的适配器类

时间:2016-11-11 05:26:33

标签: android android-fragments android-recyclerview android-viewpager

我在上一个活动中通过retrofit2呈现了一个列表,我将它设置为recyclerview的adapter.Adapter类用于片段类,这是我的代码, 我不确定我是否以正确的方式做事。请帮帮....

package com.group.visioncare;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.group.visioncare.BaseClasses.BaseClass;
import com.group.visioncare.PojoClasses.OrderModel;
import com.group.visioncare.RestInterfaces.AVCEndPointInterface;

import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class FragmentActivity extends Fragment {

    List<OrderModel> ordersList1;
    List<OrderModel> ordersList2;

    public String apiURL = BaseClass.getApiURL();
    AVCEndPointInterface avcService;

    String order_status;
    RecyclerView recyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    OrdersAdapter mAdapter;        

    public FragmentActivity(List<OrderModel> OrdersList) {            
        this.ordersList1 = OrderList;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmentactivity, container, false);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_id);
        recyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new OrdersAdapter(ordersList1);
        recyclerView.setAdapter(mAdapter);

        return rootView;
    }

    class OrdersAdapter extends RecyclerView.Adapter<OrdersAdapter.MyViewHolder> {

        public List<OrderModel> ordersModelList;

        public class MyViewHolder extends RecyclerView.ViewHolder {

            TextView Order_no, Order_Date, Sub_name, Sub_mobileno, Sub_billno, Sub_age, Sub_emailid, Order_status, Itemstatus;

            public MyViewHolder(View view) {
                super(view);

                Order_no = (TextView) view.findViewById(R.id.orderno_txtvw);
                Order_Date = (TextView) view.findViewById(R.id.date_txtvw);
                Sub_name = (TextView) view.findViewById(R.id.subname_txtvw);
                Sub_mobileno = (TextView) view.findViewById(R.id.mobileno_txtvw);
                Sub_billno = (TextView) view.findViewById(R.id.billno_txtvw);
                Sub_age = (TextView) view.findViewById(R.id.age_txtvw);
                Itemstatus = (TextView) view.findViewById(R.id.itemstatus_txtvw);                    
            }
        }

        public OrdersAdapter(List<OrderModel> ordersList) {
            this.ordersModelList = ordersList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {    
            final View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.orders_based_onfilters, null);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {    
            final OrderModel MybookedordersListInfo = ordersModelList.get(position);    

            Integer order_status_id = MybookedordersListInfo.getOrderStatus();
            holder.Order_no.setText("OrderID: " + ((MybookedordersListInfo.getId() != null) ? MybookedordersListInfo.getId() : "--"));
            holder.Order_Date.setText("Date: " + ((MybookedordersListInfo.getOrderDate() != null) ? date2 : "--"));
            holder.Sub_name.setText("Name: " + ((MybookedordersListInfo.getCustomerName() != null) ? MybookedordersListInfo.getCustomerName() : "--"));
            holder.Sub_mobileno.setText("Mobile: " + ((MybookedordersListInfo.getMobile() != null) ? MybookedordersListInfo.getMobile() : "--"));
            holder.Sub_billno.setText("BillNo: " + ((MybookedordersListInfo.getBillno() != null) ? MybookedordersListInfo.getBillno() : "--"));
            holder.Sub_age.setText("Age: " + ((MybookedordersListInfo.getAge() != null) ? MybookedordersListInfo.getAge().toString() : "--"));             
        }

        @Override
        public int getItemCount() {
            if (ordersModelList != null) {
                return ordersModelList.size();
            } else {
                return 0;
            }
        }    
    }

0 个答案:

没有答案