我使用以下代码在警报对话中显示回收者视图。调用方法showAlertDialog()时,显示屏上会出现一个对话框,但它是空的。包含输出信息的列表不为空,我检查了。
public class AlertDialogShower {
private final String LOG_TAG = "AlertDialogShower";
private Context context;
public AlertDialogShower(Context context) {
this.context = context;
}
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
public void showAlertDialog(AppInfoListModel appInfoListModel, FragmentManager fragmentManager) {
View view = LayoutInflater.from(context).inflate(R.layout.alert_app_workperiod, null);
RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.alert_app_info_list_view);
AppInfoArrayAdapter arrayAdapter = new AppInfoArrayAdapter(appInfoListModel.getAppInfos());
recyclerView.setAdapter(arrayAdapter);
AlertDialog.Builder builder = new AlertDialog
.Builder(new ContextThemeWrapper(view.getContext(), R.style.Theme_AppCompat_Dialog));
builder.setView(view);
builder.setCancelable(false)
.setNegativeButton(view.getContext().getString(R.string.cancel_text), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.setTitle(appInfoListModel.getAppPackage());
alertDialog.show();
}
/**
* This is a modified adapter for displaying and processing the list of events in listview
*/
public class AppInfoArrayAdapter extends RecyclerView.Adapter<AppInfoArrayAdapter.AppViewHolder> {
ArrayList<AppInfoListModel.AppInfo> appInfoList;
public AppInfoArrayAdapter(ArrayList<AppInfoListModel.AppInfo> appList) {
this.appInfoList = appList;
}
@Override
public AppViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.app_info_list_item, viewGroup, false);
AppViewHolder appViewHolder = new AppViewHolder(v);
return appViewHolder;
}
@Override
public void onBindViewHolder(AppViewHolder appViewHolder, int i) {
appViewHolder.periodDay.setText(appInfoList.get(i).getDate());
String periods = " ";
for (String s : appInfoList.get(i).getRunningEvents()){
periods += s + "\n";
}
appViewHolder.timeLaunch.setText(periods);
}
@Override
public int getItemCount() {
return appInfoList.size();
}
public class AppViewHolder extends RecyclerView.ViewHolder {
TextView periodDay;
TextView timeLaunch;
AppViewHolder(View v) {
super(v);
periodDay = (TextView) v.findViewById(R.id.ai_date_tv);
timeLaunch = (TextView) v.findViewById(R.id.ai_list_date_tv);
}
}
}
}
来自另一个片段的调用。 新 AlertDialogShower(fragmentView.getContext())showAlertDialog(APPINFO,getFragmentManager());
答案 0 :(得分:0)
您忘了添加带有RecycleView的LayoutManager,这就是它为空的原因。
可选的
//如果您知道更改,请使用此设置来提高性能 //在内容中不会更改RecyclerView的布局大小
mRecyclerView.setHasFixedSize(真);
需要
//使用线性布局管理器
ExternalProject_Add