gpt4 book ai didi

android - ProgressBar 不会在代码中到处加载

转载 作者:行者123 更新时间:2023-11-29 00:53:48 25 4
gpt4 key购买 nike

所以我试了一整天都没有成功。

我连接了两个 Fragment,带有 ViewPager TabLayout,

而且我已经尝试将 ProgressBar 放在每个 fragment 上,它不会显示。我还尝试将它放在为两个 fragment 启动 ViewPager 的 MainActivity 上,它也不会显示。

我尝试了新项目,它显示了。我做错了什么?

我已经删除了代码中所有位置的 ProgressBar,因为它不起作用。如果您可以自己查看,请查看代码。

第一个 fragment

public class FilmShow extends Fragment {
private static RecyclerView recyclerView;
private static final String API_KEY = "2e08750083b7e21e96e915011d3f8e2d";
private static final String TAG = FilmShow.class.getSimpleName();

public FilmShow() {
// Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getResponse getResponse = new getResponse();
getResponse.execute();
}

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

recyclerView = view.findViewById(R.id.recycler_tv);

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);

recyclerView.setVisibility(View.GONE);



return view;
}

private class getResponse extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... voids) {
ApiInterface apiInterface = ApiClient.getList().create(ApiInterface.class);

Call<Response> responseCall = apiInterface.getFilmList(API_KEY);
responseCall.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
List<ResultsItem> resultsItemList = response.body().getResults();
recyclerView.setAdapter(new FilmShowRecyclerAdapter(resultsItemList, getContext()));
}

@Override
public void onFailure(Call<Response> call, Throwable t) {
Log.d(TAG, t.toString());
}
});
return null;
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}

另一个 fragment

public class TvShow extends Fragment {
private static RecyclerView recyclerView;
private static final String API_KEY = "2e08750083b7e21e96e915011d3f8e2d";
private static final String TAG = TvShow.class.getSimpleName();

public TvShow() {
// Required empty public constructor
}

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

recyclerView = view.findViewById(R.id.recycler_tv);


Thread thread = new Thread(new Runnable() {
@Override
public void run() {
CallInternet callInternet = new CallInternet();
callInternet.execute();
}
});

thread.run();

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);


return view;
}

private class CallInternet extends AsyncTask<Void, Void, Void> {


@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... voids) {
ApiInterface apiInterface = ApiClient.getList().create(ApiInterface.class);


Call<Response> responseCall = apiInterface.getTvList(API_KEY);
responseCall.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
List<ResultsItem> resultsTv = response.body().getResults();
recyclerView.setAdapter(new TvShowRecyclerAdapter(resultsTv, getContext()));
}

@Override
public void onFailure(Call<Response> call, Throwable t) {
Log.d(TAG, t.toString());
}
});
return null;
}

@Override
protected void onProgressUpdate(Void... values) {
}

@Override
protected void onPostExecute(Void aVoid) {
recyclerView.setVisibility(View.VISIBLE);

}
}

主要 Activity

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;


public MainActivity() {
}

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

Toolbar toolbar = findViewById(R.id.toolbar);

// placing toolbar in place of action bar
setSupportActionBar(toolbar);



/*Membuat tabLayout*/
tabLayout = findViewById(R.id.tablayout);
viewPager = findViewById(R.id.viewpager);

adapter = new ViewPagerAdapter(getSupportFragmentManager());


//adding fragment
adapter.AddFragment(new TvShow(), "Tv");
adapter.AddFragment(new FilmShow(), "Film");

// Adapter Setrup
viewPager.setAdapter(adapter);

tabLayout.setupWithViewPager(viewPager);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_change_settings) {
Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}

编辑:

修改了另一个 fragment 以在 onCreateView 上显示 ProgressBar 并且它可以工作但不会停止加载

...............

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

recyclerView = view.findViewById(R.id.recycler_tv);
progressBar = view.findViewById(R.id.progress_bar);

...............

XML文件

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:padding="16dp"
>



<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="@layout/item_tv"
android:visibility="gone" />


<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyle"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>


</RelativeLayout>

编辑二:

public class TvShow extends Fragment {
private static RecyclerView recyclerView;
private static final String API_KEY = "2e08750083b7e21e96e915011d3f8e2d";
private static final String TAG = TvShow.class.getSimpleName();
private static ProgressBar progressBar;

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

progressBar.findViewById(R.id.progress_bar);

progressBar.setVisibility(VISIBLE);


Thread thread = new Thread(new Runnable() {
@Override
public void run() {
CallInternet callInternet = new CallInternet();
callInternet.execute();
}
});

thread.run();
}

public TvShow() {
// Required empty public constructor
}

java.lang.NullPointerException:尝试在空对象引用上调用虚方法“android.view.View android.widget.ProgressBar.findViewById(int)”

最佳答案

你这样试过吗

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

recyclerView = view.findViewById(R.id.recycler_tv);
progressBar = view.findViewById(R.id.progress_bar);

// progress bar Shows first
progressBar.setVisibility(VISIBLE);

ApiInterface apiInterface = ApiClient.getList().create(ApiInterface.class);

Call<Response> responseCall = apiInterface.getFilmList(API_KEY);
responseCall.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
List<ResultsItem> resultsItemList = response.body().getResults();
recyclerView.setAdapter(new FilmShowRecyclerAdapter(resultsItemList, getContext()));
progressBar.setVisibility(GONE);

}

@Override
public void onFailure(Call<Response> call, Throwable t) {
Log.d(TAG, t.toString());
progressBar.setVisibility(GONE);

}
});

这段代码不需要Asynctask,我只是让它隐藏和显示

关于android - ProgressBar 不会在代码中到处加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56825644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com