发送消息
compile 'io.reactivex:rxjava:1.1.8'
compile 'io.reactivex:rxandroid:1.2.1'
CeshiActivity这是点击到多级链接的按钮
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.cardvlaue.sys.R;
import com.cardvlaue.sys.rxbus.RxBus;
import com.cardvlaue.sys.shopadd.BusIndustrySelect;
import rx.Subscriber;
import timber.log.Timber;
public class CeshiActivity extends AppCompatActivity {
//接收消息
private Subscriber mMoneySubscriber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ceshi);
findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(CeshiActivity.this,CeshiTwoActivity.class));
}
});
mMoneySubscriber = new Subscriber() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Object o) {
if (o instanceof BusIndustrySelect) {
BusIndustrySelect busIndustrySelect = (BusIndustrySelect) o;
if (CeshiTwoActivity.BUS_INDUSTRY_SELECT.equals(busIndustrySelect.getBus())) {
Timber.e(busIndustrySelect.getTitle()+"======接收到的消息============");
((TextView)findViewById(R.id.tv)).setText(busIndustrySelect.getTitle());
}
}
}
};
RxBus.getDefaultBus().toObserverable().subscribe(mMoneySubscriber);
}
}
对应的布局
多级也面
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cardvlaue.sys.CVApplication;
import com.cardvlaue.sys.R;
import com.cardvlaue.sys.data.IndustrySelectResponse;
import com.cardvlaue.sys.data.LoginResponse;
import com.cardvlaue.sys.data.source.TasksDataSource;
import com.cardvlaue.sys.rxbus.RxBus;
import com.cardvlaue.sys.shopadd.BusIndustrySelect;
import com.cardvlaue.sys.shopadd.IndustrySelectAdapter;
import com.cardvlaue.sys.util.ToastUtil;
import com.cardvlaue.sys.view.OnItemClickListener;
import java.util.List;
import timber.log.Timber;
public class CeshiTwoActivity extends AppCompatActivity {
public static final String BUS_INDUSTRY_SELECT = "IndustrySelectActivity_BUS_INDUSTRY_SELECT";
private TasksDataSource mTasksRepository;
private IndustrySelectAdapter mSelectAdapter;
private String objectId, accessToken;
private List mGidList, mPidList, mFinalList;
private ImageButton mBackView;
private TextView mNameView;
private int step;
private String[][] gid = new String[2][2], pid = new String[2][2];
private boolean loading;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_industry_select);
mBackView = (ImageButton) findViewById(R.id.ib_title_blue_back);
mNameView = (TextView) findViewById(R.id.tv_title_blue_name);
RecyclerView listsView = (RecyclerView) findViewById(R.id.rv_industry_select);
mSelectAdapter = new IndustrySelectAdapter();
listsView.setLayoutManager(new LinearLayoutManager(this));
listsView.setAdapter(mSelectAdapter);
mTasksRepository = ((CVApplication) getApplication()).getTasksRepositoryComponent().getTasksRepository();
}
@Override
protected void onStart() {
super.onStart();
mNameView.setText(getString(R.string.shop_add_select));
mTasksRepository.getLogin(mTasksRepository.getMobilePhone(), null, null, new TasksDataSource.LoadLoginCallback() {
@Override
public void onLoginLoaded(LoginResponse response) {
queryIndustry(response.getObjectId(), response.getAccessToken(), null);
}
@Override
public void onDataNotAvailable() {
}
});
mSelectAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
if (loading) {
showFailMsg("加载中...");
return;
}
JSONObject jo = new JSONObject();
switch (step) {
case 1:
gid[0][0] = mGidList.get(position).getId();
gid[0][1] = mGidList.get(position).getTitle();
jo.put("industryGId", gid[0][0]);
queryIndustry(objectId, accessToken, jo.toJSONString());
break;
case 2:
pid[1][0] = mPidList.get(position).getId();
pid[1][1] = mPidList.get(position).getTitle();
jo.put("industryPId", pid[1][0]);
jo.put("merchantId", objectId);
queryIndustry(objectId, accessToken, jo.toJSONString());
break;
case 3:
BusIndustrySelect select = new BusIndustrySelect();
select.setBus(BUS_INDUSTRY_SELECT);
select.setId(gid[0][0] + "," + pid[1][0] + "," + mFinalList.get(position).getId());
select.setTitle(gid[0][1] + "," + pid[1][1] + "," + mFinalList.get(position).getTitle());
Timber.e(select+"========发送出去的时候==========");
RxBus.getDefaultBus().send(select);
finish();
break;
}
}
});
mBackView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (step) {
case 3:
mSelectAdapter.updateData(mPidList);
step = 2;
break;
case 2:
mSelectAdapter.updateData(mGidList);
step = 1;
break;
default:
finish();
break;
}
}
});
}
private void showFailMsg(String msg) {
ToastUtil.showFail(this, msg);
}
private void queryIndustry(String objectId, String accessToken, String where) {
loading = true;
this.objectId = objectId;
this.accessToken = accessToken;
mTasksRepository.queryIndustry(objectId, accessToken, where, new TasksDataSource.LoadStringCallBack() {
@Override
public void onSuccess(String msg) {
loading = false;
switch (step) {
case 0:
step = 1;
mGidList = JSON.parseArray(msg, IndustrySelectResponse.class);
mSelectAdapter.updateData(mGidList);
break;
case 1:
step = 2;
mPidList = JSON.parseArray(msg, IndustrySelectResponse.class);
mSelectAdapter.updateData(mPidList);
break;
case 2:
step = 3;
mFinalList = JSON.parseArray(msg, IndustrySelectResponse.class);
mSelectAdapter.updateData(mFinalList);
break;
}
}
@Override
public void onFailure(String msg) {
loading = false;
showFailMsg(msg);
}
});
}
}
对应的布局,一个返回键和一个放数据的view
对应的adapter
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cardvlaue.sys.R;
import com.cardvlaue.sys.data.IndustrySelectResponse;
import com.cardvlaue.sys.view.OnItemClickListener;
import java.util.ArrayList;
import java.util.List;
/**
* Created by cardvalue on 2016/7/21.
*/
public class IndustrySelectAdapter extends RecyclerView.Adapter {
private List mData = new ArrayList<>();
private OnItemClickListener mItemClickListener;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_industry_select, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.text.setText(mData.get(position).getTitle());
holder.group.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(position);
}
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public void updateData(List strs) {
mData.clear();
mData.addAll(strs);
notifyDataSetChanged();
}
public void setOnItemClickListener(OnItemClickListener listener) {
mItemClickListener = listener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout group;
TextView text;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.tv_financing_item_calculate_name);
group = (LinearLayout) itemView.findViewById(R.id.ll_industry_select_group);
}
}
}
IndustrySelectResponse实体类
public class BusIndustrySelect extends IndustryItemResponse {
@Getter @Setter
private String bus;
}