从活动中更改片段中的textView的简单示例

时间:2015-04-19 08:23:21

标签: android android-fragments

任何人都可以为我编写一个简单的例子,关于如何从Activity中更改Fragment中的TextView内容(例如MainActivity.java)?

我已经多次尝试过,我的应用每次都会崩溃 我尝试使用我创建的方法 通过发布我的错误代码使问题复杂化是没有意义的。

2 个答案:

答案 0 :(得分:0)

您可以使用接口在活动和片段

之间进行通信

此界面将通过包中的数据通知您的片段更新

public interface UpdateFragmentInterface {
    public void update(Bundle data);
}

此接口用于附加片段时要通知的活动

public interface OnUpdatableFragmentAttached {

    public void onAttach(UpdateFragmentInterface iFace);
    public void onDetach(UpdateFragmentInterface iFace);

}

一些虚拟活动

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import java.util.ArrayList;


public class UpdatableActivity extends FragmentActivity implements OnUpdatableFragmentAttached {

    private ArrayList<UpdateFragmentInterface> listOfUpdatableFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Do other stuff
        listOfUpdatableFragments = new ArrayList<>();
    }

    private void notifyFragments(){
        String someString = "asda";
        String someKey = "key1234";
        Bundle b = new Bundle();
        b.putString(someKey, someString);

        for(UpdateFragmentInterface fragment : listOfUpdatableFragments){
            fragment.update(b);
        }
    }

    @Override
    public void onAttach(UpdateFragmentInterface iFace) {
        listOfUpdatableFragments.add(iFace);
    }

    @Override
    public void onDetach(UpdateFragmentInterface iFace) {
        listOfUpdatableFragments.remove(iFace);
    }

}

还有一些虚拟片段

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;

    public class UpdatableFragment extends Fragment implements UpdateFragmentInterface {

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            if(activity instanceof OnUpdatableFragmentAttached){
                ((OnUpdatableFragmentAttached) activity).onAttach(this);
            }
        }

        @Override
        public void onDetach() {
            super.onDetach();
            if(getActivity() instanceof OnUpdatableFragmentAttached){
                ((OnUpdatableFragmentAttached) getActivity()).onDetach(this);
            }
        }

        @Override
        public void update(Bundle data) {
            // Do something with the data
            String result = data.getString("key1234");
        }
    }

答案 1 :(得分:0)

我只使用像Otto这样的事件总线。

public enum SingletonBus {
    INSTANCE;

    private Bus bus;

    private SingletonBus() {
        this.bus = new Bus(ThreadEnforcer.ANY);
    }

    public Bus getBus() {
        return bus;
    }
}

然后

public class MainActivity extends ActionBarActivity {
     @Override
     public void onCreate(Bundle saveInstanceState) {
          super.onCreate(saveInstanceState);
          //do whatever
     }

     @Override
     public void onResume() {
          super.onResume();
          SingletonBus.INSTANCE.getBus().register(this);
     }

     @Override
     public void onPause() {
          SingletonBus.INSTANCE.getBus().unregister(this);
          super.onPause();
     }

     @Override
     public void onPostResume() { //using this to send event only for sake of example
         SingletonBus.INSTANCE.getBus().post(new TextViewChangeEvent("hello world!"));
     }
}

public class HomeFragment extends Fragment {
     @InjectView(R.id.your_text_view)
     public TextView yourTextView;


     @Override
     public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
          View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
          ButterKnife.inject(this, view);
          //do whatever
          return view;
     }

     @Override
     public void onResume() {
          super.onResume();
          SingletonBus.INSTANCE.getBus().register(this);
     }

     @Override
     public void onPause() {
          SingletonBus.INSTANCE.getBus().unregister(this);
          super.onPause();
     }

     @Subscribe
     public void onTextViewChangeEvent(TextViewChangeEvent e) {
          this.yourTextView.setText(e.getText());
     }
}

public class TextViewChangeEvent {
     private String text;

     public TextViewChangeEvent(String text) {
          this.text = text;
     }

     public String getText() {
          return text;
     }
}