活动中的访问片段? [英] Access Fragment in Activity?
本文介绍了活动中的访问片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含 2 个片段的主要活动.两个片段都有一个 ListView
.我想更新 MainActivity
中的列表.有没有办法在活动中访问片段列表适配器,而无需将适配器设为静态适配器.目前我在 Mainactivity.java
I have one main activity with 2 fragments. Both fragments have a ListView
. I want to update the list in MainActivity
. Is there any way to access fragment list adapter in activity with out making adapter as static adapter.
Currently I am doing like this in Mainactivity.java
public void updatelist() {
if(fragmentstate=0)
Fragment1.adapter.notifyDataSetChanged();
else
Fragment2.adapter.notifyDataSetChanged();
}
推荐答案
您可以使用 Otto 事件总线:
You could do the following with Otto event bus:
public class UpdateListEvent {
private int fragmentState;
public UpdateListEvent(int fragmentState) {
this.fragmentState = fragmentState;
}
}
public class MainActivity extends ActionBarActivity {
...
public void updatelist() {
SingletonBus.INSTANCE.getBus().post(new UpdateListEvent(fragmentState));
}
}
public class FragmentA extends Fragment {
@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 onUpdateListEvent(UpdateListEvent e) {
if(e.getFragmentState() == 0) { //is this even necessary?
this.adapter.notifyDataSetChanged();
}
}
}
public class FragmentB extends Fragment {
@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 onUpdateListEvent(UpdateListEvent e) {
if(e.getFragmentState() != 0) { //is this even necessary?
this.adapter.notifyDataSetChanged();
}
}
}
以及单例总线的修订版
public enum SingletonBus {
INSTANCE;
private static String TAG = SingletonBus.class.getSimpleName();
private Bus bus;
private volatile boolean paused;
private final Vector<Object> eventQueueBuffer = new Vector<>();
private Handler handler = new Handler(Looper.getMainLooper());
private SingletonBus() {
this.bus = new Bus(ThreadEnforcer.ANY);
}
public <T> void postToSameThread(final T event) {
bus.post(event);
}
public <T> void postToMainThread(final T event) {
try {
if(paused) {
eventQueueBuffer.add(event);
} else {
handler.post(new Runnable() {
@Override
public void run() {
try {
bus.post(event);
} catch(Exception e) {
Log.e(TAG, "POST TO MAIN THREAD: BUS LEVEL");
throw e;
}
}
});
}
} catch(Exception e) {
Log.e(TAG, "POST TO MAIN THREAD: HANDLER LEVEL");
throw e;
}
}
public <T> void register(T subscriber) {
bus.register(subscriber);
}
public <T> void unregister(T subscriber) {
bus.unregister(subscriber);
}
public boolean isPaused() {
return paused;
}
public void setPaused(boolean paused) {
this.paused = paused;
if(!paused) {
Iterator<Object> eventIterator = eventQueueBuffer.iterator();
while(eventIterator.hasNext()) {
Object event = eventIterator.next();
postToMainThread(event);
eventIterator.remove();
}
}
}
}
这篇关于活动中的访问片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文