我试图达到以下效果:
2个屏幕,第一个是我的主要活动,第二个是用户可以更改主要活动的应用属性。
我希望我的应用使用左右/左右滑动手势在屏幕之间移动。我不希望这只是来回徘徊。
我做了一些研究,并在SO上找到了其他人的建议,但似乎无法正确实现这一点。
根据以下答案编辑代码:
这是我的Manifest代码:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.sample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".ScreenSlideActivity"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我的ScreenSlideActivity:
package com.sample.sample;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class ScreenSlideActivity extends FragmentActivity {
//number of pages
private static final int NUM_PAGES = 2;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
我不知道如何为另一个页面创建多个此实例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
然后,这曾经起作用,但现在它没有添加屏幕幻灯片:
我的ButtonActivity:
package com.example.android.animationsdemo;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class ButtonActivity extends ScreenSlideActivity {
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
static int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_screen_slide_page);
setupPlayPauseButton1();
setupPlayPauseButton2();
setupPlayPauseButton3();
setupPlayPauseButton4();
setupPlayPauseButton5();
}
private void setupPlayPauseButton1() {
btn1 = (ImageButton) findViewById(R.id.m1_btn);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn1.setImageResource(R.drawable.pause);
} else {
btn1.setImageResource(R.drawable.play);
}
}
});
}
private void setupPlayPauseButton2() {
btn2 = (ImageButton) findViewById(R.id.m2_btn);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn2.setImageResource(R.drawable.pause);
} else {
btn2.setImageResource(R.drawable.play);
}
}
});
}
private void setupPlayPauseButton3() {
btn3 = (ImageButton) findViewById(R.id.m3_btn);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn3.setImageResource(R.drawable.pause);
} else {
btn3.setImageResource(R.drawable.play);
}
}
});
}
private void setupPlayPauseButton4() {
btn4 = (ImageButton) findViewById(R.id.m4_btn);
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn4.setImageResource(R.drawable.pause);
} else {
btn4.setImageResource(R.drawable.play);
}
}
});
}
private void setupPlayPauseButton5() {
btn5 = (ImageButton) findViewById(R.id.m5_btn);
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn5.setImageResource(R.drawable.pause);
} else {
btn5.setImageResource(R.drawable.play);
}
}
});
}
}
ScreeSlidePageFragment:
package com.example.android.animationsdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ScreenSlidePageFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
public static ScreenSlidePageFragment create(int pageNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
}
activity_screen_slide.xml:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_screen_slide_page.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:orientation="vertical"
tools:context=".ScreenSlideActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/m1_btn_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:orientation="vertical" >
<ImageButton
android:id="@+id/m1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/m2_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@null"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/m3_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@null"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/m4_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@null"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/m5_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@null"
android:src="@drawable/pause" />
</LinearLayout>
<LinearLayout
android:id="@+id/m1_stave_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/m1_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:background="@drawable/stave_top"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/m2_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:background="@drawable/stave_middle"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/m3_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:background="@drawable/stave_middle"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/m4_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:background="@drawable/stave_middle"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/m5_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:background="@drawable/stave_bottom"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
修改 PageTwoFragment:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class PageTwoFragment extends Fragment {
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
int i = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.page1_layout, container, false);
btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
}
private void setupPlayPauseButton1(View view) {
btn1 = (ImageButton) view.findViewById(R.id.m1_btn);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn1.setImageResource(R.drawable.pause);
} else {
btn1.setImageResource(R.drawable.play);
}
}
});
}
}
答案 0 :(得分:3)
您可以使用Androids ViewPager完成所需的行为。您需要为两个视图使用Fragments,但它的工作方式相同。
ViewPagers具有内置的滑动手势来转换页面,默认情况下它们会显示屏幕幻灯片动画,因此您无需创建任何。
从Android文档中查看此示例,它有一个示例应用程序,您可以下载并测试它。
http://developer.android.com/training/animation/screen-slide.html
如果有帮助,请告诉我,
祝你好运!
修改强>
基本上您需要做的是确保要在其间滑动的视图是片段。一旦你将它们作为片段放在你的PagerAdapters getItem(int Position)方法中,你就可以这样做:
@Override
public Fragment getItem(int i) {
Fragment frag = null;
switch (i) {
case 0:
frag = PageOneFragment.create();
break;
case 1:
frag = PageTwoFragment.create();
break;
}
return frag;
}
每个片段都可以有自己的xml文件,可以是布局所需的任何内容。每个片段都可以包含可以添加侦听器的按钮和图像等元素。
有意义吗?如果没有,请告诉我崩溃的位置,并发布错误。
编辑2
开发人员网站上的示例确实有很多代码,因为它将一些演示包装成一个。看看下面的代码,它完全符合你想要的滑动,你只需要将你的xml添加到两个片段中。
MainActivity
public class MainActivity extends FragmentActivity {
private final static int NUM_PAGES = 2;
private ViewPager mPager;
private ScreenSlidePagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mAdapter);
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects,
* in sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment frag = null;
switch (i) {
case 0:
frag = new PageOneFragment();
break;
case 1:
frag = new PageTwoFragment();
break;
}
return frag;
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
activity_main.xml中
<RelativeLayout 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=".MainActivity" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
PageOneFragment(这将是您想要的第一个视图,您可以为其提供自定义xml布局并定义元素,就像在活动中一样)
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PageOneFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_one, container, false);
return rootView;
}
}
fragment_one.xml(第一个视图的布局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:padding="16dp"
android:text="Example text" />
FragmentTwo(这将是你的第二个视图,你滑到的视图)
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PageTwoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_two, container, false);
return rootView;
}
}
fragment_two.xml(第二个视图的布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_dark"
android:orientation="vertical" >
</LinearLayout>
从 EDIT 2 下来是示例应用程序的简单版本,您需要做的就是为这两个片段添加自定义xml布局。如果您想了解有关片段及其功能的更多信息,这是一个很好的起点:
http://developer.android.com/guide/components/fragments.html
onCreateView编辑
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.page1_layout, container, false);
btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
setupPlayPauseButton1(rootView);
return rootView;
}
祝你好运!