13

Tôi có RecyclerView tùy chỉnh để tạo ListView. Và nó hoạt động tuyệt vời, khi tôi đang cố gắng để cư một danh sách xem trong id của bố trí của tôi.Thêm RecyclerView (RecyclerFragment) vào Hộp thoại

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); 

Bây giờ, tôi muốn điền một danh sách mới bên trong hộp thoại. Tôi có thể làm như thế nào?

Tôi cố gắng này, Có mygrid như tĩnh

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(); 
    } 
} 

Và sau đó,

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
DialogFragment newFragment = MyDialogFragment.newInstance(); 
ft.add(R.id.MyGridContainer, newFragment); 
//getView().findViewById(R.id.MyGridContainer).setVisibility(View.VISIBLE); 
ft.commit(); 
+1

chỉ cần áp dụng cùng một logic bạn áp dụng cho * * 'Fragment' – Blackbelt

+0

Tại sao bạn không sử dụng phương pháp AlertDialog.Builder bình thường không? Ở đó bạn có thể gọi addView() –

+0

@Blackbelt, tôi đã thử sử dụng FragmentTransaction nhưng tiếc là nó không hoạt động. Tôi không nhận thức được nhiều khái niệm cốt lõi, và tôi vẫn là người học. –

Trả lời

28

DialogFragment chỉ là một phân đoạn khác, làm tăng chế độ xem tùy chỉnh của bạn như bạn sẽ làm cho bất kỳ đoạn nào khác.

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; 
    } 
} 
3

Giả sử rằng bạn có một tĩnh bình thường Fragment tên mygrid, đây là cách DialogFragment của bạn sẽ trông như thế :

public class MyDialogFragment extends DialogFragment { 
    static MyDialogFragment newInstance() { 
     return new MyDialogFragment(); 
    } 

    // this method create view for your Dialog 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return mygrid.getView(); 
    } 

    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     Dialog dialog = new Dialog(getActivity()); 
     return dialog; 
    } 
} 

Và đây là cách bạn sho uld hiển thị:

DialogFragment fragment = MyDialogFragment.newInstance(); 
fragment.show(getSupportFragmentManager(), "some tag"); // please refer to DialogFragment#show() method in documentations. 
3

Hiển thị RecyclerView trong đoạn hộp thoại cũng đơn giản như bạn làm trong đoạn bình thường. Tuy nhiên, để hiển thị trong đoạn hội thoại bạn cần phải tạo ra một hộp thoại như:

public class AppDialogs extends DialogFragment { 
private AlertDialog.Builder builder; 

public static AppDialogs newInstance(int dialogNo, String title, String msg) 
{ 
    AppDialogs fragment = new AppDialogs(); 
    Bundle args = new Bundle(); 
    args.putInt("dialogNo",dialogNo); 
    args.putString("title", title); 
    args.putString("msg", msg); 
    fragment.setArguments(args); 

    return fragment; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{ 
    if(android.os.Build.VERSION.SDK_INT<=android.os.Build.VERSION_CODES.KITKAT) { 
     getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 0, 0, 0))); 
    } 
    return null; 
} 


@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    Bundle bundle = getArguments(); 
    int pos = bundle.getInt("dialogNo"); 
    switch (pos) { 
     case 0: 
      return showList(); 


    } 

    return super.onCreateDialog(savedInstanceState); 

} 


private Dialog showList() { 
    builder = new AlertDialog.Builder(getActivity(), R.style.app_dialog_theme); 
    builder.setTitle(title); 


RecyclerView rView; 
    builder.setView(rView); 

     return builder.create(); 
    } 
} 

và gọi nó là từ mảnh hoặc hoạt động của bạn, bạn không cần bất kỳ id chứa chỉ cần gọi những dòng AppDialogs appDialogs = AppDialogs.newInstance(0, title, msg); appDialogs.setCancelable(false); appDialogs.show(getFragmentManager(), null);

Nó nên làm bạn làm việc nếu không xin vui lòng cho tôi biết.

4

Câu trả lời được chấp nhận hoạt động, nhưng nó đòi hỏi những nỗ lực bổ sung để giữ cho nó giống hộp thoại chuẩn hơn.

Dưới đây là một cách có thể khác, cho phép bạn giữ tất cả chức năng của hộp thoại (ví dụ: tiêu đề, biểu tượng, nút dương/âm/trung tính). Ý tưởng là để ghi đè onCreateDialog và sử dụng phương pháp 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(); 
    } 
} 
Các vấn đề liên quan