首页
学习
活动
专区
圈层
工具
发布

RX
EN

Stack Overflow用户
提问于 2015-07-22 14:45:41
回答 1查看 1.8K关注 0票数 1

我不熟悉rx。试着和recycleview一起使用。出于某种原因,我的代码不起作用,这就是我的代码。

带循环视图的片段

代码语言:javascript
复制
 public class CheeseListFragment extends Fragment {
        private final CompositeSubscription subscriptions = new CompositeSubscription();
        private  PublishSubject<String> timespanSubject;
        private final Func1<String, Observable<LiveInfo>> trendingSearch =
                new Func1<String, Observable<LiveInfo>>() {
                    @Override
                    public Observable<LiveInfo> call(String s) {
                        RadioLiveInfoObservableService radioLiveInfoObservableService=ApiProvider.getInstance().getRadioObserverInfo();
                        return radioLiveInfoObservableService.radioInfo(Type.INTERVAL)
                                .observeOn(AndroidSchedulers.mainThread())
                                .doOnError(trendingError)
                                .onErrorResumeNext(Observable.<LiveInfo>empty());
                    }
                };

        private final Action1<Throwable> trendingError = new Action1<Throwable>() {
            @Override public void call(Throwable throwable) {
                Timber.e(throwable, "Failed to get trending repositories");
            }
        };

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            timespanSubject = PublishSubject.create();
            final RecyclerView rv = (RecyclerView) inflater.inflate(
                    R.layout.fragment_cheese_list, container, false);    
            setupRecyclerView(rv);

            subscriptions.add(timespanSubject
                    .flatMap(trendingSearch)
                    .map(SearchResultToRepositoryList.instance())
                    .subscribe(adapter));
            return rv;
        }

            private SimpleStringRecyclerViewAdapter adapter;

        private void setupRecyclerView(RecyclerView recyclerView) {
            recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
             adapter=new SimpleStringRecyclerViewAdapter(getActivity(), new SimpleStringRecyclerViewAdapter.CurrentShowClickListener() {
                @Override
                public void onCurrentShowClick(Current currentShow) {
                    Intent intent = new Intent(CApplication.getAppContext(), CheeseDetailActivity.class);
                    intent.putExtra(CheeseDetailActivity.EXTRA_NAME, currentShow.getName());

                    CApplication.getAppContext().startActivity(intent);
                }
            });
            recyclerView.setAdapter(adapter);
            adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                @Override
                public void onChanged() {
                    Toast.makeText(getActivity(),"data changed",Toast.LENGTH_SHORT).show();
                }
            });

        }

        private List<String> getRandomSublist(String[] array, int amount) {
            ArrayList<String> list = new ArrayList<>(amount);
            Random random = new Random();
            while (list.size() < amount) {
                list.add(array[random.nextInt(array.length)]);
            }
            return list;
        }

        public static class SimpleStringRecyclerViewAdapter
                extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> implements Action1<List<Current>> {
            private List<Current> currentShows = Collections.emptyList();

            public interface CurrentShowClickListener {
                void onCurrentShowClick(Current currentShow);
            }

            private final CurrentShowClickListener currentShowClickListener;

            private final TypedValue mTypedValue = new TypedValue();
            private int mBackground;

            @Override
            public void call(List<Current> currentShows) {
                this.currentShows = currentShows;
                notifyDataSetChanged();
            }

            public SimpleStringRecyclerViewAdapter(Context context,CurrentShowClickListener currentShowClickListener) {
                context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
                mBackground = mTypedValue.resourceId;
                this.currentShowClickListener = currentShowClickListener;
            }

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                ListItemView view = (ListItemView)LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item, parent, false);
                view.setBackgroundResource(mBackground);
                return new ViewHolder(view);
            }

            @Override public void onBindViewHolder(ViewHolder viewHolder, int i) {
                viewHolder.bindTo(currentShows.get(i));
            }

            @Override public long getItemId(int position) {
                return position;
            }

            @Override public int getItemCount() {
                return currentShows.size();
            }

            public final class ViewHolder extends RecyclerView.ViewHolder {
                public final ListItemView itemView;
                private Current currentShow;

                public ViewHolder(ListItemView itemView) {
                    super(itemView);
                    this.itemView = itemView;
                    this.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override public void onClick(View v) {
                            currentShowClickListener.onCurrentShowClick(currentShow);
                        }
                    });
                }

                public void bindTo(Current currentShow) {
                    this.currentShow = currentShow;
                    itemView.bindTo(currentShow);
                }
            }
        }
    }

SearchToResultRepositoryList

代码语言:javascript
复制
public final class SearchResultToRepositoryList implements Func1<LiveInfo, List<Current>> {
  private static volatile SearchResultToRepositoryList instance;

  public static SearchResultToRepositoryList instance() {
    if (instance == null) {
      instance = new SearchResultToRepositoryList();
    }
    return instance;
  }

  @Override public List<Current> call(LiveInfo repositoriesResponse) {
    List<Current> currents=new ArrayList<>();
    currents.add(repositoriesResponse.getCurrent());
    return currents;
  }
}

休息

代码语言:javascript
复制
public interface RadioLiveInfoObservableService {

    @GET("/api/live-info/")
    Observable<LiveInfo> radioInfo(
            @Query("type") Type type);
}

只是什么都没做。我试着调试它,根本没有调用trendingSearch.call。

我只能这样做。但我还是想知道怎么订阅

代码语言:javascript
复制
RadioLiveInfoObservableService radioLiveInfoObservableService=ApiProvider.getInstance().getRadioObserverInfo();
radioLiveInfoObservableService.commits(Type.INTERVAL)
        .observeOn(AndroidSchedulers.mainThread())
        .doOnError(trendingError)
        .onErrorResumeNext(Observable.<LiveInfo>empty()).subscribe(new Action1<LiveInfo>() {
    @Override
    public void call(LiveInfo liveInfo) {
        List<Current> currents=new ArrayList<Current>();
        currents.add(liveInfo.getCurrent());
        adapter.currentShows=currents;
        adapter.notifyDataSetChanged();
        rv.setAdapter(adapter);
    }
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-22 16:56:47

这是大量需要消化的代码,以查找错误,但在调用timeSpanSubject.onNext()之前,不会发生任何事情。我在任何地方都没有看到这个调用,但是可能有一些您没有显示的缺失代码。

如果没有缺少调用timeSpanSubject.onNext()的代码,那么您可以使用一个BehaviorSubject,它将在第一次订阅时发出一个项,或者使用另一个可观察到的代码,比如timerinterval,这取决于您要做什么。timer只订阅一次可观察到的trendingSearch,而使用interval则会订阅多次。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31566510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档