我试图达到以下效果:
2 个屏幕,第一个是我的主要活动,第二个是用户可以更改主要活动的应用程序属性的地方。
我希望我的应用程序使用左右/左右滑动手势在屏幕之间移动。我不希望这个来回循环。
我做了一些研究,发现了其他人对 SO 的建议,但似乎仍然无法正确实施。
根据以下答案编辑代码:
这是我的清单代码:
<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);
然后,这曾经可以工作,但现在自从添加了屏幕幻灯片后就不行了:
我的按钮活动:
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);
}
}
});
}
}