我试图实现一个 CustomViewPager,但它说支持 ViewPager 不能被转换为自定义的。我对非 CustomViewPager 的来源感到困惑。
(包名被替换以保护隐私)
这是我的 CustomViewPager
package com.domain.heswresources;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* Created by andrewb on 30/07/2015.
*/
public class CustomViewPager extends ViewPager {
private Boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(Boolean enabled) {
this.enabled = enabled;
}
}
这是我的 Activity :
package com.domain.heswresources;
import android.app.ActionBar;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import com.domain.CustomViewPager;
import com.viewpagerindicator.CirclePageIndicator;
public class TutorialActivity extends FragmentActivity {
private static final int NUM_PAGES = 4;
private int pP = 1;
private CustomViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
ActionBar ab = getActionBar();
ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.dash_action_bar));
ab.setLogo(getResources().getDrawable(R.drawable.ic_logo));
ab.setTitle("Getting started");
getWindow().setBackgroundDrawableResource(R.drawable.main_background);
mPager = (CustomViewPager) findViewById(R.id.pager);
mPagerAdapter = new TutorialPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//Bind the title indicator to the adapter (requires the ViewPagerIndicator library by Jake Wharton)
CirclePageIndicator pageIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
pageIndicator.setViewPager(mPager);
pageIndicator.setOnPageChangeListener(new CustomViewPager.OnPageChangeListener() {
boolean isReverse = false;
@Override
public void onPageSelected(int page) {
if (String.valueOf(pP).equals(String.valueOf(page))) {
isReverse = true;
} else {
isReverse = false;
}
switch (page) {
case 0:
getWindow().setBackgroundDrawableResource(R.drawable.tran1);
TransitionDrawable trans1 = (TransitionDrawable) getWindow().getDecorView().getBackground();
trans1.startTransition(0);
trans1.reverseTransition(500);
break;
case 1:
if (isReverse) {
getWindow().setBackgroundDrawableResource(R.drawable.tran2);
TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
trans2.startTransition(0);
trans2.reverseTransition(500);
} else {
getWindow().setBackgroundDrawableResource(R.drawable.tran1);
TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
trans2.startTransition(500);
}
break;
case 2:
if (isReverse) {
getWindow().setBackgroundDrawableResource(R.drawable.tran3);
TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
trans2.startTransition(0);
trans2.reverseTransition(500);
Log.v("ANDY2", "page 4 to 3");
} else {
getWindow().setBackgroundDrawableResource(R.drawable.tran2);
TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
trans2.reverseTransition(2000);
}
break;
case 3:
if (isReverse) {
Log.v("ANDY2", "page 5 to 4");
} else {
getWindow().setBackgroundDrawableResource(R.drawable.tran3);
TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
trans2.reverseTransition(500);
}
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
pP = arg0;
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
public void OnClick(View v) {
int id = v.getId();
if (id == R.id.button1) {
Editor ed = MainActivity.prefs.edit();
ed.putString("type", "Acute");
//ed.putBoolean(MainActivity.SHOW_TUTORIAL, false);
ed.commit();
finish();
}
}
public void OnClick2(View v) {
int id = v.getId();
if (id == R.id.button2) {
Editor ed = MainActivity.prefs.edit();
ed.putString("type", "Mental Health");
//ed.putBoolean(MainActivity.SHOW_TUTORIAL, false);
ed.commit();
finish();
}
}
private class TutorialPagerAdapter extends FragmentPagerAdapter {
public TutorialPagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public TutorialFragment getItem(int position) {
return TutorialFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
还有我的 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.domain.CustomViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
它基本上落在这一行 - >
mPager = (CustomViewPager) findViewById(R.id.pager);
但我不知道为什么这个变量被设置为 CustomViewPager。
日志输出(如果有帮助)
07-30 13:50:05.630 19208-19208/com.domain.heswresources E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.domain.heswresources, PID: 19208
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.heswresources/com.domain.heswresources.TutorialActivity}: java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to com.domain.heswresources.CustomViewPager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to com.domain.heswresources.CustomViewPager
at com.domain.heswresources.TutorialActivity.onCreate(TutorialActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
任何帮助,将不胜感激!
最佳答案
引用 Sir Arthur Conan Doyle's The Sign of the Four 中的虚构人物 Sherlock Holmes :
如果您对布局进行膨胀,并且 Android 返回 ViewPager
,很可能是因为布局包含 ViewPager
,而不是您期望的(在本例中为 CustomViewPager
)。拥有多个版本的布局资源使这一点变得复杂,因为也许您正在处理的那个没有 ViewPager
,但另一个有。
如果您对命令行感到满意,那么搜索 fgrep
的 res/
目录树的 android.support.v4.view.ViewPager
将是一种追踪可能仍引用 ViewPager
的布局资源的方法。