为了账号安全,请及时绑定邮箱和手机立即绑定

button不能重复点击

关键代码:

public class FragmentActivity extends AppCompatActivity{

    private static final String TAG = "FragmentActivity";

    private ImageButton btn_weixin;
    private ImageButton btn_friend;
    private ImageButton btn_address;
    private ImageButton btn_settings;

    private LinearLayout lay_weixin;
    private LinearLayout lay_friend;
    private LinearLayout lay_address;
    private LinearLayout lay_settings;

    private Fragment weixinFragment;
    private Fragment friendFragment;
    private Fragment addressFragment;
    private Fragment settingsFragment;
    private FragmentManager fragmentManager;


    private static final int WEIXIN_POSITION = 0;
    private static final int FRIEND_POSITION = 1;
    private static final int ADDRESS_POSITION = 2;
    private static final int SETTINGS_POSITION = 3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment1_tab);
        initData();
        initView();
        setSelectFragment(WEIXIN_POSITION);//设置页面默认选中的Fragment
    }

    //初始化Fragment
    private void initData() {
        weixinFragment = new WeixinFragment();
        friendFragment = new FriendFragment();
        addressFragment = new AddressFragment();
        settingsFragment = new SettingsFragment();
        fragmentManager = getSupportFragmentManager();
    }

    private void initView() {
        lay_weixin = (LinearLayout) findViewById(R.id.lay_weixin);
        lay_friend = (LinearLayout) findViewById(R.id.lay_friend);
        lay_address = (LinearLayout) findViewById(R.id.lay_address);
        lay_settings = (LinearLayout) findViewById(R.id.lay_settings);

        btn_weixin = (ImageButton) findViewById(R.id.btn_weixin);
        btn_friend = (ImageButton) findViewById(R.id.btn_friend);
        btn_address = (ImageButton) findViewById(R.id.btn_address);
        btn_settings = (ImageButton) findViewById(R.id.btn_settings);

    }

    //设置我们的tab所选择的对应的Fragment
    private void setSelectFragment(int position){
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        resetImg();
        removeAllFragment(fragmentTransaction);
        switch (position){
            case WEIXIN_POSITION:
                fragmentTransaction.add(R.id.content,weixinFragment);
                btn_weixin.setImageResource(R.mipmap.tab_weixin_pressed);
                break;
            case FRIEND_POSITION:
                fragmentTransaction.add(R.id.content,friendFragment);
                btn_friend.setImageResource(R.mipmap.tab_find_frd_pressed);
                break;
            case ADDRESS_POSITION:
                fragmentTransaction.add(R.id.content,addressFragment);
                btn_address.setImageResource(R.mipmap.tab_address_pressed);
                break;
            case SETTINGS_POSITION:
                fragmentTransaction.add(R.id.content,settingsFragment);
                btn_settings.setImageResource(R.mipmap.tab_settings_pressed);
                break;
        }
        fragmentTransaction.commit();
    }

    /**
     * 取消所有fragment的显示
     */
    private void removeAllFragment(FragmentTransaction fragmentTransaction){
        fragmentTransaction.remove(weixinFragment);
        fragmentTransaction.remove(friendFragment);
        fragmentTransaction.remove(addressFragment);
        fragmentTransaction.remove(settingsFragment);
    }

    public void doOnClick(View view){
        switch (view.getId()){
            case R.id.lay_weixin:
                setSelectFragment(WEIXIN_POSITION);
                break;
            case R.id.lay_friend:
                setSelectFragment(FRIEND_POSITION);
                break;
            case R.id.lay_address:
                setSelectFragment(ADDRESS_POSITION);
                break;
            case R.id.lay_settings:
                setSelectFragment(SETTINGS_POSITION);
                break;
        }
    }

    /**
     * 将ImageButton的图片全部置为normal
     */
    private void resetImg() {
        btn_weixin.setImageResource(R.mipmap.tab_weixin_normal);
        btn_friend.setImageResource(R.mipmap.tab_find_frd_normal);
        btn_address.setImageResource(R.mipmap.tab_address_normal);
        btn_settings.setImageResource(R.mipmap.tab_settings_normal);
    }

}

请问我这样写问题在哪里?我的tab,如果重复的点击自己,Fragment就加载不出来了

正在回答

举报

0/150
提交
取消
多种多样的App主界面Tab实现方法
  • 参与学习       48925    人
  • 解答问题       179    个

本课程就将讲解迄今为止最流行的4种实现Tab的方法

进入课程

button不能重复点击

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信