我想在每个Activity中添加一个自定义视图(包含带按钮和文本视图的布局)我的自定义视图名称是NetworkErrorView,我有另一个类可以帮助我观察网络状态变化!
我希望在我为此构建NetworkErrorView的每个活动中的其他视图上显示我的自定义视图
private NetworkErrorView networkErrorView=new NetworkErrorView(this).build();
当网络统计数据发生变化时,我想将可见性从Gone更改为Visible(当网络状态发生变化时调用onChange):
@Override
public void onChange(boolean isConnected) {
networkErrorView.networkErorrDialog(isConnected);
}
我的onChange()
(方法正常工作,但当我改变可见度时,我无法看到自定义视图!任何人都可以帮助我吗?
NetworkErrorView :
public class NetworkErrorView {
private ViewGroup mRootView;
private Activity mActivity;
private LinearLayoutManager mLayoutManager;
private View view;
private Animation translationIn;
private Button btnNetwork;
private LinearLayout networkContainer;
private boolean networkStatus;
public NetworkErrorView(@NonNull Activity activity) {
this.mRootView = (ViewGroup) activity.findViewById(android.R.id.content);
this.mActivity = activity;
this.mLayoutManager = new LinearLayoutManager(mActivity);
}
public NetworkErrorView build(){
view= Assist.inflater.inflate(R.layout.dialog_network_error,mRootView,false);
//View.inflate(mActivity, R.layout.dialog_network_error, mRootView);
btnNetwork=(Button) view.findViewById(R.id.btn_error_network);
btnNetwork.setOnClickListener(onClickListener);
networkContainer=(LinearLayout) view.findViewById(R.id.layout_error_networkcontainer);
translationIn= AnimationUtils.loadAnimation(mActivity,R.anim.anim_wifi_container_in);
mRootView.addView(view, 1);
return this;
}
public void networkErorrDialog(boolean isConnected){
networkStatus=isConnected;
if(isConnected){
view.setVisibility(View.GONE);
}else {
view.setVisibility(View.VISIBLE);
view.bringToFront();
translationIn= AnimationUtils.loadAnimation(mActivity, R.anim.anim_wifi_container_in);
networkContainer.setAnimation(translationIn);
}
}
View.OnClickListener onClickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!networkStatus){
mActivity.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
}
};
}
mActivity :
public class mActivity extends AppCompatActivity implements NetworkObserver {
private NetworkErrorView networkErrorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
FontManager.instance().setTypeface(getWindow().getDecorView());
addView();
NetworkManager.init(this);
//...
}
private void addView(){
//...
networkErrorView=new NetworkErrorView(this).build();
}
@Override
public void onChange(boolean isConnected) {
networkErrorView.networkErorrDialog(isConnected);
}
}
答案 0 :(得分:0)
我认为视图正在添加,但是因为你在第1个位置添加了视图,所以没有显示,所以尝试这样的事情
public NetworkErrorView build(){
view= Assist.inflater.inflate(R.layout.dialog_network_error,mRootView,false);
//View.inflate(mActivity, R.layout.dialog_network_error, mRootView);
btnNetwork=(Button) view.findViewById(R.id.btn_error_network);
btnNetwork.setOnClickListener(onClickListener);
networkContainer=(LinearLayout) view.findViewById(R.id.layout_error_networkcontainer);
translationIn= AnimationUtils.loadAnimation(mActivity,R.anim.anim_wifi_container_in);
mRootView.addView(view, 0); //Change this form 1 to 0
return this;
}