0

这甚至可能吗?我创建了这个视图翻转器,我从 xml 布局内部的 onClick 调用视图翻转器。我之前创建了一个视图翻转器,但由于某种原因它不起作用。

<?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>

代码:

 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()));
        }

    }
4

2 回答 2

3

setDisplayedChild需要一个索引,而不是一个 id。

于 2012-05-23T18:39:52.820 回答
1

在创建之前

ViewFlipper mViewFlipper = (ViewFlipper)findViewbyId(location);

在你的 onCreate() 或 onStart

确保您在 setcontentView(R.layout.something) 上的任何布局都包含视图翻转器

也给我试试这个。在此行之前 mViewFlipper.setDisplayedChild(ViewFlipperModes.MAIN.index()); 输入:

int i = (ViewFlipperModes.MAIN.index()); place a debug point here
mViewFlipper.setDisplayedChild(i);

我还建议使用调试器来尝试确定您的翻转视图事件是否在调试器中被调用。

<?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>
于 2012-05-24T16:59:41.967 回答