每10秒在viewpager中刷新一个片段 - android

时间:2017-02-25 05:16:39

标签: android android-fragments android-viewpager

我是android viewpager的新手。我开发了一个使用滑动tablayout的应用程序。因此,在每个选项卡上都会显示特定的片段。

在第一个标签上:我想显示用户的手机电池信息

在主要活动中, 我已经注册了一个电池广播接收器,如:

public static BroadcastReceiver batteryInfoReceiver=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int health=intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
        int iconSmall=intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,0);
        level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
        int plugged=intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
        boolean present=intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
        int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
        int status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,0);
        String technology=intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
        temp=intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
        voltage=intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);


    }
};

这是我想要显示电池信息的第一个片段:

public class Tab1 extends Fragment {

//initializing child views
public TextView temp,level;
public ProgressBar batteryProgress;

public  Tab1()
{
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   // return inflater.inflate(R.layout.tab1,container,false);
    View v=inflater.inflate(R.layout.tab1,container,false);
            return v;
}

@Override
public void onViewCreated(View view,Bundle savedInstanceState) {

    level = (TextView) getActivity().findViewById(R.id.level);
    temp=(TextView) getActivity().findViewById(R.id.voltageNtemp);
    batteryProgress=(ProgressBar) getActivity().findViewById(R.id.batteryProgress);

    //setting battery info
    level.setText(String.valueOf(MainActivity.level)+"%");
    temp.setText("V: "+String.valueOf(MainActivity.voltage)+" mV ,  T: "+String.valueOf(MainActivity.temp)+" C");
    batteryProgress.setMax(100);
    batteryProgress.setProgress(MainActivity.level);

}}

这是寻呼机适配器类

public class Pager extends FragmentStatePagerAdapter {

//integer to count number of tabs
int tabCount;
int current;

int voltage, temp, level;

//constructor to the class
public Pager(FragmentManager fm, int tabCount)
{
    super(fm);
    //initializing tab count
    this.tabCount=tabCount;
}

@Override
public Fragment getItem(int position) {
    this.current=position;
    switch (position)
    {
        case 0:
            Tab1 t1=new Tab1();
            return t1;

        case 1:
            Tab2 t2=new Tab2();
            return t2;

        case 2:
            Tab3 t3=new Tab3();
            return t3;

        default:
            return null;
    }
}



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

在主要活动中:

//initializing the tablayout
    tabLayout=(TabLayout)findViewById(R.id.tabLayout);

    //adding tabs using assTab() method
    tabLayout.addTab(tabLayout.newTab().setText("Overview"));//set tab title
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    //initializing viewpager
    viewPager=(ViewPager) findViewById(R.id.pager);

    //creating out pager adapter
    adapter=new Pager(getSupportFragmentManager(),tabLayout.getTabCount());

    //adding adapter to pager
    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    //adding onTabSelectedListener to swipe views
    tabLayout.setOnTabSelectedListener(this);

    this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

所以使用这段代码: 我在应用程序启动时成功获取电池电压,温度和电平。

但是假设,手机状态当前正在充电,用户正在使用此当前片段。意味着,如果片段还活着。

我想每N秒刷新一次片段,因此代码将确定电池电量的变化并动态更新片段数据。

任何人都请帮忙

0 个答案:

没有答案