简体   繁体   English

recyclerview项目显示导航抽屉片段中的空白色屏幕

[英]recyclerview item showing showing empty white screen in navigation drawer fragment

I am developing news and I have implemented recyclerview with search view in navigation drawer fragment however recyclerview item in navigation drawer showing empty white screen. 我正在开发新闻,我已经在导航抽屉片段中实现了带有搜索视图的recyclerview,但导航抽屉中的recyclerview项目显示空白屏幕。 I am getting two ending point in fragment class and below my root URL. 我在片段类和我的根URL下面得到两个结束点。

private static final String ROOT_URL = "https://newsapi.org";

below my first ending point where I am getting top headlines. 低于我的第一个结束点,我正在获得头条新闻。

 @GET("/v2/top-headlines?sources=bbc-sport&apiKey=my-api-key")
    Call<SportNews> getArticles();

below second ending point where I am getting everything 低于第二个结束点,我得到了一切

 @GET("/v2/everything?apiKey=my-api-key")
    Call<Search> getSearchViewArticles(@Query("q") String q);

below BBCSportsFragment class where I have implemented RecyclerView and SearchView 在BBCSportsFragment类下面,我实现了RecyclerView和SearchView

public class BBCSportFragment extends Fragment implements ArticleAdapter.ClickListener {

public static List<Article> articleList = new ArrayList<>();

public static List<Article> origArticleList = new ArrayList<>();

@ActivityContext
public Context activityContext;
Search search;
@ApplicationContext
public Context mContext;

@BindView(R.id.recycler_view)
RecyclerView recyclerView;
BBCSportFragmentComponent bbcSportFragmentComponent;
BBCFragmentContextModule bbcFragmentContextModule;
private SportNews sportNews;
private static ArticleAdapter articleAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_bbcsport, container, false);
    ButterKnife.bind(this, view);
    SportInterface sportInterface = SportClient.getApiService();
    Call<SportNews> call = sportInterface.getArticles();
    call.enqueue(new Callback<SportNews>() {
        @Override
        public void onResponse(Call<SportNews> call, Response<SportNews> response) {
            if (response == null) {
                sportNews = response.body();
                if (sportNews != null && sportNews.getArticles() != null) {
                    articleList.addAll(sportNews.getArticles());

                }
                articleAdapter = new ArticleAdapter(articleList, sportNews);
                ApplicationComponent applicationComponent;
                applicationComponent = (ApplicationComponent) MyApplication.get(Objects.requireNonNull(getActivity())).getApplicationContext();
                bbcSportFragmentComponent = (BBCSportFragmentComponent) DaggerApplicationComponent.builder().contextModule(new ContextModule(getContext())).build();
                bbcSportFragmentComponent.injectBBCSportFragment(BBCSportFragment.this);
                RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(applicationComponent));
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(articleAdapter);
            }
        }

        @Override
        public void onFailure(Call<SportNews> call, Throwable t) {

        }
    });


    SportInterface searchInterface = SportClient.getApiService();
    Call<Search> searchCall = searchInterface.getSearchViewArticles("q");
    searchCall.enqueue(new Callback<Search>() {
        @Override
        public void onResponse(Call<Search> call, Response<Search> response) {
            search = response.body();

            if (search != null && search.getArticles() != null) {
                articleList.addAll(search.getArticles());
                origArticleList.clear();
                origArticleList.addAll(search.getArticles());
            }

            articleAdapter = new ArticleAdapter(articleList, search);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(articleAdapter);
        }

        @Override
        public void onFailure(Call<Search> call, Throwable t) {

        }
    });


    return view;


}

private Context getContext(ApplicationComponent applicationComponent) {
    return null;
}

public static void doFilter(String searchQuery) {
    searchQuery = searchQuery.toLowerCase();
    articleList.clear();
    for (Article article : origArticleList) {
        final String text = article.getTitle();
        if (text.equals(searchQuery))
            articleList.add(article);
    }
    articleAdapter.notifyDataSetChanged();
}
}

