【发布时间】: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