无法在RecyclerView

时间:2017-01-31 17:47:50

标签: java android xml

我在Android Studio中创建了一个RecyclerView,就像在另一个片段中一样。问题是所有的工作都是第一次看,但在创建一个行对象并说明应用程序之后,我看不到任何条目。我做过这个RecyclerView的方式和我原来的那个做得很好。我已经搜索了很多,但我可以找到错误。

这是我的片段xml:



<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/training_swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/trainingtTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="16dp"
            android:textStyle="bold"
            android:textColor="@color/colorPrimary"
            android:text="@string/trainingTitle" />

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

    </RelativeLayout>

</android.support.v4.widget.SwipeRefreshLayout>
&#13;
&#13;
&#13;

这是RecyclerView的行布局:

&#13;
&#13;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:focusable="true"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">

    <!-- Plan icon -->
    <ImageView
        android:id="@+id/planImage"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_marginRight="8dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        android:contentDescription="Icon"
        android:src="@drawable/ic_menu_train" />

    <!-- Plan title -->
    <TextView
        android:id="@+id/planTitle"
        android:textColor="@color/colorBlack"
        android:layout_width="match_parent"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="14dp"
        android:textSize="16dp"
        android:layout_height="wrap_content" />

    <!-- Plan type -->
    <TextView
        android:id="@+id/planType"
        android:layout_below="@id/planTitle"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- Plan date -->
    <TextView
        android:id="@+id/planDate"
        android:layout_width="match_parent"
        android:layout_below="@id/planType"
        android:layout_marginLeft="42dp"
        android:layout_marginRight="40dp"
        android:layout_marginBottom="14dp"
        android:layout_height="wrap_content" />

    <!-- Plan view -->
    <ImageView
        android:id="@+id/planView"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        android:contentDescription="Icon"
        android:src="@drawable/ic_menu_show" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:layout_below="@id/planDate"
        android:layout_marginLeft="42dp"
        android:background="#DCDCDC" />

</RelativeLayout>
&#13;
&#13;
&#13;

这是我称之为适配器的片段:

&#13;
&#13;
public class TrainingFragment extends Fragment {
    private OnFragmentInteractionListener mListener;
    // Variables for Recycler View
    private List<Plans> planList = new ArrayList<>();
    private RecyclerView trainingRecyclerView;
    private PlansAdapter pAdapter;

    public TrainingFragment() {
        // Required empty public constructor
    }

    //Change the title in action bar
    public void onResume() {
        super.onResume();
        String titleString = getResources().getString(R.string.title_activity_navigation_drawer_training);
        // Set title bar
        ((NavigationDrawerActivity) getActivity())
                .setActionBarTitle(titleString);
    }

    public static TrainingFragment newInstance() {
        TrainingFragment fragment = new TrainingFragment();
        return fragment;
    }

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

