Android - ViewPager - 更改第二个选项卡时,在第一个选项卡上更新UI(滑动检测)

时间:2017-01-29 07:57:06

标签: android

以下是MainActivity的代码:

import android.app.ActionBar;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements TabListener {
    private ViewPager viewPager;
    private TabPagerAdapter tabPagerAdapter;
    private ActionBar actionBar;
    private String[] tabNames = { "Main", "Settings", "Rules" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabPagerAdapter);
        actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (int i = 0; i < 3; i++) {
            actionBar.addTab(actionBar.newTab().setText(tabNames[i])
                    .setTabListener(this));
        }
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int postion) {
                actionBar.setSelectedNavigationItem(postion);
                if(postion==1){
                    View view = viewPager.getChildAt(postion);
                    set_lang(view);
                }
                if(postion==0){
                    View view = viewPager.getChildAt(postion);
                    set_flag(view);
                }


            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

    }

    public void set_lang(View view){
        MainFrame.preferences = getSharedPreferences("flag", Context.MODE_PRIVATE);
        MainFrame.editor = MainFrame.preferences.edit();

        RadioGroup radio = (RadioGroup)view.findViewById(R.id.radioGroup);
        int radioButtonID = radio.getCheckedRadioButtonId();
        RadioButton radioBut = (RadioButton)view.findViewById(radioButtonID);
        String lang = (String) radioBut.getTag();

        MainFrame.editor.putString("lang", lang);
        MainFrame.editor.apply();
    }

    public View set_flag(View view) {
        Button btn_flag = (Button)view.findViewById(R.id.btn_flag);
        MainFrame.preferences = getSharedPreferences("flag", Context.MODE_PRIVATE);
        String lang = MainFrame.preferences.getString("lang","");
        if(lang.equals("english")){
            btn_flag.setBackgroundResource(R.drawable.eng_flag);
        }
        if(lang.equals("russian")){
            btn_flag.setBackgroundResource(R.drawable.rus_flag);
        }
        if(lang.equals("german")){
            btn_flag.setBackgroundResource(R.drawable.german_flag);
        }

        return view;
    }
}

以下是TabPageAdapter的代码:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabPagerAdapter extends FragmentPagerAdapter {

    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        switch (arg0) {
            case 0:
                return new MainFrame();
            case 1:
                return new Settings();
            case 2:
                return new Rules();
            default:
                break;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

我知道,我必须更改onPageSelected(int postion)方法以捕获页面更改,但它不起作用。目前它没有什么改变,当我从第三个选项卡切换到第二个选项时 - 应用程序崩溃。enter image description here 为了更好地理解:我想在第一个选项卡上更改按钮背景,当用户在第二个选项卡上切换语言时,使用单选按钮(radiogroup)。 见截图。

2 个答案:

答案 0 :(得分:1)

我们可以使用Fragment的public void setUserVisibleHint(boolean isVisibleToUser)方法实现这一点。

当用户更改设置片段存储中的任何内容时,在Preference中以及当用户切换到Main Fragment时,在检查isVisibleToUser时读取 setUserVisibleHint 方法中新选择的值的值,然后从Preference中读取值并使用更新的值更改背景。

答案 1 :(得分:1)

@Nagesh Jatagond,谢谢你的建议,我最终找到了解决方案!

Settings.java:

@Override
    public void setUserVisibleHint(boolean visible)
    {
        super.setUserVisibleHint(visible);
        View v = getView();
        if (!visible && v!=null){
            set_lang(v);
        }
    }

Main.java:

@Override
    public void setUserVisibleHint(boolean visible)
    {
        super.setUserVisibleHint(visible);
        View v = getView();
        if (!visible && v!=null){
            set_flag(v);
        }
        if (visible && v!=null){
            set_flag(v);
        }
    }