RecyclerView无需碎片即可运行。我使用片段时不起作用

时间:2015-04-09 03:00:25

标签: android android-fragments

我开始学习Android,并且使用片段时遇到了一些困难。你能帮我弄清楚问题是什么吗?除了下面提到的内容之外的所有其他内容在两个项目中都是相同的

1. Code without fragments (works totally fine. Gives a simple list with some data):



    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerList);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(llm);
    recyclerView.setAdapter(new MyRecyclerAdapter(generatePalettes()));
}



2. Code with Fragment (doesnt work. Blank Screen):



    MainActivity
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new MainActivityFragment()).commit();
            }
        }



    MainActivityFragment    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerList);
            LinearLayoutManager llm = new LinearLayoutManager(getActivity());
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(llm);
            recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));
            return rootView;
        }

-------------------------------- UPDATE ----------- -

代码更改为以下内容:仍然无法正常工作。显示空白屏幕。尝试将UI更新放在onActivityCreated和onViewCreated()中。请点击此处的代码:https://gist.github.com/adityarb88/12f5ccce665afbf58069

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerList);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));
    }

3 个答案:

答案 0 :(得分:1)

构建视图后的Fragments安装适配器:

MainActivityFragment

此方法应该只会膨胀并返回您的视图:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

然后在onViewCreated(View, Bundle)onActivityCreated(Bundle):

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

      // getView() returns your Fragments root view
      RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerList);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));

   }

答案 1 :(得分:1)

终于找到了这个问题的答案。首先,谢谢Pankaj。你是部分正确的。我的布局确实不正确。所以:  1.我确实将其更改为RelativeLayout,但我以编程方式将片段添加到我的活动中。  为了更清楚地看清事物,我改变了活动的背景     和片段,分别添加边距和填充。

执行此操作后,即使我知道我的片段正确添加,我仍然无法填充回收站视图。所以错误发生在PaletteAdapter类中。

 public PaletteAdapter(List<Palette> palettes){
        this.palettes = new ArrayList<Palette>();
        this.palettes.addAll(palettes);
    }

我在palettes.addAll(调色板)面前错过了这个。因此列表是空的。我已将代码上传到新的要点,以防有人想要引用它:

https://gist.github.com/adityarb88/c6109fc1d8a796383c71

答案 2 :(得分:0)

您必须对activity_main.xml进行一些更改

<LinearLayout 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:orientation="vertical">

 <FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

在MainACtivity中,您必须使用此frame_container ID将片段替换为

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,                                 Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
       recyclerView = (RecyclerView) rootView .findViewById(R.id.recyclerList);
        return rootView;
    }


@Override
    public void onActivityCreated(bala,bala, bala, cause i don't remember ) {

       LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(llm);
    recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));
    }