My RecyclerView
in Fragment
(FrameLayout
) is sometimes empty on BottomNavigationView
item touch (sometimes you can touch 5x and it's not empty but then it's empty again until the next touch).
YouTuberFragment
public class YouTuberFragment extends Fragment {
private List<YouTuber> youtuber;
private class GetYouTuber extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
try {
InputStream inputStream = getActivity().getAssets().open("youtuber.json");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) total.append(line);
String m = total.toString();
JSONObject jsonObject = new JSONObject(m);
JSONArray jsonYouTuberArray = jsonObject.getJSONArray("youtuber");
int youTubersCount = 0;
if (jsonYouTuberArray != null) youTubersCount = jsonYouTuberArray.length();
for (int i = 0; i < youTubersCount; i++) {
JSONObject jsonYouTuberObject = jsonYouTuberArray.getJSONObject(i);
YouTuber youTuber = new YouTuber(
jsonYouTuberObject.getString("name"),
jsonYouTuberObject.getString("role"),
jsonYouTuberObject.getString("picture"),
jsonYouTuberObject.getString("episodes"),
jsonYouTuberObject.getString("youtube")
);
youtuber.add(youTuber);
}
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
public YouTuberFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_youtuber, container, false);
LinearLayoutManager llm = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
youtuber = new ArrayList<>();
RVAdapter adapter = new RVAdapter(youtuber, getContext());
new YouTuberFragment.GetYouTuber().execute();
RecyclerView rv = (RecyclerView) v.findViewById(R.id.rv);
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
rv.setAdapter(adapter);
return v;
}
}
RVAdapter
class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
private final Context context;
private Resources res;
private List<YouTuber> youTuber;
RVAdapter(List<YouTuber> youTuber, Context context){
this.youTuber = youTuber;
this.context = context;
}
static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
ImageView ytPicture;
TextView ytName;
TextView ytRole;
TextView ytEpisodes;
Button ytYoutube;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
ytPicture = (ImageView) itemView.findViewById(R.id.yt_picture);
ytName = (TextView) itemView.findViewById(R.id.yt_name);
ytRole = (TextView) itemView.findViewById(R.id.yt_role);
ytEpisodes = (TextView) itemView.findViewById(R.id.yt_episodes);
ytYoutube = (Button) itemView.findViewById(R.id.yt_youtube);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_youtuber, viewGroup, false);
res = v.getResources();
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(final PersonViewHolder personViewHolder, final int i) {
personViewHolder.ytPicture.setImageResource(res.getIdentifier(youTuber.get(i).picture, "drawable", BuildConfig.APPLICATION_ID));
personViewHolder.ytName.setText(youTuber.get(i).name);
personViewHolder.ytRole.setText(youTuber.get(i).role);
personViewHolder.ytEpisodes.setText(res.getString(R.string.yt_episodes, youTuber.get(i).episodes));
personViewHolder.ytYoutube.setText(youTuber.get(i).youtube);
personViewHolder.ytYoutube.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri = Uri.parse("https://" + youTuber.get(i).youtube);
Intent youtube_url = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(youtube_url);
}
});
}
@Override
public int getItemCount() {
return youTuber.size();
}
}