今天看了一下EventBus的文章,自己做了一个小demo,运行成功,分享一下;
开始是EventBus的源码下载下来,导入到工程里面,各种报错,鼓捣的时间比较久最后放弃了,找别人要了一个EventBus的jar包,导入工程就可以使用了,使用起来非常简单!
看下运行起来后的效果,不会做gif图片,就直接看运行的图片
现在来分享代码吧
package saiyimap.com.eventbus;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Activity里面啥都没做,就是在主布局里面放入了两个静态的fragment;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/list"
android:name="saiyimap.com.eventbus.FragmentListOfContainer"
android:layout_width="150dp"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/container"
android:name="saiyimap.com.eventbus.FragmentContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
看一下FragmentConatainer
package saiyimap.com.eventbus;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.android.pc.ioc.event.EventBus;
/**
* Created by Administrator on 2015/9/16.
*/
public class FragmentContainer extends Fragment {
private TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_container,null);
mTextView = (TextView)view.findViewById(R.id.textview_container);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
public void onEventMainThread(String item)
{
if (item != null)
mTextView.setText(item);
}
}
这个布局里面就是只有一个TextView,分别在oncreate里面 EventBus.getDefault().register(this);和在onDestroy里面 EventBus.getDefault().unregister(this);,onEventMainThread方法里面更新ui就可以了,
再看FragmentListOfContainer
package saiyimap.com.eventbus;
import android.content.ClipData;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.android.pc.ioc.event.EventBus;
import java.util.ArrayList;
/**
* Created by Administrator on 2015/9/16.
*/
public class FragmentListOfContainer extends Fragment {
private AdapterList mAdapter;
private ArrayList<String> mList;
private ListView mListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list,null);
mListView = (ListView)view.findViewById(R.id.listview);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
init();
initData();
EventBus.getDefault().post(new String(mList.get(0)));
}
private void init() {
mList = new ArrayList<String>();
mAdapter = new AdapterList(getActivity(),mList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
EventBus.getDefault().post(mListView.getItemAtPosition(position));
}
});
}
private void initData(){
Resources resources = getResources();
mList.add(resources.getString(R.string.q));
mList.add(resources.getString(R.string.w));
mList.add(resources.getString(R.string.e));
mList.add(resources.getString(R.string.r));
mList.add(resources.getString(R.string.t));
mList.add(resources.getString(R.string.y));
mList.add(resources.getString(R.string.u));
mList.add(resources.getString(R.string.i));
mList.add(resources.getString(R.string.o));
mList.add(resources.getString(R.string.p));
mList.add(resources.getString(R.string.l));
mAdapter.notifyDataSetChanged();
}
}
就是在onItemClick里面 EventBus.getDefault().post(mListView.getItemAtPosition(position));把信息传过去就可以了
adapter就是只有一个view这里就不贴代码了