如何更新所有片段? onProgressUpdate不更新所有片段

时间:2016-08-14 12:19:05

标签: java android android-fragments android-asynctask

我基本上在AsyncTask内更新了值,并使用onPostExecute将其发送到片段。 所有片段应在TextView上显示相同的值。

我的问题是我的应用程序有3个片段页面,只有中间页面(Fragment_B)正在更新......

(如果我将Fragment_B更改为Fragment_A (在下面的代码行中),则只会更新Fragment_A)。

public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new Fragmento_B(); 
            case 1:
                return new Fragmento_A(); //now only Fragment_A updates
            case 2:
                return new Fragmento_C();
            default:
                return null;
        }

    }

为什么所有片段不会同时更新?该值永远不会显示在Fragment_A和Fragment_C上。

OnPostUpdate应该更新所有片段,但它只更新Fragment_B。但我试图调试这个问题,我创建了一个onPreUpdate和我的SetText,它适用于每个片段。我不知道为什么会这样。有人可以帮帮我吗?

public class Cliente extends AsyncTask<Void, Void, Void> {

String dstAddress;
int dstPort;
String[] valores = new String[2];
TextView textResponse,textResponse2;

public Cliente(String addr, int port, TextView textResponse) {
    dstAddress = addr;
    dstPort = port;
    this.textResponse = textResponse;
   // this.textResponse2 = textResponse2;

}

public Cliente(TextView textResponse) {
    this.textResponse = textResponse;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
     textResponse.setText("HELLO"); 
}

@Override
protected Void doInBackground(Void... arg0) {


        Socket socket = null;

        try {
            socket = new Socket(dstAddress, dstPort);

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                    1024);
            byte[] buffer = new byte[1024];

            int bytesRead;
            Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));

     /*
      * notice: inputStream.read() will block if no data return
      */
            valores[0] = r.nextLine();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    return null;

}


@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    textResponse.setText(":D");
}

片段A:

        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_second,container,false);


    x_atual = (TextView) v.findViewById(R.id.x_atual);
    y_atual = (TextView) v.findViewById(R.id.y_atual);
    x_desejado = (TextView) v.findViewById(R.id.x_desej);
    y_desejado = (TextView) v.findViewById(R.id.y_desej);
    ola = (TextView) v.findViewById(R.id.textView12);

    new Cliente("192.168.2.5",6000,ola).execute();


    return v;
}

片段B:

public class Fragmento_B extends android.support.v4.app.Fragment{

public TextView x_atual,y_atual,x_desejado,y_desejado,ola2,ola;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_third,container,false);

    ola2 = (TextView) v.findViewById(R.id.textView2);

    new Cliente("192.168.2.5",6000,ola2).execute();

    return v;
}

基本上onPostUpdate只会使片段_B上出现“:D”,并且onPreUpdate效果很好,并且两者都显示为“Hello”。

FRAGMENTADAPTER ACTIVITY

public class Main2Activity extends FragmentActivity {

private TabLayout mTabLayout;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mViewPager = (ViewPager) findViewById(R.id.view_pager);

    mViewPager.setAdapter(new FragmentoAdapter(getSupportFragmentManager(),getResources().getStringArray(R.array.tiles_tab)));

    mTabLayout.setupWithViewPager(mViewPager);

}

FRAGMENTADAPTER CLASS

    public class FragmentoAdapter extends FragmentPagerAdapter {

    private String[] mTabTiles;

    public FragmentoAdapter(FragmentManager fm,String[] mTabTiles) {
        super(fm);
        this.mTabTiles = mTabTiles;
    }



    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new Fragmento_A();
            case 1:
                return new Fragmento_B();
            case 2:
                return new Fragmento_C();
            default:
                return null;
        }

    }


    @Override
    public int getCount() {
        return this.mTabTiles.length;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return this.mTabTiles[position];
    }
}

MY APPLICATION

1 个答案:

答案 0 :(得分:0)

为什么你在单独的片段中制作相同的逻辑? 您应该创建DataManager来获取和存储数据(阅读有关MVP / MVC模式的更多信息)。此外,您不应该将视图作为arg传递。如果要使用Asynctask,请在Activity中将其用作内部类,然后获取此TextView字段并在onPostExecute()

中设置