无法在MainActivity(子类)中查看在BaseActivity(父类)中创建的progressBar?

huangapple go评论94阅读模式
英文:

Cannot view progressBar created in BaseActivity(parent) in MainActivity(child)?

问题

以下是翻译好的部分:

我正在尝试为所有重复的操作创建一个单一的基本活动。我尝试将进度条放在BaseActivity中,并尝试在MainActivity中扩展它,但它根本不显示出来。

activity_base.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/activity_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </FrameLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        android:indeterminate="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

BaseActivity.java

public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";

    public ProgressBar progressBar;

    @Override
    public void setContentView(int layoutResID) {

        ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
        FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content);
        progressBar = constraintLayout.findViewById(R.id.progressBar);
        Log.i(TAG, "setContentView: Progressbar" + progressBar.getVisibility());

        getLayoutInflater().inflate(layoutResID, frameLayout, true);
        super.setContentView(layoutResID);
    }

    public void showProgressBar(boolean visibility) {
        progressBar.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }
}

MainActivity.java

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_get_data = (Button) findViewById(R.id.btn_get_data);

        btn_get_data.setOnClickListener(view -> {

            if (progressBar.getVisibility() == View.VISIBLE) {
                Log.i(TAG, "onCreate: DISABLING PROGRESS BAR");
                showProgressBar(false);
            } else {
                showProgressBar(true);
            }

        });
    }
}

此外,在应用中还有其他内容,但与进度条无关。是的,我已正确声明了按钮和其他内容。

提前感谢!

英文:

I am trying to make a single base activity for all the redundant actions. I tried putting the progress bar in BaseActivity and tried extending it in MainActivity, But it is not showing up at all.
My Code

activity_base.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;FrameLayout
        android:id=&quot;@+id/activity_content&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;


    &lt;/FrameLayout&gt;

    &lt;ProgressBar
        android:id=&quot;@+id/progressBar&quot;
        android:indeterminate=&quot;true&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
        app:layout_constraintRight_toRightOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

BaseActivity.java

public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = &quot;BaseActivity&quot;;

    public ProgressBar progressBar;

    @Override
    public void setContentView(int layoutResID) {

        ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
        FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content);
        progressBar = constraintLayout.findViewById(R.id.progressBar);
        Log.i(TAG, &quot;setContentView: Progressbar&quot; + progressBar.getVisibility());

        getLayoutInflater().inflate(layoutResID, frameLayout, true);
        super.setContentView(layoutResID);
    }

    public void showProgressBar(boolean visibility) {
        progressBar.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }
}

MainActivity.java


public class MainActivity extends BaseActivity {

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_get_data = (Button) findViewById(R.id.btn_get_data);

        btn_get_data.setOnClickListener(view -&gt; {


       if (progressBar.getVisibility() == View.VISIBLE) {
                Log.i(TAG, &quot;onCreate: DISABLING PROGRESS BAR&quot;);
                showProgressBar(false);
            } else {
                showProgressBar(true);
            }

   }


}

There are other things in the app, but they have no connection with the progress bar. And yes I have declared the button and other things properly.

Thanks in advance!

答案1

得分: 3

setContentView()方法中的以下代码:

super.setContentView(layoutResID);

更改为:

super.setContentView(constraintLayout);

BaseActivity中进行这个修改。

英文:

Change

super.setContentView(layoutResID); 

to

super.setContentView(constraintLayout); 

in setContentView() method of the BaseActivity.

huangapple
  • 本文由 发表于 2020年8月4日 15:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63242159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定