Search code examples
androidfragmentandroid-recyclerviewdialogfragment

DialogFragment From RecyclerView


now i have new problem ..... i don't know how initialize dialogfragment in recyclerview and recyclerview in fragment , so who know haw to make it ?

Fragment code:

public View onCreateView( LayoutInflater inflater,  ViewGroup container3,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =inflater.inflate(R.layout.fragment_one_day_tasks,container3,false);
    final View vd =inflater.inflate(R.layout.dialogod,null);
    TextView textView= (TextView) v.findViewById(R.id.ActiveMenu);
    TextView textView1= (TextView) v.findViewById(R.id.Alert);
    database = new Database(v.getContext());
    mId = database.getLastIdOD();
    if(mId>=1) {
        oneDayList = database.getOneDayList(1);
    }
    database.close();

    //dlg1 = new DialogFragment();
    //GridView gridView = (GridView) v.findViewById(R.id.gridViewActiveOd);
    RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.gridResc);

    if(0<mId){
        recyclerView.setLayoutManager(new GridAutofitLayoutManager(v.getContext(),350));
        recyclerView.setAdapter(new adapterRecylcerViewGridODN(v.getContext(),oneDayList,inflater));
     /*   gridView.setAdapter(new GridOdAdapter(v.getContext(),oneDayList));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    final int position, long id) {
                tapOnAlert(vd,position);
            }
        });*/
    }else{
        textView1.setText("You Haven't got any Active Tasks !");
    }

    FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fabCreateOd);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            getActivity().finish();
            Intent intent = new Intent(getActivity().getApplicationContext(),CreateOneDayNotif.class);
            intent.putExtra("mId",1 + database.getLastIdOD());
            startActivity(intent);
            Snackbar.make(view, "New Task", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    return v;
}

RecyclerView Adapter code:

public void onBindViewHolder(SimpleViewHolder holder, final int position) {
    holder.title.setText(mItems.get(position).getTitle());
    datecount = new Datecount(mItems.get(position).getDateAndTime());
    long minute = datecount.getMinuteInt();
    long hour=datecount.getHourInt();
    long day=datecount.getDayInt();
    long year=datecount.getYearInt();
    long month=datecount.getMonthInt();
    holder.dataAtime.setText("On date: " + day+"/"+month+"/"+year+ " On time: "+hour+":"+minute);
    int iconResId = mContext.getResources().getIdentifier(mItems.get(position).getIcon(), "drawable", mContext.getPackageName());
    holder.image.setImageResource(iconResId);
    GradientDrawable bgShape = (GradientDrawable) holder.circle.getDrawable();
    bgShape.setColor(Color.parseColor(mItems.get(position).getColour()));
    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //I want to put here dialog.show
          //I want to put here dialog.show

    }
       });

And DialogFragment code:

   public class DialogFragmentOd extends DialogFragment {

     boolean isFabOpen;
     Animation fab_open,fab_close,rotate_forward,rotate_backward;
   List<OneDayTD> oneDayList;
   int  position;
   Database database;


public static DialogFragmentOd getInstance() {
    DialogFragmentOd dialog = new DialogFragmentOd();
    //Bundle arguments = new Bundle();

    //arguments.putParcelable(Person.class.getName(), person);
    //dialog.setArguments(arguments);
    return dialog;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View vd = inflater.inflate(R.layout.dialogod, null);
    .........
 getDialog().setContentView(layout);
   return vd;
 }
}

So what i need to do ???


Solution

  • Here is an alternative way to handle all clicks in your Activity or Fragment where we have the Context.

    Add a custom click listener interface in your Adapter.

    public interface OnDataClickListener {
        void onDataClick();
    }
    

    Create a variable in you Adapter to hold the custom click listener

    public MyAdapter {
        private OnDataClickListener mOnDataClickListener;
    
        public MyAdapter(OnDataClickListener listener) {
            mOnDataClickListener = listener;
        }
    }
    

    Call the click method of the custom interface when a button is clicked in your ViewHolder

    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mOnDataClickListener.onDataClick();
        }
    }
    

    Implement View.OnClickListener in your Fragment

    MyFragment implement View.OnClickListener {
        @Override
        public void onClick(View view) {
            // Call/Create your dialog fragment show from here.
        }
    }
    

    Pass the Fragment instance when initialising the Adapter

    MyAdapter adapter = new MyAdapter(this);

    That's all.