我有一个带有ProgressBar和TextView的自定义ViewGroup(RelativeLayout)。 我想显示进度条,然后隐藏并显示内容中的视图。
CustomRelative
公共类RelativeProgressNew扩展了RelativeLayout {
ProgressBar progressBar;
TextView txtEmptyData;
Activity activity;
LayoutInflater mInflater;
View v;
public RelativeProgressNew(Context context, String text) {
super(context);
mInflater = LayoutInflater.from(context);
init();
}
public RelativeProgressNew(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mInflater = LayoutInflater.from(context);
init();
}
public RelativeProgressNew(Context context, AttributeSet attrs) {
super(context, attrs);
mInflater = LayoutInflater.from(context);
init();
}
private void init() {
v = mInflater.inflate(R.layout.layout_custom, this, true);
txtEmptyData = v.findViewById(R.id.txtEmptyData);
progressBar = v.findViewById(R.id.progressBar);
}
private void finish(String text) {
v = mInflater.inflate(R.layout.layout_custom, this, true);
txtEmptyData = v.findViewById(R.id.txtEmptyData);
progressBar = v.findViewById(R.id.progressBar);
progressBar.setVisibility(GONE);
txtEmptyData.setVisibility(VISIBLE);
txtEmptyData.setText(text);
}
public void finishValidData() {
progressBar.setVisibility(GONE);
txtEmptyData.setVisibility(GONE);
}
自定义布局
<FrameLayout android:layout_marginTop="?attr/actionBarSize"
android:id="@+id/linearProgress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible"
xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:id="@+id/progressBar"
android:layout_gravity="center|center_vertical|center_horizontal"
android:layout_width="48dp"
android:layout_height="48dp" />
<TextView
android:id="@+id/txtEmptyData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center"
android:gravity="center"
android:paddingEnd="20dp"
android:paddingStart="20dp"
android:text="EJEMPLO"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible" />
</FrameLayout>
在我的片段布局中,我有这个:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.yustapps.agenda_novecientos.arq.RelativeProgressNew
android:id="@+id/relativeProgress"
android:layout_width="match_parent"
android:background="@color/window_background"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:text="EXAMPLE"
android:layout_height="match_parent" />
</com.yustapps.agenda_novecientos.arq.RelativeProgressNew>
</RelativeLayout>
片段
public class Inicio extends Fragment implements ConstantsInterface {
View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_inicio, container, false);
relativeProgress = new RelativeProgressNew(getActivity(), ConstantsInterface.EMPTY);
simulateLoading();
return v;
}
private void simulateLoading() {
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3300);
} catch (InterruptedException e) {
//Log.e("MainActivity", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void param) {
relativeProgress.finish();
}
}.execute();
}
}
那我该怎么办?当我在片段中显示视图时,可以正常工作,但是随后我想隐藏progressBar并且片段不会更新。
有人可以帮助我吗?
答案 0 :(得分:0)
我找到了您的问题。您正在RelativeProgressNew
中创建一个新的onCreateView
实例。但是,您应该做的是:(RelativeProgressNew)v.findViewById(R.id.relativeProgress)
。
所以您有:relativeProgress = (RelativeProgressNew)v.findViewById(R.id.relativeProgress);
在RelativeProgressNew
函数中的finish(String text)
中,也不要重新添加视图。这应该只发生一次,并且您已经在init()
函数中完成了此操作(这与您的问题无关,但可以改善代码)