1

私のアクティビティでは、Viewpager 内で Fragment を使用しています。私のフラグメントには、ImageView や ImageButton などのアイテムが含まれています。私のフラグメントで指定されたアイテムにアクセスすることは可能ですか? これをコントロールしたい。私のプロジェクトでは、キーボードの指定されたボタンを押したときに Fragment の最初の ImageButton にフォーカスを設定したいですか? ありがとうございます!

main.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. アクティビティ (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 に答える