简体   繁体   English

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

[英]How to show recycler view in the fragments of tablayout?

This is the data file which I have prepared in order to put data in the recycler view of my fragment 这是我准备的数据文件,用于将数据放入片段的回收器视图中

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;
    }
}

This is the information class which I have created in order to assign ID. 这是我为了分配ID而创建的信息类。

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

The listadapter which I have created in order to serve the recycler view and bind it to the view: 我创建的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);
        }
    }
}

This is the XML file which I am trying to serve in the adapter of the recyclerview. 这是我试图在recyclerview的适配器中提供的XML文件。

list_single.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>

This is the layout of the fragment: 这是片段的布局:

<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>

This is the fragmenttab.java class where I am initiating the recycler view of the layout: 这是fragmenttab.java类,我在其中启动布局的recycleler视图:

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()));
    }

}

but I am not able to get the data on the this vehicle health tab. 但我无法获得此车辆运行状况标签上的数据。 I want the images in the image array and text in front of the image which is the categories string array. 我希望图像阵列中的图像和图像前面的文本是类别字符串数组。

In the fragmentTab.java class, simply add: 在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()));
    }

}

In place of getActivity I applied getContext() method because it a fragment and then it needs a context in order to setlayout. 代替getActivity我应用了getContext()方法,因为它是一个片段,然后它需要一个上下文才能进行setlayout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM