Updating a textView/s in a fragment from an Activity (using ViewPager)

若如初见. 提交于 2019-12-25 08:57:25

问题


I'm constantly receiving data in MainActivity and I need it to only be written a textView when the fragment that contains the textView is inflated. Obviously the app crashes if I try to write to a textView that is in a fragment currently not in display. I have made a dummy activity to explain my problem and I'm using a countdown timer to represent the data that is continually changing in the MainActivity. The issue resides in MainActivity.

Would someone mind explaining how I can only write to the textView when it is inflated?

Thanks

FragmentA.java (Fragment B and C are almost identical)

package com.felhr.scrolltabs;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Luke on 20/04/2015.
 */
public class FragmentA extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }
}

MainActivity.java

package com.felhr.scrolltabs;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends FragmentActivity {

    ViewPager viewPager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager)findViewById(R.id.pager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));

        final TextView timer = (TextView)findViewById(R.id.textViewTimer);

        // Countdown timer code -> to be run constantly but only displayed in Fragment A is in focus
        // This section breaks the code
        new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {
                timer.setText("seconds remaining: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                timer.setText("done!");
            }
        }.start();
    }
}

class MyAdapter extends FragmentPagerAdapter
{
    public MyAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;
        Log.d("SWIPE","get Item is called"+i);
        if (i==0)
        {
            fragment=new FragmentA();
        }
        if (i==1)
        {
            fragment=new FragmentB();
        }
        if (i==2)
        {
            fragment=new FragmentC();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        Log.d("SWIPE","get count is called");
        return 3;
    }

    // talks to title to give the page
    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0) {
            return "Tab 1";
        }
        if (position == 1) {
            return "Tab 2";
        }
        if (position == 2) {
            return "Tab 3";
        }
        return null;
    }
}

fragment_a.xml (fragments b and c are almost identical)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C3f3f3">

    <TextView
        android:id="@+id/textViewTimer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Fragment A"
        android:textColor="#000000"
        android:gravity="center"/>

</RelativeLayout>

activity_main.xml

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pager">
    <android.support.v4.view.PagerTitleStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:background="#33B5E5">

        </android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>

来源:https://stackoverflow.com/questions/29743862/updating-a-textview-s-in-a-fragment-from-an-activity-using-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!