Android : Android TV control focus from imageViewA in fragmentA to another imageViewB in fragmentB

on Friday, September 12, 2014


I am building an app for Google TV ,I Have a FragmentActivity ActivityA , and three Fragment FragmentA 、FragmentB 、FragmentC,in each fragment there are some ImageView.


Just as the pic shows below:


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>


Now i use D-pad move my focus from ImageView IV_2 to IV_7,however it do not work as I want .More often when I use D-pad move focus from IV_2 , the focus goes to IV_8!


For this I use android:nextFocusRight to specify the next focus right view ,but it do not work.


How can i control focus when i focused at IV_2 in FragmentA move to IV_7 in FragmentB ?


0 comments:

Post a Comment