在片段中使用 Recyclerview
Using Recyclerview in a fragment
我试图在片段中输出 Recycler 视图,但启动后出现空白屏幕。请告诉我如何修复它?
我从 URL(XML 字符串)获取数据,然后解析它并将其写入 ArrayList。
BusinessFragment.java(在片段中,我使用第二个流获取 XML 字符串并进一步使用它(实际上有更多数据)):
'''
public class BusinessFragment extends Fragment {
RecyclerView recyclerView;
GetBusinessHelperAdapter getBusinessHelperAdapter;
ArrayList<String> title = new ArrayList<>();
class APITask extends AsyncTask<URL, Void,String> {
@Override
protected String doInBackground(URL... urls) {
String response = null;
try {
response = getResponseFromUrl(urls[0]);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
protected void onPostExecute(String response) {
try {
boolean inEntry = false;
String textValue = null;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(response));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equals("item")) {
inEntry = true;
}
} else if (eventType == XmlPullParser.TEXT) {
textValue = xpp.getText();
} else if (eventType == XmlPullParser.END_TAG) {
if (inEntry) {
if (xpp.getName().equals("item")) {
inEntry = false;
} else if (xpp.getName().equals("title")) {
title.add(textValue);
}
}
}
eventType = xpp.next();
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
getBusinessHelperAdapter = new GetBusinessHelperAdapter(title);
recyclerView.setAdapter(getBusinessHelperAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
URL generateURL = generateURL();
new APITask().execute(generateURL);
}
}
'''
GetBusinessHelperAdapter.java(Recyclerview 适配器)
'''
public class GetBusinessHelperAdapter extends
RecyclerView.Adapter<GetBusinessHelperAdapter.MyViewClass> {
ArrayList<String> title;
public GetBusinessHelperAdapter(ArrayList<String> title){
this.title=title;
}
public MyViewClass onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
MyViewClass myViewClass=new MyViewClass(view);
return myViewClass;
}
public void onBindViewHolder(MyViewClass holder, @SuppressLint("RecyclerView") int position) {
holder.title.setText(title.get(position));
BusinessFragment businessFragment = new BusinessFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title.get(position));
businessFragment.setArguments(bundle);
}
public int getItemCount(){
return title.size();
}
public class MyViewClass extends RecyclerView.ViewHolder{
TextView title;
public MyViewClass(View itemView){
super(itemView);
title = itemView.findViewById(R.id.title);
}
}
}
'''
在BusinessFragment中file.java在while循环后添加了'get Business Helper Adapter.notifyDataSetChanged();',一切都出来了
我试图在片段中输出 Recycler 视图,但启动后出现空白屏幕。请告诉我如何修复它? 我从 URL(XML 字符串)获取数据,然后解析它并将其写入 ArrayList。
BusinessFragment.java(在片段中,我使用第二个流获取 XML 字符串并进一步使用它(实际上有更多数据)):
'''
public class BusinessFragment extends Fragment {
RecyclerView recyclerView;
GetBusinessHelperAdapter getBusinessHelperAdapter;
ArrayList<String> title = new ArrayList<>();
class APITask extends AsyncTask<URL, Void,String> {
@Override
protected String doInBackground(URL... urls) {
String response = null;
try {
response = getResponseFromUrl(urls[0]);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
protected void onPostExecute(String response) {
try {
boolean inEntry = false;
String textValue = null;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(response));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equals("item")) {
inEntry = true;
}
} else if (eventType == XmlPullParser.TEXT) {
textValue = xpp.getText();
} else if (eventType == XmlPullParser.END_TAG) {
if (inEntry) {
if (xpp.getName().equals("item")) {
inEntry = false;
} else if (xpp.getName().equals("title")) {
title.add(textValue);
}
}
}
eventType = xpp.next();
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
getBusinessHelperAdapter = new GetBusinessHelperAdapter(title);
recyclerView.setAdapter(getBusinessHelperAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
URL generateURL = generateURL();
new APITask().execute(generateURL);
}
}
'''
GetBusinessHelperAdapter.java(Recyclerview 适配器)
'''
public class GetBusinessHelperAdapter extends
RecyclerView.Adapter<GetBusinessHelperAdapter.MyViewClass> {
ArrayList<String> title;
public GetBusinessHelperAdapter(ArrayList<String> title){
this.title=title;
}
public MyViewClass onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
MyViewClass myViewClass=new MyViewClass(view);
return myViewClass;
}
public void onBindViewHolder(MyViewClass holder, @SuppressLint("RecyclerView") int position) {
holder.title.setText(title.get(position));
BusinessFragment businessFragment = new BusinessFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title.get(position));
businessFragment.setArguments(bundle);
}
public int getItemCount(){
return title.size();
}
public class MyViewClass extends RecyclerView.ViewHolder{
TextView title;
public MyViewClass(View itemView){
super(itemView);
title = itemView.findViewById(R.id.title);
}
}
}
'''
在BusinessFragment中file.java在while循环后添加了'get Business Helper Adapter.notifyDataSetChanged();',一切都出来了