Tabhost窃取焦点

时间:2015-03-09 19:28:57

标签: android-tabhost android-scrollview tabwidget

我的应用程序底部有一个RadioGroup由4个RadioButtons组成。当我按下按钮时,它会启动一个活动。已经启动的活动是一个scrollview,其中有一个TabHost TabWidget ViewPager。当我第一次启动应用程序并使用TabHost活动单击RadioButton时,焦点位于顶部。但是,当我点击另一个RadioButton并返回TabHost活动时。焦点位于viewPager的底部。

public class DActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private TabsPagerAdapter mAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.me_profile);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);

    // Tab Initialization
    initialiseTabHost();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    // Fragments and ViewPager Initialization


    mViewPager.setAdapter(mAdapter);

    View v = this.mTabHost.getTabWidget().getChildAt(0);
    v.setBackgroundColor(Color.parseColor("#fff0f0f0"));


    TextView tv = (TextView) this.mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
    tv.setTextColor(Color.parseColor("#fa4e52"));

    View v2 = this.mTabHost.getTabWidget().getChildAt(1);
    v2.setBackgroundColor(Color.parseColor("#fff0f0f0"));

    TextView tv2 = (TextView) this.mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
    tv2.setTextColor(Color.parseColor("#b3b3b3"));

    mViewPager.setOnPageChangeListener(DActivity.this);
}

// Method to add a TabHost
private static void AddTab(DActivity activity, final TabHost tabHost, TabHost.TabSpec tabSpec) {
    tabSpec.setContent(new MyTabFactory(activity));
    tabHost.addTab(tabSpec);

}

// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
    int pos = this.mTabHost.getCurrentTab();
    this.mViewPager.setCurrentItem(pos);
    for (int i = 0; i < this.mTabHost.getTabWidget().getChildCount(); i++) {
        this.mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#fff0f0f0")); // unselected
        TextView tv = (TextView) this.mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
        tv.setTextColor(Color.parseColor("#b3b3b3"));
    }

    this.mTabHost.getTabWidget().getChildAt(this.mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff0f0f0")); // selected
    TextView tv = (TextView) this.mTabHost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
    tv.setTextColor(Color.parseColor("#fa4e52"));



}


@Override
public void onPageScrollStateChanged(int arg0) {
}

// Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    int pos = this.mViewPager.getCurrentItem();
    this.mTabHost.setCurrentTab(pos);
}

@Override
public void onPageSelected(int arg0) {
}


// Tabs Creation
private void initialiseTabHost() {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    // TODO Put here your Tabs
    DActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("MOST CHAMPION").setIndicator("MOST CHAMPION"));
    DActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("RECENT MATCH").setIndicator("RECENT MATCH"));


    mTabHost.setOnTabChangedListener(this);
}

}

