【问题标题】:Convert Activity to Fragment将活动转换为片段
【发布时间】:2015-06-21 10:29:04
【问题描述】:

我是在 Android 中使用 Fragment 的新手。我在 Activity 中完成了 viewFilpper,现在我尝试将此函数添加为 Fragment(我只能将其转换为 Fragment 而不是 FragmentActivity,因为需要整个程序)。但总是出错。有没有人可以帮我解决这个问题。 这是Activity中的代码:

/*
* Copyright 2012 Harri Smatt Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
* or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
public class BinderActivity extends Activity implements BinderAdapter
{

private static final int[] LAYOUT_IDS = { R.layout.layout1, R.layout.layout2, R.layout.layout3, R.layout.layout4 };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        BinderView binder = (BinderView) findViewById(R.id.binder);
        binder.setAdapter(this);
    }

    @Override
    public View createView(ViewGroup container, int position)
    {
        return getLayoutInflater().inflate(LAYOUT_IDS[position], null);
    }

    @Override
    public int getCount()
    {
        return LAYOUT_IDS.length;
    }
}

这是片段中的代码:

public class BoltFragment extends Fragment implements BinderAdapter{
    private static final int[] LAYOUT_IDS = { R.layout.layout1, R.layout.layout2, R.layout.layout3, R.layout.layout4 };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        container.removeAllViews();
        View boltLayout = inflater.inflate(R.layout.fragment_bolt,
                container, false);
        // Set title bar
        ((MenuTabsActivity) getActivity())
                .setActionBarTitle("Bolt");

        BinderView binder = (BinderView) getView().findViewById(R.id.binder);
        binder.setAdapter(this);

        return boltLayout;
    }

    @Override
    public View createView(ViewGroup container, int position)
    {
        //CHANGE HERE LATER
        return getActivity().getLayoutInflater().inflate(LAYOUT_IDS[position], null);
    }

    @Override
    public int getCount()
    {
        return LAYOUT_IDS.length;
    }
}

这是BinderAdapter中的代码:

public interface BinderAdapter
{

public View createView(ViewGroup container, int position);

public int getCount();

}

我该怎么办

return getLayoutInflater().inflate(LAYOUT_IDS[position], null);

在片段中?

因为转换还没有完成,所以Fragment肯定会出现错误。

非常感谢。

【问题讨论】:

  • 什么是 BinderAdapter?你能把里面的代码贴出来吗?
  • 你也可以发布错误吗?
  • FragmentActivity 呢?
  • 感谢您的回答。我已经发布了 BinderAdapter。

标签: android android-fragments android-activity converter


【解决方案1】:

我最近做了同样的事情。

首先Fragment支持onCreateView函数。
参考:http://developer.android.com/guide/components/fragments.html 所以把 createView() 改成这样。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
   return inflater.inflate(LAYOUT_IDS[position], container, false);
}

【讨论】:

  • 非常感谢您回答我的问题。但是createView只是接口中的普通方法 public View createView(ViewGroup container, int position); ,其他一些方法将链接到那个。有什么办法可以保留 creatView(container, position)?
【解决方案2】:

由于您尚未发布错误日志,我猜测问题出在BinderView binder = (BinderView) getView().findViewById(R.id.binder); binder.setAdapter(this); 如果你还在创建它,你不能使用 getView(),所以它应该是BinderView binder = (BinderView)boltLayout.findViewById(R.id.binder); binder.setAdapter(this);

但是由于您真的不清楚您的问题是什么,因此也可能是其他问题。请将您的堆栈跟踪与错误消息一起发布。

【讨论】:

    猜你喜欢
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多