Android RX Java RecycleView
我不熟悉RXJava。尝试将其与recycleview一起使用。由于某种原因,我的代码不起作用。这里是我的代码 具有循环视图的片段Android RX Java RecycleView,android,android-recyclerview,rx-java,Android,Android Recyclerview,Rx Java,我不熟悉RXJava。尝试将其与recycleview一起使用。由于某种原因,我的代码不起作用。这里是我的代码 具有循环视图的片段 public class CheeseListFragment extends Fragment { private final CompositeSubscription subscriptions = new CompositeSubscription(); private PublishSubject<String&g
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);
}
}
}
}
公共类CheeseListFragment扩展片段{
私有最终CompositeSubscription订阅=新CompositeSubscription();
私人出版主体时间跨度主体;
私人最终功能1趋势研究=
新功能1(){
@凌驾
公共可观察调用(字符串s){
RadioriveInfoObservableService RadioriveInfoObservableService=ApiProvider.getInstance().getRadioObserverInfo();
返回radioLiveInfoObservableService.radioInfo(类型.间隔)
.observeOn(AndroidSchedulers.mainThread())
.doError(趋势错误)
.onErrorResumeNext(Observable.empty());
}
};
私有最终操作1趋势错误=新操作1(){
@覆盖公共作废调用(可丢弃可丢弃){
Timber.e(可丢弃,“未能获得趋势存储库”);
}
};
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
timespanSubject=PublishSubject.create();
最终再循环视图rv=(再循环视图)充气机。充气(
R.layout.fragment_cheese_list,容器,假);
设置回收视图(rv);
订阅。添加(timespanSubject)
.平面图(趋势研究)
.map(SearchResultToRepositoryList.instance())
.订阅(适配器));
返回rv;
}
专用SimpleStringRecycleServiceAdapter适配器;
私有void设置回收视图(回收视图回收视图){
setLayoutManager(新的LinearLayoutManager(recyclerView.getContext());
adapter=new SimpleStringRecycleServiceAdapter(getActivity(),new SimpleStringRecycleServiceAdapter.CurrentShowClickListener()){
@凌驾
当前显示单击时的公共无效(当前显示){
Intent Intent=新的Intent(capapplication.getAppContext(),CheeseDetailActivity.class);
intent.putExtra(CheeseDetailActivity.EXTRA_NAME,currentShow.getName());
capapplication.getAppContext().startActivity(intent);
}
});
recyclerView.setAdapter(适配器);
registerAdapterDataObserver(新的RecyclerView.AdapterDataObserver(){
@凌驾
更改后的公共无效(){
Toast.makeText(getActivity(),“数据已更改”,Toast.LENGTH_SHORT.show();
}
});
}
私有列表getRandomSublist(字符串[]数组,整数金额){
ArrayList=新的ArrayList(金额);
随机=新随机();
while(list.size()
SearchToResultRepositoryList
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;
}
}
公共最终类SearchResultToRepositoryList实现Func1
public interface RadioLiveInfoObservableService {
@GET("/api/live-info/")
Observable<LiveInfo> radioInfo(
@Query("type") Type type);
}
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);
}
});