        // Menü bekannt geben, dadurch kann Fragment Menü-Events verarbeiten
        setHasOptionsMenu(true);
    }

    //Fragment XML geben, sowie als Menü setzen
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.menu_training, menu);

        // Declare ImageView and Animation for rotation animation
        MenuItem reloadButton = (MenuItem) menu.findItem(R.id.menu_reloadbutton);
        final Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_rotate);

        //if (reloadButton != null) {
        //    //reloadButton.setImageResource(R.drawable.ic_menu_reloadentry);
        //    reloadButton.getActionView().setOnClickListener(new View.OnClickListener() {
        //        @Override
        //        public void onClick(View view) {
        //            rotation.setRepeatCount(Animation.INFINITE);
        //            view.startAnimation(rotation);
        //        }
        //    });
        //}
    }

    //AddEntry click abfangen und verarbeiten
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Wir prüfen, ob Menü-Element mit der ID "action_daten_aktualisieren"
        // ausgewählt wurde und geben eine Meldung aus
        int id = item.getItemId();
        if (id == R.id.menu_reloadbutton) {

            // Text ausgeben
            //Toast.makeText(getActivity(), "Liste aktualisieren gedrückt!", Toast.LENGTH_LONG).show();


            return true;
        }
        return super.onOptionsItemSelected(item);
    }

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

        // Get Refresh Layout
        SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.training_swiperefresh);
        // Change color for the refresh layout
        swipeRefreshLayout.setColorSchemeColors(Color.rgb(99, 195, 195));

        trainingRecyclerView = (RecyclerView) view.findViewById(R.id.training_recycler_view);

        pAdapter = new PlansAdapter(planList, getContext());
        RecyclerView.LayoutManager pLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());

        trainingRecyclerView.setLayoutManager(pLayoutManager);
        trainingRecyclerView.setItemAnimator(new DefaultItemAnimator());
        trainingRecyclerView.setAdapter(pAdapter);

        preparePlansData();

        return view;
    }

    private void preparePlansData() {
        // Set plan data
        Plans plan = new Plans("ABC-Bestellung", "Muskelaufbau", "Datum: 21.04.1997");
        // Add Object to list
        planList.add(plan);

        plan = new Plans("test", "tttt", "ttttt");
        planList.add(plan);

        // Notify data changes
        pAdapter.notifyDataSetChanged();
    }

    // TODO: Rename method, update argument and hook method into UI event

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onStart() {
        super.onStart();
        try {
            mListener = (OnFragmentInteractionListener) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException(getActivity().toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
&#13;
&#13;
&#13;

这是我的适配器:

&#13;
&#13;
public class PlansAdapter extends RecyclerView.Adapter<PlansAdapter.MyPlanHolder> {

    private List<Plans> planList;
    private final Context customContext;

    public class MyPlanHolder extends RecyclerView.ViewHolder {

        public TextView planTitle, planType, planDate;

        public MyPlanHolder(View view) {
            super(view);
            planTitle = (TextView) view.findViewById(R.id.planTitle);
            planType = (TextView) view.findViewById(R.id.planType);
            planDate = (TextView) view.findViewById(R.id.planDate);
        }
    }

    public PlansAdapter(List<Plans> planList, Context customContext) {
        this.planList = planList;
        this.customContext = customContext;
    }

    @Override
    public MyPlanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.plans_list_row, parent, false);

        return new MyPlanHolder(itemView);
    }

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

        final Plans plan = planList.get(position);

        holder.planTitle.setText(plan.getPlanTitle());
        holder.planType.setText(plan.getPlanType());
        holder.planDate.setText(plan.getPlanDate());

    }

    @Override
    public int getItemCount() {
        return 0;
    }
}
&#13;
&#13;
&#13;

最后我的元素对象类:

&#13;
&#13;
public class Plans {
    private String planTitle, planType, planDate;

    public Plans(String planTitle, String planType, String planDate) {
        this.planTitle = planTitle;
        this.planType = planType;
        this.planDate = planDate;
    }

    public void setPlanTitle(String planTitle) {
        this.planTitle = planTitle;
    }

    public String getPlanTitle() {
        return planTitle;
    }

    public void setPlanType(String planType) {
        this.planType = planType;
    }

    public String getPlanType() {
        return planType;
    }

    public void setPlanDate(String planDate) {
        this.planDate = planDate;
    }

    public String getPlanDate() {
        return planDate;
    }
}
&#13;
&#13;
&#13;

我希望你能找到这个问题。

2 个答案:

答案 0 :(得分:2)

在Adapter的getItemCount()方法中,您将返回0作为list的大小。您应该将其更改为return planList.size()

答案 1 :(得分:1)

你的代码很好,除了在你的适配器中你在getItemCount()方法中返回0。

这是更新后的代码:

 public class PlansAdapter extends RecyclerView.Adapter<PlansAdapter.MyPlanHolder> {

private List<Plans> planList;
private final Context customContext;

public class MyPlanHolder extends RecyclerView.ViewHolder {

    public TextView planTitle, planType, planDate;

    public MyPlanHolder(View view) {
        super(view);
        planTitle = (TextView) view.findViewById(R.id.planTitle);
        planType = (TextView) view.findViewById(R.id.planType);
        planDate = (TextView) view.findViewById(R.id.planDate);
    }
}

public PlansAdapter(List<Plans> planList, Context customContext) {
    this.planList = planList;
    this.customContext = customContext;
}

@Override
public MyPlanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.plans_list_row, parent, false);

    return new MyPlanHolder(itemView);
}

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

    final Plans plan = planList.get(position);

    holder.planTitle.setText(plan.getPlanTitle());
    holder.planType.setText(plan.getPlanType());
    holder.planDate.setText(plan.getPlanDate());

}

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