在特定页面的RecyclerView中生成AsyncTask



我调用AsyncTask并使用RecyclerView创建一个内容滑块。

生成的滑块为2页或3页。

在第二页或第三页上,我必须进行AsyncTask makelist调用。

如果在onBindViewHolder中添加new makeList().execute();,则列表总是在第一页生成。

我尝试使用position变量将new makeList().execute();插入到其他方法中,如onItemChanged,但没有成功。

如何在特定的RecycleView页面上进行AsyncTask调用?

这是代码的一部分,我认为这是基本部分:

@Override
public void onCurrentItemChanged(@Nullable RecyclerView.ViewHolder viewHolder, int adapterPosition) {
onItemChanged(adapterPosition);
}

private void onItemChanged(int position) {
}

@Override
public void onScroll(float scrollPosition, int currentPosition, int newPosition, @Nullable ListServiceAdapter.ViewHolder currentHolder, @Nullable ListServiceAdapter.ViewHolder newCurrent) {
}

private class MyService extends AsyncTask<Void, Void, String> {

String content="";

@Override
protected void onPreExecute() {
ListService = null;
ListService = new ArrayList<ListServiceModel>();
}

@Override
protected String doInBackground(Void... params) {

//RETURN content from CALL

return content;
}

@Override
protected void onPostExecute(String result) {

JSONArray jsonArray2 = null;
JSONObject jsonObject;
try {
jsonObject = new JSONObject(content);
jsonArray2 = jsonObject.getJSONArray("service_1");
} catch (JSONException e) {
e.printStackTrace();
}


if (jsonArray2.length() != 0) {

try {

for (int i = 0; i < jsonArray2.length(); i++) {

JSONObject item_json = jsonArray2.getJSONObject(i);

final ListServiceModel item = new ListServiceModel();
item.desc(item_json.getString("desc"));
...

ListService.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}

mAdapter = new ListServiceAdapter(ListService);
scroll_1.setAdapter(mAdapter);

}

}

}



public class ListServiceAdapter extends RecyclerView.Adapter<ListServiceAdapter.ViewHolder> {

private List<ListServiceModel> data;
private View v;

public ListServiceAdapter(List<ListServiceModel> data) {
this.data = data;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v =null;


v = inflater.inflate(R.layout.item_Service, parent, false);


return new ViewHolder(v);


}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {


tempValues = null;
tempValues = (ListServiceModel) data.get(position);


holder.name.setText(tempValues.getName());


}


private void onItemChanged(int position) {


}


public void onItemClick(int mPosition) {

ListServiceModel tempValues2 = (ListServiceModel) ListService.get(mPosition);


}

private class OnItemClickListener implements View.OnClickListener {
private int mPosition;

OnItemClickListener(int position) {
mPosition = position;
}


@Override
public void onClick(View arg0) {

onItemClick(mPosition);
}
}

@Override
public int getItemCount() {

return data.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

private TextView desc;
...

@SuppressLint("ClickableViewAccessibility")
public ViewHolder(View itemView) {
super(itemView);
desc = itemView.findViewById(R.id.desc);
....
}


}

}

这是用AsyncTask生成的要在RecycleView页面中执行的列表:

private class makeList extends AsyncTask<String, Void, String> {

String content="";

@Override
protected void onPreExecute() {}

@Override
protected void onProgressUpdate(Void... values) {}

@Override
protected String doInBackground(String... params) {


//RETURN content from CALL


urlist = new ArrayList<>();
JSONArray jsonArray4 = null;
JSONObject jsonObject4;
try {
jsonObject4 = new JSONObject(content);
jsonArray4 = jsonObject4.getJSONArray("list");



} catch (JSONException e) {
e.printStackTrace();
}
JSONObject jsonInner;
String nome;


for (int count = 0; count<jsonArray4.length(); count++){

try {
jsonInner = jsonArray4.getJSONObject(count);
name = jsonInner.getString("name");

ServiceListModel obj = new ServiceListModel(nome);
urlist.add(obj);


} catch (JSONException e) {
e.printStackTrace();
}

}


return String.valueOf(urlist);

}


@Override
protected void onPostExecute(String result) {
mAdapter_2 = new ServiceListdapter(context, urlist);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(mAdapter_2);

}

}

编辑:这是我试图插入makelist执行的地方,但显然这是错误的

//This is where the data is written, which will be shown in each generated slider
@Override
public void onBindViewHolder(ViewHolder holder, int position) {


tempValues = null;
tempValues = (ListServiceModel) data.get(position);


holder.name.setText(tempValues.getName());
//On the second page, i would like to generate a list.
//Done in this way is not correct, the list is generated on the first page. 
//Even if I do "if position=1" the list is always generated on the first page
new makeList().execute();


}

我还尝试在此处插入函数执行。这似乎是解决方案,但滚动似乎没有被正确识别,列表有时出现在一个页面上,其他时间出现在另一个页面,其他时间不在任何地方,或者只是在我开始移动滑块时生成的,然后滚动就从我所在的页面开始了。

@Override
public void onScroll(float scrollPosition, int currentPosition, int newPosition, @Nullable ListServiceAdapter.ViewHolder currentHolder, @Nullable ListServiceAdapter.ViewHolder newCurrent) {
if(newPosition == 1){
new makeList().execute();
}
}

我认为这不是正确的方式。在回收器视图中,不可能使用视图模型(支持生命周期(。对于适配器,您应该只显示简单的数据。

另一种想法——AsyncTask早就准备好了。如果你的项目允许使用更新的东西(如ViewModel(,请使用它。使用AsyncTask,当你在onPostExecute(…(之前关闭视图时,你可能会有内存泄漏。

当你从async获得响应时,你可以使用notifyDataChanged(…(更新recyclerView适配器

第一个asyncTask的结果可能会返回带有片段数据的列表。您应该使用此列表在视图寻呼机中创建每个片段。这对我来说很简单——如果您还有问题,请给我看代码片段。

相关内容

  • 没有找到相关文章

最新更新