Fragment中的RecyclerView有时在BottomNavigationView项目触摸时为空

时间:2017-03-25 19:11:35

标签: java android android-fragments android-recyclerview bottomnavigationview

RecyclerView Fragment FrameLayout中的BottomNavigationView有时在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; } } 项触摸时为空(有时你可以触摸5x并且它不是空的但是它再次为空,直到下次触摸)。

YouTuberFragment

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();
    }
}

RVAdapter

Dim  result = File.ReadLines("C:\Users\f1r1a\Desktop\memes.txt").
                   Where(Function(x) Not x.Contains("fraser"))
File.WriteAllLines("C:\Users\f1r1a\Desktop\memes.txt", result.ToArray)

1 个答案:

答案 0 :(得分:2)

当您在主线程上运行适配器初始化并且在检索数据之前呈现数据时,可能是您正在运行不同的线程来收集数据。您是否尝试过旋转设备?