gpt4 book ai didi

android - 通过服务类将项目添加到 RecyclerView

转载 作者:行者123 更新时间:2023-11-29 01:18:48 24 4
gpt4 key购买 nike

在我的 Fragment 类中,这是我将 Item 添加到我的 RecyclerView 的方式。

        @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.online_devices, container, false);

recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);

theDataList = new ArrayList<>();
theRecyclerAdpater = new TheRecyclerAdapter(getActivity().getBaseContext(), theDataList);

recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(theRecyclerAdpater);

TheData a = new TheData("01","another value","another value","another value","another value");
theDataList.add(a);
a = new TheData("02","another value","another value","another value","another value");
theDataList.add(a);
theRecyclerAdpater.notifyDataSetChanged();

return view;
}

但是如果我想在 UI 可用时通过 Service 添加一个项目到我的 RecyclerView 怎么办?我的意思是,Service 在后台运行,如果应用程序的 Main UI RecyclerViewAvailable 那么添加项目否则不要添加项目。有人知道我该怎么做吗?

目前,我的 Service 中只有这个

    @Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate(){
//Add an Item here to RecyclerView if the UI is Available.
}

@Override
public void onDestroy() {
super.onDestroy();
}

更新我只是试了一下,但没有用。

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.online_devices, container, false);

recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);

theDataList = new ArrayList<>();
theRecyclerAdpater = new TheRecyclerAdapter(getActivity().getBaseContext(), theDataList);

recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(theRecyclerAdpater);

getActivity().startService(new Intent(getActivity(),MyService.class));
bus.register(this);

return view;
}

@Subscribe
public void getMessage(String s) {
TheData a = new TheData(s);
theDataList.add(a);
theRecyclerAdpater.notifyDataSetChanged();
}

在我的服务中

@Override
public void onCreate(){
bus.post("PP");
}

最佳答案

首先,Otto 现在已折旧,我不得不将其更改为 EventBus .我不得不再问一遍,感谢you .所以这就是我所做的。

添加到 gradle compile 'org.greenrobot:eventbus:3.0.0'

在我的 fragment 类中

/*I had to import*/
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public static class MyFragment extends Fragment {

RecyclerView recyclerView;
LinearLayoutManager linearLayoutManager;
static TheRecyclerAdapter theRecyclerAdpater;
private List<TheData> theDataList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
EventBus.getDefault().register(this); //Register it first

View view = inflater.inflate(R.layout.online_devices, container, false);

recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);

theDataList = new ArrayList<>();
theRecyclerAdpater = new TheRecyclerAdapter(getActivity().getBaseContext(), theDataList);

recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(theRecyclerAdpater);

getActivity().startService(new Intent(getActivity(),MyService.class)); //I call the Service

return view;
}

//Subscribe, run it on UI
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(TheData data){
TheData a = new TheData(data.getPc_number());
theDataList.add(a);
theRecyclerAdpater.notifyDataSetChanged();
}
}

在我的服务类中

public class ServerHelperService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate(){
EventBus.getDefault().post(new TheData("OK")); //and This
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
}
}

然后就完成了。仍在阅读它的 guide .感谢您的帮助!

关于android - 通过服务类将项目添加到 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38115139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com