BroadcastReceiver可以说是一种系统级的监听,有自己进程,与Activity通过intent来通讯,android中的广播又分为有序和无序两种,有序广播是通过其优先级来改变广播的传递状态,在实际开发中,巧妙的利用广播来实现页面的刷新可以使我们的开发工作变得简单高效,下面通过一个简单的例子来总结ListView是如何与广播结合使用的。
需求:点击接单后,对应订单状态变为已接单
public class MainActivity extends AppCompatActivity {
//广播监听用来更新ui
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("order")){
//获取要改变状态的position
int position = intent.getIntExtra("position",100);
if(position!=100){
data.get(position).setType(1);
//更新ui
myAdp.setData(data);
}
}
}
};
private List<Order> data;
private ListView lv;
private MyAdp myAdp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
//注册广播,用来监听订单变化
IntentFilter intentFilter = new IntentFilter("order");
registerReceiver(receiver,intentFilter);
}
private void initData() {
data = new ArrayList<>();
for(int i=0;i<20;i++){
Order order = new Order();
order.setOrder_name("订单"+i);
data.add(order);
}
myAdp = new MyAdp(this,data);
lv.setAdapter(myAdp);
}
private void initView() {
lv = (ListView) findViewById(R.id.lv);
}
@Override
protected void onDestroy() {
super.onDestroy();
//别忘了反注册,不然会报异常
unregisterReceiver(receiver);
}
}
以下是adapter,为了简介,其中BaseAdapter中的部分方法已经向上抽取了。
/**
* Created by zhoukai on 2016/12/17.
*/
public class MyAdp<T> extends CommonAdp<T> {
private Context context;
private List<T>data;
public MyAdp(Context context, List<T> data) {
super(context, data);
this.data = data;
this.context =context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHold viewHold;
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
viewHold = new ViewHold();
viewHold.tv_get_order = (TextView) convertView.findViewById(R.id.tv_get_order);
viewHold.tv_order = (TextView) convertView.findViewById(R.id.tv_order);
convertView.setTag(viewHold);
}else {
viewHold = (ViewHold) convertView.getTag();
}
Order order = (Order) data.get(position);
viewHold.tv_order.setText(order.getOrder_name());
if(order.getType()==0){
viewHold.tv_get_order.setText("接单");
}else {
viewHold.tv_get_order.setText("已接单");
}
viewHold.tv_get_order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击接单后,发送一条广播,通知activity订单状态已经改变
Intent intent = new Intent();
intent.setAction("order");
intent.putExtra("position",position);
context.sendBroadcast(intent);
}
});
return convertView;
}
static class ViewHold{
TextView tv_order;
TextView tv_get_order;
}
public List<T> getData() {
return data;
}
//更新数据源
public void setData(List<T> data) {
this.data = data;
notifyDataSetChanged();
}
}
主布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iwintrue.brodcastdemo.LvActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:divider="@color/colorAccent"/>
</RelativeLayout>
在activity之间的传值可以用广播来实现,当想要回传的值不是相邻页面时,使用广播非常有效。
源码下载地址