我想在我的应用程序中使用RecyclerView。我正在跟踪并尝试改编开发者网站上的代码。这里。

Tutorial

我无法锻炼在Adapter中设置行布局的方式。我将在下面的代码中发布以下代码:

TextView startDate = (TextView) LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.get_rota_row_layout, parent, false);




给了我以下例外:

> java.lang.ClassCastException: android.widget.LinearLayout cannot be
> cast to android.widget.TextView
>                                                                                              at
> loneworker.carefreegroup.com.loneworker.LoneworkerGetRotaActivityFragment$MyAdapter.onCreateViewHolder(LoneworkerGetRotaActivityFragment.java:161)
>                                                                                              at
> loneworker.carefreegroup.com.loneworker.LoneworkerGetRotaActivityFragment$MyAdapter.onCreateViewHolder(LoneworkerGetRotaActivityFragment.java:135)
>                                                                                              at
> android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6073)
>                                                                                              at
> android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5243)
>                                                                                              at
> android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5153)
>                                                                                              at
> android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
>                                                                                              at
> android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
>                                                                                              at
> android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
>                                                                                              at
> android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
>                                                                                              at
> android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3374)
>                                                                                              at
> android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3183)
>                                                                                              at
> android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3627)
>                                                                                              at android.view.View.layout(View.java:18799)




我以前用过ListViews和recyclerViews,但我以前没有这样做过。在Android开发人员网站上的上述教程中,我看不到cardView的设置位置。



public class ActivityFragment extends Fragment {

    private static final String TAG = LoneworkerGetRotaActivityFragment.class.getSimpleName();
    AppObj appObj;
    List<Visit> mVisitList;
    String URL;
    String companyID;
    String ID2;
    String userID;

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;






    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        appObj = (AppObj)getActivity().getApplication();
        URL = appObj.dbModel.getCompanyUrl();
        Log.e(TAG, "URL = " + URL);
        companyID = appObj.dbModel.getCompanyID();
        ID2 = appObj.dbModel.getCompanyWebServiceGuid();
        userID = appObj.dbModel.getUserID();



    }//end of onCreate





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        LayoutInflater lf = getActivity().getLayoutInflater();
        View view = lf.inflate(R.layout.l_w_get_rota_activity_fragment, container, false);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.get_rota_recycler_view);



        sendRequest();

        return view;


    }




    private void sendRequest(){

        //https://CFTest.carefreeapp.co.uk/roadrunner.asmx/GetRotaHours?CarerID=36&RotaDate=2-Nov-2017
        //https://CFTest.carefreeapp.co.uk/roadrunner.asmx/RRGetRota?ID1=900&ID2=9d817606-9194-4528-84b4-028e5aa90330&CarerID=36&RotaDate=2-Nov-2017

        StringRequest stringRequest = new StringRequest(URL + "/roadrunner.asmx/RRGetRota?ID1=" + companyID + "&ID2=" + ID2 + "&CarerID=" + userID + "&RotaDate=" + "2-Nov-2017",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //success - parse rota

                        DomParser dp = new DomParser(getActivity());
                        mVisitList = dp.parseUserRota(response);
                        Log.e(TAG, "mVisitList has size of " + mVisitList.size());

                        // use this setting to improve performance if you know that changes
                        // in content do not change the layout size of the RecyclerView
                        mRecyclerView.setHasFixedSize(true);

                        // use a linear layout manager
                        mLayoutManager = new LinearLayoutManager(getActivity());
                        mRecyclerView.setLayoutManager(mLayoutManager);

                        // specify an adapter (see also next example)
                        mAdapter = new MyAdapter(mVisitList);
                        mRecyclerView.setAdapter(mAdapter);




                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });


        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);
    }






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


        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public class ViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            public TextView tvStartDate;
            public ViewHolder(TextView v) {
                super(v);
                tvStartDate = v;
            }
        }

        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(List<Visit> myVisitList) {
            mVisitList = myVisitList;
        }

        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            // create a new view
            TextView startDate = (TextView) LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.get_rota_row_layout, parent, false);
            // set the view's size, margins, paddings and layout parameters

            ViewHolder vh = new ViewHolder(startDate);
            return vh;
        }

        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            holder.tvStartDate.setText(mVisitList.get(position).getStartDate());

        }

        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return mVisitList.size();
        }
    }




}//end of class




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:card_view="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <!-- A CardView that contains a TextView -->

            <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            card_view:cardCornerRadius="4dp">

                <TextView
                    android:id="@+id/get_rota_startdate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />


            </android.support.v7.widget.CardView>



    </LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/get_rota_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />


</RelativeLayout>

最佳答案

像这样尝试

onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View startDate = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.get_rota_row_layout, parent, false);

        return new ViewHolder(startDate);

    }

public class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView tvStartDate;
        public ViewHolder(View v) {
            super(v);
            tvStartDate = v.findViewById(R.id.textview);
        }
    }


ViewHolder就像是具有所有视图的整行。在onBindViewHolder()中,您可以使用所有已经初始化的视图持有者视图

10-07 13:52