【发布时间】:2019-09-19 10:25:16
【问题描述】:
我有一个带有 ProgressBar 和 TextView 的自定义 ViewGroup (RelativeLayout)。 我想显示进度条一段时间,然后在我的内容中隐藏和显示视图。
自定义相对
公共类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();
}
}
那我该怎么办?当我在片段中显示视图时,可以正常工作,但是我想隐藏进度条并且片段不更新。
有人可以帮帮我吗?
【问题讨论】:
-
可以添加片段的java代码吗?
-
完成。感谢您的快速回答。
标签: android android-relativelayout viewgroup