简体   繁体   中英

RecyclerView inside Fragment from TabBar

I got a small problem.

I tried to load a recycler view with some card.

Without the recycler view the fragment works and I got the card displayed, but since I added the recyclerView I keep getting error about it.

FATAL EXCEPTION: main Process: com.example.pierre.all, PID: 1729 android.view.InflateException: Binary XML file line #24: RecyclerView has no LayoutManager at android.view.LayoutInflater.inflate(LayoutInflater.java:539) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at com.example.pierre.all.Categories$PlaceholderFragment.onCreateView(Categories.java:131) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) at android.support.v4.view.V iewPager.populate(ViewPager.java:1177) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551) at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2008) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995) at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272) at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Me thod) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

This is my fragment_categories_favorites.xml

<FrameLayout 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="layout.Categories_favorites">

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

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

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="SHOP"
            android:textSize="20sp"
            android:gravity="center" />

        <TextView
            android:id="@+id/info_text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="25 items"
            android:layout_marginTop="40dp"
            android:gravity="center"
            android:textSize="12sp"
            />
    </android.support.v7.widget.CardView>
    </android.support.v7.widget.RecyclerView>
    </FrameLayout>

My Categories_favorites.java

public class Categories_favorites extends Fragment {

    private static final String TAG = "RecyclerViewFragment";

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


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

        RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.list);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(manager);
        //I tried this before instead of the 3 lignes before.
        //rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.setAdapter(new MyAdapter());
        return rootView;

    }
}

And this is MyAdaptater.java

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

    private final List<Pair<String, String>> characters = Arrays.asList(
            Pair.create("Lyra Belacqua", "Brave, curious, and crafty, she has been prophesied by the witches to help the balance of life"),
            Pair.create("Pantalaimon", "Lyra's daemon, nicknamed Pan."),
            Pair.create("Roger Parslow", "Lyra's friends"),
            Pair.create("Lord Asriel", "Lyra's uncle"),
            Pair.create("Marisa Coulter", "Intelligent and beautiful, but extremely ruthless and callous."),
            Pair.create("Iorek Byrnison", "Armoured bear, Rightful king of the panserbjørne. Reduced to a slave of the human village Trollesund."),
            Pair.create("Serafina Pekkala", "Witch who closely follows Lyra on her travels."),
            Pair.create("Lee Scoresby", "Texan aeronaut who transports Lyra in his balloon. Good friend with Iorek Byrnison."),
            Pair.create("Ma Costa", "Gyptian woman whose son, Billy Costa is abducted by the \"Gobblers\"."),
            Pair.create("John Faa", "The King of all gyptian people.")
    );

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.fragment_categories_favorites, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Pair<String, String> pair = characters.get(position);
        holder.display(pair);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView name;
        private final TextView description;

        private Pair<String, String> currentPair;

        public MyViewHolder(final View itemView) {
            super(itemView);

            name = ((TextView) itemView.findViewById(R.id.info_text));
            description = ((TextView) itemView.findViewById(R.id.info_text2));

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new AlertDialog.Builder(itemView.getContext())
                            .setTitle(currentPair.first)
                            .setMessage(currentPair.second)
                            .show();
                }
            });
        }

        public void display(Pair<String, String> pair) {
            currentPair = pair;
            name.setText(pair.first);
            description.setText(pair.second);
        }
    }

}

What I have done wrong ? (sorry for my english too)

Thanks in advance

您不能具有fragment_categories_favorites.xml的回收器视图中的子视图。为行项目使用单独的xml,并从适配器中对其进行充气,请参见此处以获取工作示例http://www.androidhive.info/2016/01/ android-with-recycler-view /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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