tabhost 通過popup跳轉activity

RadioGroup保持圖片原來大小,的方法:
	android:text=""
        android:textSize="1sp"

去掉RadioButton的樣式:
	android:button="@null"

taphost通過popup跳轉activity:


import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TabWidget;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by zuox on 2016/3/12.
 */
public class T_MainNewActivity extends Activity implements
        CompoundButton.OnCheckedChangeListener , HomeChannelAdapter.ItemListener{

    private RadioButton mR1;
    private RadioButton mR2;
    private RadioButton mR3;
    private RadioGroup mRadioGroup;
    private PopupWindow mPopupWindow;

    private LocalActivityManager localActivityManager;
    private TabHost tabHost;
    private TabWidget tabWidget;
    private RadioGroup radiogroup_btn;

    private RecyclerView mRecyclerView;

    private HomeChannelAdapter mAdapter;
    private String[] tabNames;
    private Intent[] intents;

    private List<HomeChannel> homeChannels=new ArrayList<HomeChannel>();

    /**
     * 基準屏幕密度
     */
    public double STANDARD_DENSITY = 160;
    private boolean isvisible = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.t_activity_maintwo);
        localActivityManager = new LocalActivityManager(this, true);
        localActivityManager.dispatchCreate(savedInstanceState);
        tabNames = new String[]{"MAIN", "USER", "TV", "LIVESHOW", "LIVING", "SPORT", "BROADCAST", "RECORDTHELIVE"};
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabWidget = (TabWidget) findViewById(android.R.id.tabs);
        tabHost.setup(localActivityManager);
        mR1 = ((RadioButton) findViewById(R.id.radio_button0));
        mR2 = ((RadioButton) findViewById(R.id.radio_button1));
        mR3 = ((RadioButton) findViewById(R.id.radio_button2));
        mRadioGroup = (RadioGroup)findViewById(R.id.main_radio);

        initPopupMenu();
    }



    private void initPopupMenu()
    {
        View mPopupMenu = LayoutInflater.from(this).inflate(R.layout.popupmenu, null);
        mPopupWindow =
                new PopupWindow(mPopupMenu, LinearLayout.LayoutParams.FILL_PARENT,
                        DensityUtils.dp2px(this,160+53),false);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mRecyclerView = (RecyclerView)mPopupMenu.findViewById(R.id.home_channel);
        if(mRecyclerView != null) {
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
            homeChannels.clear();
            homeChannels.add(new HomeChannel("電視", R.drawable.t_home_channel_01));
            homeChannels.add(new HomeChannel("LiveShow",R.drawable.t_home_channel_02));
            homeChannels.add(new HomeChannel("全民播",R.drawable.t_home_channel_03));
            homeChannels.add(new HomeChannel("體育賽事", R.drawable.t_home_channel_04));
            homeChannels.add(new HomeChannel("電臺", R.drawable.t_home_channel_05));
            homeChannels.add(new HomeChannel("錄直播", R.drawable.t_home_channel_06));
            mRecyclerView.setAdapter(mAdapter == null ? mAdapter = new HomeChannelAdapter(this) : mAdapter);

            mAdapter.setListener(this);
        }
    }


    private void dissmissPopupWindows()
    {
        if (mPopupWindow != null && mPopupWindow.isShowing())
        {
            mPopupWindow.dismiss();
            setImg(R.drawable.t_home_channel_nor, mR2);//選中時圖片更換
            isvisible = false;

        }
    }


    @Override
    public void onItemOnClick(int position) {
        if(isvisible){
            dissmissPopupWindows();
        }
        if(tabHost!=null)tabHost.setCurrentTab(position);
    }

    private void setImg(int img, RadioButton r){
        Drawable drawable= getResources()
                .getDrawable(img);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
        r.setCompoundDrawables(null, drawable, null, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.sendEmptyMessage(0);
        setImg(R.drawable.t_home_sel, mR1);
        mR2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(mPopupWindow != null && mPopupWindow.isShowing())) {
                    isvisible = false;
                }
                if (!isvisible) {
                    //頻道選中時 頻道圖片設置爲選中,其他兩個設置爲未選中圖片
                    setImg(R.drawable.t_home_channel_sel, mR2);
                    setImg(R.drawable.t_home_nor,mR1);
                    setImg(R.drawable.t_home_user_nor,mR3);
                    mPopupWindow.showAsDropDown(mR2, 0, 0);
                    isvisible = true;
                } else {
                    dissmissPopupWindows();
                }

            }
        });
        mAdapter.addItems(homeChannels);

        mR1.setOnCheckedChangeListener(T_MainNewActivity.this);
        mR2.setOnCheckedChangeListener(T_MainNewActivity.this);
        mR3.setOnCheckedChangeListener(T_MainNewActivity.this);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            TabHost.TabSpec spec1 = tabHost.newTabSpec("MAIN");
            spec1.setContent(new Intent(T_MainNewActivity.this, T_MainNewActivity.class));
            spec1.setIndicator("MAIN");

            TabHost.TabSpec spec2 = tabHost.newTabSpec("USER");
            spec2.setContent(new Intent(T_MainNewActivity.this, T_UserActivity.class));
            spec2.setIndicator("USER");

            TabHost.TabSpec spec3 = tabHost.newTabSpec("TV");
            spec3.setContent(new Intent(T_MainNewActivity.this, T_TvActivity.class));
            spec3.setIndicator("TV");

            TabHost.TabSpec spec4 = tabHost.newTabSpec("LIVESHOW");
            spec4.setContent(new Intent(T_MainNewActivity.this, T_LiveShowActivity.class));
            spec4.setIndicator("LIVESHOW");

            TabHost.TabSpec spec5 = tabHost.newTabSpec("LIVING");
            spec5.setContent(new Intent(T_MainNewActivity.this, T_LivingActivity.class));
            spec5.setIndicator("LIVING");

            TabHost.TabSpec spec6 = tabHost.newTabSpec("SPORT");
            spec6.setContent(new Intent(T_MainNewActivity.this, T_SportActivity.class));
            spec6.setIndicator("SPORT");

            TabHost.TabSpec spec7 = tabHost.newTabSpec("BROADCAST");
            spec7.setContent(new Intent(T_MainNewActivity.this, T_BroadcastActivity.class));
            spec7.setIndicator("BROADCAST");

            TabHost.TabSpec spec8 = tabHost.newTabSpec("RECORDTHELIVE");
            spec8.setContent(new Intent(T_MainNewActivity.this, T_RecordTheLiveActivity.class));
            spec8.setIndicator("RECORDTHELIVE");

            tabHost.addTab(spec1);
            tabHost.addTab(spec2);
            tabHost.addTab(spec3);
            tabHost.addTab(spec4);
            tabHost.addTab(spec5);
            tabHost.addTab(spec6);
            tabHost.addTab(spec7);
            tabHost.addTab(spec8);
        }
    };

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if (isChecked)
        {
            switch (buttonView.getId())
            {
                case R.id.radio_button0:
                    if(tabHost!=null) {
                        dissmissPopupWindows();
                        tabHost.setCurrentTabByTag("MAIN");
                        //首頁選中時圖片換成選中圖片,用戶中心、頻道圖片設置未選中
                        setImg(R.drawable.t_home_sel,mR1);
                        setImg(R.drawable.t_home_user_nor, mR3);
                        setImg(R.drawable.t_home_channel_nor, mR2);
                    }
                    break;
                case R.id.radio_button1:
                    if(isvisible){
                        dissmissPopupWindows();
                    }
                    break;
                case R.id.radio_button2:
                    if(tabHost!=null){
                        dissmissPopupWindows();
                        tabHost.setCurrentTabByTag("USER");
                        setImg(R.drawable.t_home_nor, mR1);
                        setImg(R.drawable.t_home_user_sel, mR3);
                        setImg(R.drawable.t_home_channel_nor, mR2);
                    }
                    break;
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        dissmissPopupWindows();
        return super.onTouchEvent(event);
    }
}








