【问题标题】:Edit CustomViewGroup (RelativeLayout) after show (Android)显示后编辑 CustomViewGroup (RelativeLayout) (Android)
【发布时间】: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


【解决方案1】:

我发现了你的问题。您正在onCreateView 中创建一个新的RelativeProgressNew 实例。但是,您应该做的是:(RelativeProgressNew)v.findViewById(R.id.relativeProgress)

所以你有:relativeProgress = (RelativeProgressNew)v.findViewById(R.id.relativeProgress);

在你的finish(String text) 函数中的RelativeProgressNew 中也不要重新膨胀你的视图。这应该只发生一次,并且您已经在 init() 函数中执行此操作(这与您的问题无关,但它改进了您的代码)

【讨论】:

  • 我复制并粘贴了您的 RelativeProgressNew 代码,它对我来说很好用。如果您仍然有异常,您可以发布日志吗?
猜你喜欢
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多