此活动的xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:wheel="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    android:id="@+id/scrollView"
    tools:context="com.example.james.leagueoflegend.SummonerInfo">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout">

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:focusable="true">

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/SummonerFavChamp"
                    android:src="@drawable/ahriclassic"
                    android:layout_alignParentTop="false"

                    android:adjustViewBounds="true"
                    android:baselineAlignBottom="false"
                    android:cropToPadding="false"
                    android:clickable="false"
                     />
                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_alignParentTop="false"
                    android:src="@drawable/champion_gradient"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="false"
                    android:layout_alignParentRight="false" />

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Dark Avenger"
                        android:id="@+id/summonerName"
                        android:textColor="#ffffffff"
                        android:layout_marginLeft="27dp"
                        android:textSize="20dp"
                        android:layout_alignParentLeft="false"
                        android:layout_alignParentStart="false"
                        android:layout_marginStart="27dp"

                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="60dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="win"
                        android:id="@+id/winTitle"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="62dp"
                        android:layout_marginRight="20dp"

                        android:layout_alignParentEnd="false"
                        android:textColor="#ffcecece"
                        android:textSize="12dp"
                        android:alpha="0.9" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="345"
                        android:id="@+id/textView2"
                        android:textColor="#ffffffff"
                        android:layout_alignBottom="@+id/winTitle"
                        android:layout_toLeftOf="@+id/winTitle"
                        android:layout_toStartOf="@+id/winTitle"
                        android:layout_marginRight="5dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="NORMAL"
                        android:id="@+id/textView3"
                        android:layout_above="@+id/winTitle"
                        android:layout_alignRight="@+id/winTitle"
                        android:layout_alignEnd="@+id/winTitle"
                        android:textColor="#ffcecece"
                        android:textSize="10dp"
                        android:alpha="0.9" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Lv 30"
                        android:id="@+id/summonerLevel"
                        android:layout_above="@+id/summonerName"
                        android:layout_alignLeft="@+id/summonerName"
                        android:layout_alignStart="@+id/summonerName"
                        android:textColor="#ffcecece"
                        android:alpha="0.9"
                        android:textSize="12dp" />
                </RelativeLayout>

            </FrameLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="350dp"
                android:layout_marginTop="-50dp"
                android:background="#f0f0f0">

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/Rank3v3"
                    android:layout_weight="0.33"
                    android:touchscreenBlocksFocus="false"
                    android:transitionGroup="false">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/rankTeam3v3Icon"
                        android:layout_centerVertical="true"
                        android:src="@drawable/ch" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Ranked Team (3vs3)"
                        android:id="@+id/rankTeam3v3Name"
                        android:textStyle="italic"
                        android:textColor="#ff757575"
                        android:layout_marginTop="20dp"
                        android:layout_alignParentTop="true"
                        android:textSize="10dp"
                        android:layout_marginLeft="150dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Challenger Tier"
                        android:id="@+id/rankTeam3v3Rank"
                        android:textColor="#2aa2cf"
                        android:textStyle="bold"
                        android:textSize="20dp"
                        android:layout_below="@+id/rankTeam3v3Name"
                        android:layout_alignLeft="@+id/rankTeam3v3Name"
                        android:layout_marginTop="2dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="WIN"
                        android:id="@+id/rankTeam3v3WinName"
                        android:background="#528bcf"
                        android:textColor="#ffffff"
                        android:padding="4dp"
                        android:textSize="10dp"
                        android:layout_below="@+id/rankTeam3v3Rank"
                        android:layout_alignLeft="@+id/rankTeam3v3Rank"
                        android:layout_marginTop="10dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="LOSE"
                        android:id="@+id/rankTeam3v3LoseName"
                        android:background="#e64f45"
                        android:textColor="#ffffff"
                        android:textSize="10dp"
                        android:padding="4dp"
                        android:layout_alignTop="@+id/rankTeam3v3WinName"
                        android:layout_toRightOf="@+id/rankTeam3v3WinName"
                        android:layout_marginLeft="40dp" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/RankSolo"
                    android:layout_weight="0.33"
                    android:background="@drawable/tab_top_border">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/soloIcon"
                        android:layout_centerVertical="true"
                        android:src="@drawable/difive" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Solo/Duo Que"
                        android:id="@+id/soloName"
                        android:textStyle="italic"
                        android:textColor="#ff757575"
                        android:layout_marginTop="20dp"
                        android:layout_alignParentTop="true"
                        android:textSize="10dp"
                        android:layout_marginLeft="150dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Diamond IV"
                        android:id="@+id/soloRank"
                        android:textColor="#695cdc"
                        android:textStyle="bold"
                        android:textSize="20dp"
                        android:layout_centerVertical="true"
                        android:layout_alignLeft="@+id/soloName"
                        android:layout_marginTop="2dp"
                        android:layout_below="@+id/soloName" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="WIN"
                        android:id="@+id/soloWin"
                        android:background="#528bcf"
                        android:textColor="#ffffff"
                        android:padding="4dp"
                        android:textSize="10dp"
                        android:layout_below="@+id/soloRank"
                        android:layout_alignLeft="@+id/soloRank"
                        android:layout_marginTop="10dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="LOSE"
                        android:id="@+id/soloLose"
                        android:background="#e64f45"
                        android:textColor="#ffffff"
                        android:textSize="10dp"
                        android:padding="4dp"
                        android:layout_alignTop="@+id/soloWin"
                        android:layout_marginLeft="40dp"
                        android:layout_toRightOf="@+id/soloWin" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/Rank5v5"
                    android:layout_weight="0.33"
                    android:background="@drawable/tab_top_border">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/rankTeam5v5Icon"
                        android:src="@drawable/plthree"
                        android:layout_centerVertical="true"
                        android:layout_alignParentLeft="true" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Solo/Duo Que"
                        android:id="@+id/rankTeam5v5Name"
                        android:textStyle="italic"
                        android:textColor="#ff757575"
                        android:layout_marginTop="20dp"
                        android:layout_alignParentTop="true"
                        android:textSize="10dp"
                        android:layout_marginLeft="150dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Platinum V"
                        android:id="@+id/rankTeam5v5Rank"
                        android:textColor="#109b83"
                        android:textStyle="bold"
                        android:textSize="20dp"
                        android:layout_alignLeft="@+id/rankTeam5v5Name"
                        android:layout_below="@+id/rankTeam5v5Name"
                        android:layout_marginTop="2dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="WIN"
                        android:id="@+id/rankTeam5v5WinName"
                        android:background="#528bcf"
                        android:textColor="#ffffff"
                        android:padding="4dp"
                        android:textSize="10dp"
                        android:layout_marginTop="10dp"
                        android:layout_below="@+id/rankTeam5v5Rank"
                        android:layout_alignLeft="@+id/rankTeam5v5Rank" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="LOSE"
                        android:id="@+id/rankTeam5v5LoseName"
                        android:background="#e64f45"
                        android:textColor="#ffffff"
                        android:textSize="10dp"
                        android:padding="4dp"
                        android:layout_alignTop="@+id/rankTeam5v5WinName"
                        android:layout_toRightOf="@+id/rankTeam5v5WinName"
                        android:layout_marginLeft="40dp" />
                </RelativeLayout>
            </LinearLayout>


            <TabHost
                android:id="@android:id/tabhost"
                android:layout_width="fill_parent"
                android:layout_height="500dp"
                >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    >

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:orientation="horizontal"

                        android:measureWithLargestChild="false" />

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="0dp"
                        android:layout_height="0dp"

                        android:layout_weight="0" />

                    <android.support.v4.view.ViewPager
                        android:id="@+id/viewpager"
                        android:layout_width="fill_parent"
                        android:layout_height="1000dp"

                        />
                </LinearLayout>
            </TabHost>

        </LinearLayout>


    </RelativeLayout>
</ScrollView>

图片:1。当我点击活动时:http://postimg.org/image/53skngik3/ 图片:2。当我使用滚动视图向下滑动时:http://postimg.org/image/3sllp75nx/ 图3.当我点击另一个单选按钮,然后回到我的活动:http://postimg.org/image/7400ebpzf/

0 个答案:

没有答案