java - 如何停止 LinearLayout 内 ProgressBar 的可见性

标签 java android progress-bar

我有一个包含 5-6 个 fragment 的 Activity ,每当 fragment 从 REST api 加载其数据时,我都会尝试显示一个进度条。我能够显示进度条,但无法停止它的可见性。每当我试图阻止它的可见性时,进度条根本不会出现。这是我的 Activity.java

public class MainActivity extends AppCompatActivity {


    private Toolbar   toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    public LinearLayout linearLayout;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ImageView imageTitle = (ImageView) toolbar.findViewById(R.id.appbar_image);
           setSupportActionBar(toolbar);
           getSupportActionBar().setDisplayShowTitleEnabled(false);
           getSupportActionBar().setDisplayHomeAsUpEnabled(false);
           linearLayout = (LinearLayout)findViewById(R.id.linlaHeaderProgress);
           linearLayout.setVisibility(View.VISIBLE);



        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);


       }


private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new HomeFragment(), "Home News");
        adapter.addFragment(new BusinessFragment(),"Business News");
        adapter.addFragment(new PoliticsFragment(),"Politics News");
        adapter.addFragment(new TechnologyFragment(),"Technology News");
        adapter.addFragment(new SportsFragment(),"Sports News");
        adapter.addFragment(new EntertainmentFragment(),"Entertainment News");
        adapter.addFragment(new HealthFragment(),"Health News");
        adapter.addFragment(new WorldFragment(),"World News");
        adapter.addFragment(new MoreFragment(),"More News");


        viewPager.setAdapter(adapter);



}

private void stopBar() {

    linearLayout.setVisibility(View.GONE);
}


class ViewPagerAdapter extends FragmentPagerAdapter {
     private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
    super(manager);
}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
}

@Override
public int getCount() {
    return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);


}

     @Override
    public CharSequence getPageTitle(int position) {
    return mFragmentTitleList.get(position);
   }
 }
}  

这是我的 Activity.xml

<android.support.design.widget.CoordinatorLayout 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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/appbar_image"
                android:foregroundGravity="center"
                android:scaleType="fitCenter"
                android:src="@drawable/splash"


                />



        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"/>
    </android.support.design.widget.AppBarLayout>


    <LinearLayout
        android:id="@+id/linlaHeaderProgress"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/pbHeaderProgress"
            style="@style/Widget.AppCompat.ProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/CircularProgress">
        </ProgressBar>

    </LinearLayout>



    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />



</android.support.design.widget.CoordinatorLayout>

这是我的fragment.java

    public class HomeFragment extends android.support.v4.app.Fragment {

    private RecyclerView recyclerView;
    private ArrayList<HomeNews> data;
    private HomeNewsAdapter adapter;
    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        recyclerView = (RecyclerView)rootView.findViewById(R.id.rv_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);


        loadJSON();


        return rootView;

    }



    private void loadJSON() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.vaonlinenews.com/news_api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<HomeResponse> call = request.getHomeJSON();

        call.enqueue(new Callback<HomeResponse>() {
            @Override
            public void onResponse(Call<HomeResponse> call, Response<HomeResponse> response) {

                HomeResponse homeResponse = response.body();
                data = new ArrayList<>(Arrays.asList(homeResponse.getArticles()));
                adapter = new HomeNewsAdapter(getContext(),data);
                recyclerView.setAdapter(adapter);



            }





            @Override
            public void onFailure(Call<HomeResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());
                Error_Dialog alert = new Error_Dialog();
                alert.showDialog(getActivity()," Please Check Your Internet Connection");

            }
        });

    }




}

请建议我应该在哪里放置代码以在加载 fragment 后停止进度条的可见性。

最佳答案

停止进度条的主要 Activity 方法

public void stopProgressBar(){
if (progresslayout.getVisibility() == View.VISIBLE) 
{
 progresslayout.setVisibility(View.GONE);
}
}

获取数据后在fragment中调用上述方法 例如:在 onattach 中获取 mainactivity 上下文,如下所示

 @Override
 public void onAttach(Context context) {
    super.onAttach(context);

    mainActivity = (MainActivity) context;
    this.context = context;
}

在 fragment 中调用停止进度条方法

mainActivity.stopProgressBar();

关于java - 如何停止 LinearLayout 内 ProgressBar 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44920133/

相关文章:

java - JSF持久性文件问题

java - Java 中的 Fluent Builder/DSL 示例

c++ - 不确定的进度条?

java - AlarmManager 第二次过早触发 PendingIntent

JavaFX:使用 StringProperty 和 ChangeListener 根据 TimerTask 定期更新标签

java - 第一次 Android 程序员无法在 Activity 之间移动

android - 在 styles.xml 中使用自定义属性

java - 在Android项目中包含另一个项目

python - 如何在 Python 中显示和解析 rsync 进度?

html - 样式进度条——计算宽度