问题
Is this even possible? I created this view flipper and I am calling the view flipper from the onClick inside of the xml layout. I have created a view flipper before but for some reason it's not working.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@android:color/transparent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:cacheColorHint="@android:color/transparent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vf_sample1_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@android:color/transparent"
android:orientation="vertical" >
<include layout="@layout/headers_main" />
<include layout="@layout/searchbar" />
<TextView
android:id="@+id/sdfsd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:maxHeight="85dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
onClick="gotoSomething"
android:singleLine="false" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/aquablue"
android:textSize="15dp"
android:textStyle="bold" />
<include layout="@layout/listview_item_row_now_playing_info" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/aquablue"
android:textSize="15dp"
android:textStyle="bold" />
<include layout="@layout/container_listview" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vf_sdfsdfadf"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:cacheColorHint="@android:color/transparent"
android:orientation="vertical" >
<include layout="@layout/header_vf_asdfasdfadadafsd" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vf_adsfasdfadsfasd"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:cacheColorHint="@android:color/transparent"
android:orientation="vertical" >
</LinearLayout>
</ViewFlipper>
</LinearLayout>
code:
public class BeatSpotPrototypeActivity extends BaseListActivity {
private final String TAG = BeatSpotPrototypeActivity.class.getSimpleName();
private ApplicationStateManager mAppStateMananger = ApplicationStateManager.GetInstance();
private Context mCtx = null;
private boolean mIsInitalized = false;
private boolean mDataChanded= false;
private ViewFlipper mViewFlipper= null;
private DrawableManager mDrawManager = new DrawableManager();
private enum ViewFlipperModes {
MAIN (0),
NOW_PLAYING (1),
SONG_INFORMATION (2);
private final int index;
ViewFlipperModes(int index) {
this.index = index;
}
public int index() {
return index;
}
}
</code>
<code>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_beatspot_activity);
if (!mIsInitalized)
init();
else
mDataChanded = true;
showLoading(false);
}
private void init() {
mCtx = this;
mIsInitalized = true;
mViewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
fadeDrawables();
}
public void gotoBeatspotMain(View v) {
mViewFlipper.setDisplayedChild(ViewFlipperModes.MAIN.index());
}
public void gotoNowPlaying(View v) {
Log.d(TAG, mViewFlipper.getCurrentView().toString());
mViewFlipper.setDisplayedChild(ViewFlipperModes.NOW_PLAYING.index());
}
public void gotoSongDetails(View v) {
mViewFlipper.setDisplayedChild(ViewFlipperModes.SONG_INFORMATION.index());
Log.d(TAG, String.valueOf(ViewFlipperModes.SONG_INFORMATION.index()));
}
}
回答1:
before on create
ViewFlipper mViewFlipper = (ViewFlipper)findViewbyId(location);
on your onCreate() or onStart
Ensure that whatever your layout on setcontentView(R.layout.something) does contain the view flipper
Also try this for me. before this line mViewFlipper.setDisplayedChild(ViewFlipperModes.MAIN.index()); Type in:
int i = (ViewFlipperModes.MAIN.index()); place a debug point here
mViewFlipper.setDisplayedChild(i);
I also suggest using the debugger to try figure out if your flip views events are being called inside the debugger.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/book_as_whole_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<ViewFlipper
android:layout_marginTop="20dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ViewFlipper">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/page1"
android:text="@string/loading_string"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:typeface="serif"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/page2"
android:text="@string/loading_string"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:typeface="serif" />
</LinearLayout>
</ViewFlipper>
</LinearLayout>
回答2:
setDisplayedChild
takes an index, not an id.
来源:https://stackoverflow.com/questions/10725855/view-flipper-is-not-flipping