1

在我的活动中,我在 Viewpager 中使用了片段。我的片段包括一些像 ImageView 和 ImageButton 的项目。是否可以访问我的片段中的指定项目?我想控制这个。在我的项目中,当我按下键盘上的指定按钮时,我想将焦点设置为 Fragment 中的第一个 ImageButton ?谢谢你的帮助!

主.xml ....

            <android.support.v4.view.ViewPager
                android:id="@+id/viewPagerDetail"
                android:layout_width="1170dp"
                android:layout_height="140dp" >
            </android.support.v4.view.ViewPager>

            <LinearLayout
                android:id="@+id/motionCotrol"
                android:layout_marginTop="10dp"
                android:layout_width="84dp"
                android:layout_height="120dp"
                android:paddingBottom="15dp">

                <ImageView
                    android:id="@+id/img"
                    android:layout_width="84dp"
                    android:layout_height="105dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/selection_thick" 
                    />

            </LinearLayout> 
        </RelativeLayout>

…………

片段.xml

<LinearLayout
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >

    <!--
         <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.1"
         />
    -->

    <include

        android:layout_width="100dp"
        android:layout_height="wrap_content"
        layout="@layout/items" />

    <include
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        layout="@layout/items" />

    <include
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        layout="@layout/items" />
4

1 回答 1

0

1. 在您的片段类中创建一个自定义 KeyDown 事件。


    public void keyDownInFragment(int keyCode){
      // Your Implementation for focusing a widget goes here
    }
    

2. 在您的 Activity(FragmentActivity 或 MainActivity)中,使用以下代码捕获 KeyDown 事件,

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Replace the key you have pressed in Keyboard for KeyEvent.KEYCODE_HOME in the following line
        if (keyCode == KeyEvent.KEYCODE_HOME) {
           // Call your fragment's keydown event method
                  ((FragmentName)fragments.get(0)).myOnKeyDown(keyCode);
           // get(0) instead of zero user the index number at which you have inserted your fragment in the Page Adapter
        }
    }
于 2012-09-26T11:18:03.147 回答