我正在构建一个适用于Google TV的应用程序,我有一个FragmentActivity ActivityA,以及三个Fragment FragmentA,FragmentB,FragmentC,每个片段中都有一些ImageView。
如下图所示:
http://drp.io/files/541275c964ae0.png
MainActivityGameLTV2:
public class MainActivityGameLTV2 extends FragmentActivity{
private static final String TAG ="MainActivityGameLTV2";
private WoDouViewPager mViewPager;
//记录当前选中位置
private int currentIndex;
//主页页面数量
private static final int PAGE_NUMBER = 3;
private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main_activity_game_lt_v2);
initView();
initData();
}
private void initData() {
initFragment();
}
private void initView() {
mViewPager = (WoDouViewPager) this.findViewById(R.id.view_pager_game_v2);
}
/**
* 初始化Fragment
*/
private void initFragment() {
fragments.clear();//清空
int count = PAGE_NUMBER;
MainGameLeftFragment fragmentLeft = new MainGameLeftFragment();
MainGameMiddleFragment fragmentMiddle = new MainGameMiddleFragment();
MainGameRightFragment fragmentRight = new MainGameRightFragment();
fragments.add(fragmentLeft);
fragments.add(fragmentMiddle);
fragments.add(fragmentRight);
MainActivityGameLTV2FragmentAdapter mAdapter = new MainActivityGameLTV2FragmentAdapter(getSupportFragmentManager(), fragments);
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(pageListener);
mViewPager.setCurrentItem(1);// 共三页设置第二页居中
}
/**
* ViewPager切换监听方法
*/
public ViewPager.OnPageChangeListener pageListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
mViewPager.setCurrentItem(position);
}
};
}
view_pager_game_v2.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewPager
android:background="@drawable/wodou_bj"
android:id="@+id/view_pager_game_v2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MainActivityGameLTV2FragmentAdapter:
public class MainActivityGameLTV2FragmentAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> mFragments;
private FragmentManager fm;
public MainActivityGameLTV2FragmentAdapter(FragmentManager fm,ArrayList<Fragment> fragments) {
super(fm);
mFragments = fragments;
this.fm=fm;
}
@Override
public Fragment getItem(int i) {
return mFragments.get(i);
}
@Override
public int getCount() {
return mFragments.size();
}
}
MainGameRightFragment:FragmentA:
public class MainGameRightFragment extends WoDouGameBaseFragment{
ImageView mIv_01;
ImageView mIv_02;
ImageView mIv_03;
ImageView mIv_04;
ImageView mIv_05;
ImageView mIv_06;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.main_game_right_fragment, null);
initView(view);
return view;
}
private void initView(View view) {
mIv_01 = (ImageView) view.findViewById(R.id.wodou_01);
mIv_02 = (ImageView) view.findViewById(R.id.wodou_02);
mIv_03 = (ImageView) view.findViewById(R.id.wodou_03);
mIv_04 = (ImageView) view.findViewById(R.id.wodou_04);
mIv_05 = (ImageView) view.findViewById(R.id.wodou_05);
mIv_06 = (ImageView) view.findViewById(R.id.wodou_06);
mIv_01.setOnFocusChangeListener(mFocusChangeListener);
mIv_02.setOnFocusChangeListener(mFocusChangeListener);
mIv_03.setOnFocusChangeListener(mFocusChangeListener);
mIv_04.setOnFocusChangeListener(mFocusChangeListener);
mIv_05.setOnFocusChangeListener(mFocusChangeListener);
mIv_06.setOnFocusChangeListener(mFocusChangeListener);
}
}
main_game_right_fragment.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:id="@+id/titleview_myapps_about"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04"
android:gravity="center"
>
<ImageButton
android:id="@+id/wodou_01"
android:layout_width="206dp"
android:nextFocusRight="@+id/wodou_02"
android:layout_height="205dp"
android:layout_marginBottom="6dp"
android:background="@drawable/sharp_background"
android:scaleType="fitXY"
android:src="@drawable/wodou_game_01"
android:tag="#FF666666"
android:layout_marginRight="23dp"
android:alpha="255"/>
<ImageButton`enter code here`
android:id="@+id/wodou_04"
android:layout_width="206dp"
android:layout_height="205dp"
android:background="@drawable/sharp_background"
android:scaleType="fitXY"
android:src="@drawable/wodou_game_04"
android:tag="#FF666666"
android:layout_alignTop="@+id/wodou_05"
android:layout_alignLeft="@+id/wodou_01"/>
<ImageButton
android:id="@+id/wodou_02"
android:layout_width="446dp"
android:layout_height="420dp"
android:layout_marginLeft="10dp"
android:nextFocusRight="@+id/wodou_03"
android:background="@drawable/sharp_background"
android:scaleType="fitXY"
android:src="@drawable/wodou_game_02"
android:tag="#FF666666"
android:layout_alignTop="@+id/wodou_01"
android:layout_toRightOf="@+id/wodou_04"/>
<ImageButton
android:id="@+id/wodou_03"
android:layout_width="423dp"
android:layout_height="205dp"
android:layout_marginLeft="10dp"
android:background="@drawable/sharp_background"
android:scaleType="fitXY"
android:src="@drawable/wodou_game_03"
android:tag="#FF666666"
android:layout_alignTop="@+id/wodou_02"
android:layout_toRightOf="@+id/wodou_02"/>
<ImageButton
android:id="@+id/wodou_05"
android:layout_width="206dp"
android:layout_height="205dp"
android:background="@drawable/sharp_background"
android:scaleType="fitXY"
android:src="@drawable/wodou_game_05"
android:tag="#FF666666"
android:layout_alignBottom="@+id/wodou_02"
android:layout_alignLeft="@+id/wodou_03"/>
<ImageButton
android:id="@+id/wodou_06"
android:layout_width="206dp"
android:layout_height="205dp"
android:background="@drawable/sharp_background"
android:scaleType="fitXY"
android:src="@drawable/wodou_game_06"
android:tag="#FF666666"
android:layout_alignTop="@+id/wodou_05"
android:layout_alignRight="@+id/wodou_03"/>
</RelativeLayout>
</LinearLayout>
现在我使用D-pad将我的焦点从FragmentA中的ImageView IV_2移动到FragmentB中的IV_7,但它不能正常工作。更常见的是当我使用D-pad从IV_2移动焦点时,焦点转到IV_8或FragmentB中的其他imageView!
为此,我使用android:nextFocusRight指定下一个焦点右视图,但它不起作用。
当我专注于FragmentA中的IV_2移动到FragmentB中的IV_7时,我如何控制焦点?
答案 0 :(得分:3)
下面的示例代码将帮助您将焦点从一个视图元素移动到另一个视图元素:
public class DpadActivity extends Activity {
private ImageView iv1,iv2;
private static Toast tst;
private int current_position=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dpad);
iv1=(ImageView)findViewById(R.id.imageView1);
iv2=(ImageView)findViewById(R.id.ImageView2);
iv1.requestFocus();
iv1.setBackgroundResource(R.drawable.hilight);
iv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "iv2 clicked", Toast.LENGTH_LONG).show();
}
});
iv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "iv1 clicked", Toast.LENGTH_LONG).show();
}
});
tst.makeText(getApplicationContext(), "Ha hai", Toast.LENGTH_LONG).show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_DPAD_CENTER == keyCode
|| KeyEvent.KEYCODE_ENTER == keyCode) {
tst.makeText(getApplicationContext(), "Enter Pressed", Toast.LENGTH_LONG).show();
return true;
} else if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode) {
tst.makeText(getApplicationContext(), "Left Pressed", Toast.LENGTH_LONG).show();
return true;
} else if (KeyEvent.KEYCODE_DPAD_RIGHT == keyCode) {
tst.makeText(getApplicationContext(), "Right Pressed", Toast.LENGTH_LONG).show();
return true;
} else if (KeyEvent.KEYCODE_DPAD_UP == keyCode) {
if(current_position==1)
{
iv2.requestFocus();
current_position=2;
}else if(current_position==2)
{
iv1.requestFocus();
current_position=1;
}
tst.makeText(getApplicationContext(), "Up Pressed", Toast.LENGTH_LONG).show();
return true;
} else if (KeyEvent.KEYCODE_DPAD_DOWN == keyCode) {
if(current_position==1)
{
iv2.requestFocus();
current_position=2;
}else if(current_position==2)
{
iv1.requestFocus();
current_position=1;
}
tst.makeText(getApplicationContext(), "Down Pressed", Toast.LENGTH_LONG).show();
return true;
}
Toast.makeText(getApplicationContext(), "Nothing Detected", Toast.LENGTH_LONG).show();
return super.onKeyDown(keyCode, event);
}
}