Mah*_*kar 18 android android-fragments android-alertdialog android-recyclerview
我有自定义的RecyclerView来创建ListView.当我尝试在我的布局ID中填充列表视图时,它工作得很好.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putBoolean("enablePullToRefresh", false);
GridValues gridValues = new GridValues();
gridValues.rowViewLayout = R.layout.my_detail_row_view;
gridValues.delegate = this;
mygrid = new CustomGridView(gridValues, bundle);
mygrid.showAsGrid = true;
mygrid.spanCount = 2;
mygrid.layoutOrientation = LinearLayoutManager.VERTICAL;
mygrid.noRowColor = true;
mygrid.gridName = "mygrid";
mygrid.setArguments(mygrid.bundle);
ft.replace(R.id.MyGridContainer, mygrid);
Run Code Online (Sandbox Code Playgroud)
现在,我想在对话框中填充一个新列表.我怎样才能做到这一点?
我试过这个,让mygrid成为静态的
public static class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return mygrid.getView();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,
FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyDialogFragment.newInstance();
ft.add(R.id.MyGridContainer, newFragment);
//getView().findViewById(R.id.MyGridContainer).setVisibility(View.VISIBLE);
ft.commit();
Run Code Online (Sandbox Code Playgroud)
rah*_*lrv 37
DialogFragment只是另一个片段,像对待任何其他片段一样为您的自定义视图充气.
public class MyDialogFragment extends DialogFragment {
private RecyclerView mRecyclerView;
private MyRecyclerAdapter adapter;
// this method create view for your Dialog
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate layout with recycler view
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//setadapter
CustomAdapter adapter = new MyRecyclerAdapter(context, customList);
mRecyclerView.setAdapter(adapter);
//get your recycler view and populate it.
return v;
}
}
Run Code Online (Sandbox Code Playgroud)
已接受的答案有效,但需要额外的努力才能使其更像标准对话框.
下面是另一种可能的方法,它将允许您保留所有对话框功能(例如标题,图标,正/负/中性按钮).想法是覆盖onCreateDialog和使用AlertDialog.Builder#setView()方法
public class MyDialogFragment extends DialogFragment {
private RecyclerView mRecyclerView;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mRecyclerView = new RecyclerView(getContext());
// you can use LayoutInflater.from(getContext()).inflate(...) if you have xml layout
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setAdapter(/* your adapter */);
return new AlertDialog.Builder(getActivity())
.setTitle(/* your title */)
.setView(mRecyclerView)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do something
}
}
).create();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25926 次 |
| 最近记录: |