来自TabBar

时间:2016-09-01 05:38:58

标签: java android android-layout android-fragments android-recyclerview

我遇到了一个小问题。

我尝试用某张卡加载回收者视图。

如果没有回收者视图,片段可以工作,我显示了卡片,但是由于我添加了recyclerView,我不断收到错误。

  

致命的例外:主要                                                                              处理:com.example.pierre.all,PID:1729                                                                              android.view.InflateException:二进制XML文件行#24:RecyclerView   没有LayoutManager                                                                                  在android.view.LayoutInflater.inflate(LayoutInflater.java:539)                                                                                  在android.view.LayoutInflater.inflate(LayoutInflater.java:423)                                                                                  在   com.example.pierre.all.Categories $ PlaceholderFragment.onCreateView(Categories.java:131)                                                                                  在   android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)                                                                                  在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)                                                                                  在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)                                                                                  在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)                                                                                  在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)                                                                                  在   android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)                                                                                  在   android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)                                                                                  在android.support.v4.view.ViewPager.populate(ViewPager.java:1177)                                                                                  在   android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)                                                                                  在   android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)                                                                                  在   android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)                                                                                  在   android.support.design.widget.TabLayout $ ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2008)                                                                                  在   android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025)                                                                                  在   android.support.design.widget.TabLayout.selectTab(TabLayout.java:995)                                                                                  在   android.support.design.widget.TabLayout $ Tab.select(TabLayout.java:1272)                                                                                  在   android.support.design.widget.TabLayout $ TabView.performClick(TabLayout.java:1377)                                                                                  在android.view.View $ PerformClick.run(View.java:21147)                                                                                  在android.os.Handler.handleCallback(Handler.java:739)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:148)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是我的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>

我的分类_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;

    }
}

这是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);
        }
    }

}

我做错了什么? (对不起我的英语)

提前致谢

1 个答案:

答案 0 :(得分:2)

您不能拥有fragment_categories_favorites.xml的recycler视图中存在的子视图。为您的行项创建一个单独的xml并从您的适配器中提取它请参阅此处的工作示例http://www.androidhive.info/2016/01/android-working-with-recycler-view/