我正在考虑一个解决方案,如何实现两个不同的活动,以便我可以像浏览器一样在它们之间滑动。
我在eclipse中创建了两个项目。在其中一个中,我实现了一个简单的音乐播放器,它显示了mp3.file的封面。它还会显示搜索栏在播放歌曲时的移动。 在另一个项目中,我实现了一个openstreetmap活动,它显示了我在地图上的位置。我想要的是结合这两个活动,以便我可以在它们之间滑动。那可能吗?我该如何解决呢?提供使用片段的其他解决方案?我会非常感谢你的建议 在此先感谢并原谅我的英语:)
编辑:为了更好地理解:
我有一个项目,播放一首歌并显示封面,歌曲名称,翻译和搜索栏,它们在播放歌曲时移动。它仍然工作正常。我想要的是将一个布局拆分为两个布局 this。在图片中你可以看到我实现的播放器(在左侧)。我想分割相同的布局组件,所以我在最后(右侧)有2个布局。我尝试用片段。这是我目前的代码:
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.swipeview3.R.layout.main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(com.example.swipeview3.R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(com.example.swipeview3.R.menu.main, menu);
return true;
}
// ------------------------------#########################------------------------------
// ------------------------------#########################------------------------------
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0) {
PlayerFragment f = new PlayerFragment(position);
return f;
}
if(position == 1) {
SongParameterFragment f = new SongParameterFragment(position);
return f;
}
else {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(com.example.swipeview3.R.string.title_section1).toUpperCase();
case 1:
return getString(com.example.swipeview3.R.string.title_section2).toUpperCase();
case 2:
return getString(com.example.swipeview3.R.string.title_section3).toUpperCase();
}
return null;
}
}
// ------------------------------#########################------------------------------
// ------------------------------#########################------------------------------
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return textView;
}
}
// ------------------------------#########################------------------------------
// ------------------------------#########################------------------------------
public static class PlayerFragment extends Fragment {
int fragmentNR;
Button btnPlay, btnStop, btnExit;
SeekBar seekBar;
public PlayerFragment(int position) {
this.fragmentNR = position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
btnPlay = (Button)getView().findViewById(com.example.swipeview3.R.id.button_play);
btnStop = (Button)getView().findViewById(com.example.swipeview3.R.id.button_stop);
btnExit = (Button)getView().findViewById(com.example.swipeview3.R.id.button_exit);
return inflater.inflate(com.example.swipeview3.R.layout.player, container, false);
}
}
// ------------------------------#########################------------------------------
// ------------------------------#########################------------------------------
public static class SongParameterFragment extends Fragment {
int fragmentNR;
ImageView cover;
TextView songParameter, songDuration;
public SongParameterFragment(int position) {
this.fragmentNR = position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(com.example.swipeview3.R.layout.song_parameters, container, false);
}
}
}
当我在PlayerFragment中分配按钮ID时,我变成了nullpointerException
01-23 13:33:09.553: D/AndroidRuntime(15881): Shutting down VM
01-23 13:33:09.553: W/dalvikvm(15881): threadid=1: thread exiting with uncaught exception (group=0x40c571f8)
01-23 13:33:09.553: E/AndroidRuntime(15881): FATAL EXCEPTION: main
01-23 13:33:09.553: E/AndroidRuntime(15881): java.lang.NullPointerException
01-23 13:33:09.553: E/AndroidRuntime(15881): at com.example.swipeview3.MainActivity$PlayerFragment.onCreateView(MainActivity.java:142)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 13:33:09.553: E/AndroidRuntime(15881): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2257)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.os.Looper.loop(Looper.java:137)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-23 13:33:09.553: E/AndroidRuntime(15881): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 13:33:09.553: E/AndroidRuntime(15881): at java.lang.reflect.Method.invoke(Method.java:511)
01-23 13:33:09.553: E/AndroidRuntime(15881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-23 13:33:09.553: E/AndroidRuntime(15881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-23 13:33:09.553: E/AndroidRuntime(15881): at dalvik.system.NativeStart.main(Native Method)
这正是我的问题。我不知道在哪里分配它们或在哪里写函数。 sry家伙,但我真的不明白它是如何与片段一起工作的。希望你有理解并可以帮助我:/非常感谢
答案 0 :(得分:0)
您可以将gestureOverlay放入活动中以检测手势和滚动,为第一个活动的关闭/移出和第二个活动的打开/移入设置动画。
答案 1 :(得分:0)
看看这个例子
public class MainActivity extends FragmentActivity
{
private ViewPager mPager;
private FragAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new FragAdapter(getSupportFragmentManager());
mPager.setAdapter(mAdapter);
mPager.setOnPageChangeListener(new OnPageChangeListener()
{
public void onPageSelected(int position)
{
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
public void onPageScrollStateChanged(int state)
{
if (state == ViewPager.SCROLL_STATE_DRAGGING)
{
}
if (state == ViewPager.SCROLL_STATE_IDLE)
{
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class FragAdapter extends FragmentPagerAdapter
{
private Map<Integer, Object> mPageReferenceMap = new HashMap<Integer, Object>();
public FragAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public int getCount()
{
return 3;
}
@Override
public Fragment getItem(int position)
{
Fragment frag;
if (position == 0)
{
frag = new FirstActivity();
}
else if (position == 3)
{
frag = new SecondActivity();
}
else if (position == 6)
{
frag = new ThirdActivity();
}
mPageReferenceMap.put(Integer.valueOf(position),frag);
return frag;
}
@SuppressWarnings("deprecation")
@Override
public void destroyItem(View container, int position, Object object) {
super.destroyItem(container, position, object);
mPageReferenceMap.remove(Integer.valueOf(position));
}
public Object getFragment(int key) {
return mPageReferenceMap.get(key);
}
}
}
现在创建一个新的FirstActivity.java
public class FirstActivity extends Fragment
{
public RadioGroup rythm;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
//position = getArguments().getInt("position");
}
/**
* Handle the results from the voice recognition activity.
*/
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.rythm, container, false);
TextView text = (TextView) view.findViewById(R.id.textView);
text.setText("Select Rythm");
rythm = (RadioGroup) view.findViewById(R.id.rythm);
RadioButton first = (RadioButton) rythm.getChildAt(0);
first.setText("Sinus");
RadioButton second = (RadioButton) rythm.getChildAt(1);
second.setText("Afib");
RadioButton third = (RadioButton) rythm.getChildAt(1);
third.setText("Other");
return view;
}
}
答案 2 :(得分:0)
在其布局文件中仅使用framelayout
使用片段活动。在两个单独的片段中实现两个活动的UI(回想一下,片段实现了活动的所有生命周期方法)。
A Fragment represents a behavior or a portion of user interface in an Activity.
更多关于fragments。
首先在收到用户的guester输入时使用FragmentManager
类向framelayout添加一个片段,用第二个framgemt替换片段。
使用片段可以非常轻松地重用UI元素(假设在扩展应用程序以支持平板电脑的情况下,您希望将两个活动的UI放在一个屏幕上)。