import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zuox on 2016/3/10.
 */
public class HomeChannelAdapter extends BaseRecyclerAdapter<HomeChannel> {


    public HomeChannelAdapter(Context context) {
        super(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.home_channel_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        final HomeChannel item = mItems.get(position);
        viewHolder.ivImg.setImageResource(item.getImg());
        viewHolder.tvTitle.setText(item.getName());
        viewHolder.item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemOnClick(position + 2);
            }
        });

    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.title)
        TextView tvTitle;
        @Bind(R.id.img)
        ImageView ivImg;
        @Bind(R.id.item_layout)
        LinearLayout item_layout;

        public ItemViewHolder(View item_view) {
            super(item_view);
            ButterKnife.bind(this, item_view);
        }
    }
    private ItemListener listener;

    public void setListener(ItemListener listener) {
        this.listener = listener;
    }

    public interface ItemListener{
        void onItemOnClick(int position);
    }
}




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/px_221"
    android:id="@+id/item_layout"
    android:background="@drawable/list_selector"
    android:gravity="center"
    >
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/title"
        android:layout_marginLeft="@dimen/px_20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="@dimen/sp_48"
        />


</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_channel"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="222dp"
        android:layout_marginBottom="48dp"
        android:visibility="visible"
        />

    </LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <!-- tabhost -->

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            ></FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            >

        </TabWidget>
        <RadioGroup
            android:id="@+id/main_radio"
            android:layout_width="fill_parent"
            android:layout_height="53.55dp"
            android:layout_gravity="bottom"
            android:background="@color/white"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/radio_button0"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                android:button="@null"
                android:checked="true"
                android:layout_centerHorizontal="true"
                android:layout_weight="1"
                android:text=""
                android:textSize="1sp"
                android:drawableTop="@drawable/t_home_tab_selector"
                android:layout_gravity="center" />

            <RadioButton
                android:id="@+id/radio_button1"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                android:layout_weight="1"
                android:layout_centerHorizontal="true"
                android:button="@null"
                android:layout_gravity="center"
                android:text=""
                android:textSize="1sp"
                android:drawableTop="@drawable/t_home_channel_tab_selector" />

            <RadioButton
                android:id="@+id/radio_button2"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                android:layout_weight="1"
                android:layout_centerHorizontal="true"
                android:text=""
                android:button="@null"
                android:textSize="1sp"
                android:layout_gravity="center"
                android:drawableTop="@drawable/t_home_user_tab_selector" />

        </RadioGroup>
    </TabHost>

</RelativeLayout>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章