below adapter class 在适配器类下面

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.CustomViewHolder> {

public static final String urlKey = "urlKey";
List<Article> articles;

private ClipboardManager myClipboard;
private ClipData myClip;


public ArticleAdapter(List<Article> articles, SportNews sportNews) {
    this.articles = articles;


}

public ArticleAdapter(ClickListener clickListener) {
}

public ArticleAdapter(List<Article> articleList, Search search) {
}

@NonNull
@Override
public ArticleAdapter.CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.article_list, null);
    return new ArticleAdapter.CustomViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull edgar.yodgorbek.sportnews.adapter.ArticleAdapter.CustomViewHolder customViewHolder, int position) {
    Article article = articles.get(position);
    SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");

    Date d = new Date();
    try {
        d = input.parse(article.getPublishedAt());
    } catch (ParseException e) {
        e.printStackTrace();
    }


    String formatted = output.format(d);
    customViewHolder.articleTime.setText(formatted);
    customViewHolder.articleAuthor.setText(article.getSource().getName());
    customViewHolder.articleTitle.setText(article.getTitle());
    Picasso.get().load(article.getUrlToImage()).into(customViewHolder.articleImage);
    customViewHolder.itemView.setOnClickListener(v -> {
        Intent intent = new Intent(v.getContext(), DetailActivity.class);

        intent.putExtra("urlKey", article.getUrl());

        v.getContext().startActivity(intent);

    });
    customViewHolder.articleShare.setOnClickListener(v -> {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String articleDescription = article.getDescription();
        String articleTitle = article.getTitle();
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, articleDescription);
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, articleTitle);
        v.getContext().startActivity((Intent.createChooser(sharingIntent, "Share using")));


    });

    customViewHolder.articleFavorite.setOnClickListener(v -> {
        myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);


        myClip = ClipData.newPlainText("label", customViewHolder.articleTitle.getText().toString());
        myClipboard.setPrimaryClip(myClip);
        Toast.makeText(v.getContext(), "Copied to clipboard", Toast.LENGTH_SHORT).show();

    });
}


@Override
public int getItemCount() {
    if (articles == null) return 0;
    return articles.size();
}

public interface ClickListener {
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.articleAuthor)
    TextView articleAuthor;
    @BindView(R.id.articleTitle)
    TextView articleTitle;
    @BindView(R.id.articleImage)
    ImageView articleImage;
    @BindView(R.id.articleTime)
    TextView articleTime;
    @BindView(R.id.articleShare)
    ImageButton articleShare;
    @BindView(R.id.articleFavorite)
    ImageButton articleFavorite;

    public CustomViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);


    }
}


}

below main.xml 在main.xml下面

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_white"
        android:title="@string/search_hint"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

following screeshot of recyclerview item in navgation drawer fragment 跟随导航抽屉片段中的recyclerview项目的screeshot

Sorry for late reply, but it seems like you are calling both APIs at once. 很抱歉迟到的回复,但好像你是在同时调用这两个API。 One for getting all data and another one for getting filtered data which contain some string (Eg In your code you are having "q"). 一个用于获取所有数据,另一个用于获取包含某些字符串的过滤数据(例如,在您的代码中,您有“q”)。

You need to remove below lines from onCreateView of your Fragment. 您需要从片段的onCreateView中删除以下行。 Only call it after click on search icon. 只有在点击搜索图标后才能调用它。 And replace the parameter "q" in searchInterface.getSearchViewArticles("q"); 并在searchInterface.getSearchViewArticles(“q”)中替换参数“q” ; with the string value which you will get from the search box. 使用您将从搜索框中获取的字符串值。

    SportInterface searchInterface = SportClient.getApiService();
    Call<Search> searchCall = searchInterface.getSearchViewArticles("q");
    searchCall.enqueue(new Callback<Search>() {
        @Override
        public void onResponse(Call<Search> call, Response<Search> response) {
            search = response.body();

            if (search != null && search.getArticles() != null) {
                articleList.clear();
                articleList.addAll(search.getArticles());
            }

            if(articleAdapter != null){
                articleAdapter.notifyDataSetChanged();
            } else {
                articleAdapter = new ArticleAdapter(articleList, search);
                RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(articleAdapter);
            }
        }

        @Override
        public void onFailure(Call<Search> call, Throwable t) {

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM