我有一个活动,里面有5-6个片段,我试图在片段从REST api加载数据时显示进度条。我能够显示进度条但无法停止它的可见性。每当我试图阻止它的可见性时,Progress Bar根本不会出现。这是我的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");
}
});
}
}
请告诉我应该放置代码以在片段加载后停止进度条的可见性。
答案 0 :(得分:1)
停止进度条的主要活动方法
public void stopProgressBar(){
if (progresslayout.getVisibility() == View.VISIBLE)
{
progresslayout.setVisibility(View.GONE);
}
}
获取数据后在片段中调用上面的方法 例如:在onattach上得到如下面的主要活动背景
@Override
public void onAttach(Context context) {
super.onAttach(context);
mainActivity = (MainActivity) context;
this.context = context;
}
在片段
中调用stop progressbar方法mainActivity.stopProgressBar();
答案 1 :(得分:0)
使用
linearLayout.setVisibility(LinearLayout.GONE);
答案 2 :(得分:0)
在您的HomeFragment中,当您致电
时 loadJSON()
在回复或失败时,您可以通过隐藏进度条的方式调用活动的stopBar()
方法
将stopBar()
方法公开,以便可以从Activity
public void stopBar() {
linearLayout.setVisibility(View.GONE);
}
在HomeFragment的onCreate中,您可以像下面一样参考您的活动,然后您可以调用stopBar()方法
MainActivity activity = ((MainActivity)getActivity());
activity.stopBar();
它看起来像这样,
private void loadJSON() {
Retrofit retrofit = new Retrofit.Builder()
//
// here goes your retrofit code
//
Call<HomeResponse> call = request.getHomeJSON();
call.enqueue(new Callback<HomeResponse>() {
@Override
public void onResponse(Call<HomeResponse> call, Response<HomeResponse> response) {
//
// here goes your code
//
activity.stopBar();
}
@Override
public void onFailure(Call<HomeResponse> call, Throwable t) {
//
// here goes your code
//
activity.stopBar();
Error_Dialog alert = new Error_Dialog();
alert.showDialog(getActivity()," Please Check Your Internet Connection");
}
});
}