如何从recyclerview适配器调用子片段

时间:2017-09-26 10:31:23

标签: android

当我点击recyclerview的单行时,我想从recyclerview适配器打开片段。我在此活动中有一个主要活动和三个片段与视图寻呼机。在第二个片段里面我还有两个片段。在第一个片段里面我使用了recyclerview。从那个recyclerview我想在点击recyclelerview行时打开新的片段。

TodayTaskAdapter

package com.smartitventures.employeeapp.Adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.smartitventures.employeeapp.Activities.ProfileActivity3;
import com.smartitventures.employeeapp.Fragments.FragmentTaskHistory;
import com.smartitventures.employeeapp.Fragments.TaskDetailFragment;
import com.smartitventures.employeeapp.Interfaces.ItemClickListener;
import com.smartitventures.employeeapp.R;
import com.smartitventures.employeeapp.Response.Response.TaskResponse.TaskPayload;

import java.util.ArrayList;


public class TodayTaskAdapter extends RecyclerView.Adapter<TodayTaskAdapter.MyViewHolder> {

    private ArrayList<TaskPayload> taskPayloadArrayList;
    private Context context;

    ProfileActivity3 profileActivity3 = (ProfileActivity3) context;

    public TodayTaskAdapter(Activity context, ArrayList<TaskPayload> taskPayloadArrayList)
    {
        this.taskPayloadArrayList = taskPayloadArrayList;
        this.context = context;

    }

    @Override
    public TodayTaskAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.today_task_row, parent, false);

        context = parent.getContext();
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(TodayTaskAdapter.MyViewHolder holder, int position) {


        TaskPayload taskPayload  = taskPayloadArrayList.get(position);

        holder.tvTitle.setText(taskPayload.getTitle());
        holder.tvDesc.setText(taskPayload.getDescription());
        holder.tvAssignDate.setText(taskPayload.getCreatedOn());
        holder.tvDeadline.setText(taskPayload.getDeadline());

        holder.setClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isLongClick) {

                Toast.makeText(context, "Clickkkkkkkkkkkkkkkkkkkkkkkk", Toast.LENGTH_SHORT).show();

                FragmentTaskHistory fragmentTaskHistory = new FragmentTaskHistory();
                FragmentTransaction transaction = profileActivity3.getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_container, fragmentTaskHistory, "Fragment History Task").commit();

            }
        });

    }

    @Override
    public int getItemCount() {
        return taskPayloadArrayList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        TextView tvTitle,tvDesc,tvAssignDate,tvDeadline;

        private ItemClickListener clickListener;


        public MyViewHolder(View itemView) {
            super(itemView);

            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvDesc = itemView.findViewById(R.id.tvDesc);
            tvAssignDate = itemView.findViewById(R.id.tvAssignDate);
            tvDeadline = itemView.findViewById(R.id.tvDeadline);

            itemView.setOnClickListener(this);

        }

        public void setClickListener(ItemClickListener itemClickListener){
            this.clickListener = itemClickListener;
        }

        @Override
        public void onClick(View view) {
            clickListener.onClick(view,getPosition(),false);

        }


    }
}

1 个答案:

答案 0 :(得分:1)

试试这种方式;

holder.setClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {

             Fragment fragment = new FragmentTaskHistory();
             FragmentManager fm = getActivity().getSupportFragmentManager();
             FragmentTransaction ft = fm.beginTransaction();
             ft.replace(R.id.frame_container, fragment);
             ft.commit();

        }
    });