如何在tablayout的片段中显示回收者视图?

时间:2017-01-05 18:15:49

标签: android android-fragments

这是我准备的数据文件,用于将数据放入片段的回收者视图中

public class Data
{
    public static ArrayList<Information> getData(){

        ArrayList<Information> data= new ArrayList<>();
        int [] images={
                    R.drawable.fuel,
                    R.drawable.milage,
                    R.drawable.serviceicon,
                    R.drawable.engine,
                    R.drawable.carbattery
        };

        String[] InformationAbout = {"245 Est miles to empty","4768 Miles Drove", "2 service compaings","245 EST mile to empty","72% battery level is good"};

        for (int i=0;i<=images.length-1;i++)
        {
            Information current= new Information();
            current.title=InformationAbout[i];
            current.imageId=images[i];

            data.add(current);
        }
        return data;
    }
}

这是我为了分配ID而创建的信息类。

public class Information
{
    public int imageId;        //Reference to all the images in the drawable folder
    public String title;
}

我创建的listadapter,用于为回收站视图提供服务并将其绑定到视图:

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyViewHolder> {

    private Activity context;

    ArrayList<Information> data;

    //LayoutInflater inflator;

    public ListAdapter(Activity context , ArrayList<Information> data){
        this.context = context;
        this.data=data;
        //this.inflator = LayoutInflater.from(context);
    }


    @Override
    public ListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = context.getLayoutInflater() ;
        View view=inflater.inflate(R.layout.list_single,parent,false);
        MyViewHolder myViewHolder= new MyViewHolder(view);
        return myViewHolder;
    }


    @Override
    public void onBindViewHolder(ListAdapter.MyViewHolder holder, int position) {
        holder.textview.setText(data.get(position).title);
        holder.imageView.setImageResource(data.get(position).imageId);
    }

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


    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView textview;
        ImageView imageView;
        public MyViewHolder(View itemView) {
            super(itemView);
            textview = (TextView) itemView.findViewById(R.id.text_corresponding_image);
            imageView = (ImageView) itemView.findViewById(R.id.icon_health_report);
        }
    }
}

这是我试图在recyclerview的适配器中提供的XML文件。

list_single.xml:

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


        <ImageView
            android:id="@+id/icon_health_report"
            android:layout_width="@dimen/tabel_image_width"
            android:layout_height="@dimen/table_image_height"/>

        <TextView
            android:id="@+id/text_corresponding_image"
            android:layout_width="wrap_content"
            android:layout_height="20dp"/>


</LinearLayout>

这是片段的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.a129346.applicationpoc.Fragments.HealthReportFragmentTab">

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


</RelativeLayout>

这是fragmenttab.java类,我在其中启动布局的回收器视图:

public class HealthReportFragmentTab extends Fragment {

    private static String TAG = HealthReportFragmentTab.class.getSimpleName();

            private RecyclerView recyclerView;

            private ListAdapter mylistAdapter;

    private HealthHistoryFragmentTab.OnFragmentInteractionListener mListener;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.health_report_fragment, container, false);
        initViews(rootview);
            return rootview;

    }


    private void initViews(View view){

        recyclerView = (RecyclerView) view.findViewById(R.id.vechile_report_lv);

        mylistAdapter = new ListAdapter(getActivity(), Data.getData());

        recyclerView.setAdapter(mylistAdapter);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

}

但我无法获取此车辆运行状况标签上的数据。我希望图像阵列中的图像和图像前面的文本是类别字符串数组。

1 个答案:

答案 0 :(得分:0)

在fragmentTab.java类中,只需添加:

public class HealthReportFragmentTab extends Fragment {

    private static String TAG = HealthReportFragmentTab.class.getSimpleName();

            private RecyclerView recyclerView;

            private ListAdapter mylistAdapter;

    private HealthHistoryFragmentTab.OnFragmentInteractionListener mListener;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.health_report_fragment, container, false);
        initViews(rootview);
            return rootview;

    }


    private void initViews(View view){

        recyclerView = (RecyclerView) view.findViewById(R.id.vechile_report_lv);

        mylistAdapter = new ListAdapter(getActivity(), Data.getData());

        recyclerView.setAdapter(mylistAdapter);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

}

代替getActivity我应用了getContext()方法,因为它是一个片段,然后需要一个上下文来设置布局。

相关问题