Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call fragment From recyclerView adapter

I want call fragment on imageItem click getSupportFragmentManger cannot call hera and when pass it in adapter constructor also cannot work

   public class MarketOneRecyclerAdapter extends       RecyclerView.Adapter<MarketOneRecyclerAdapter.MainViewHolder>{                                          
                                                                                                                                                      
       public interface OnItemClickListener {                                                                                                            
           void onItemClick(ArrayList< MarketItems> item, int Pos);                                                                                      
       }                                                                                                                                                 
       private final OnItemClickListener listener;                                                                                                       
       private ArrayList<MarketItems>marketArrayList;                                                                                                    
       private Context context;                                                                                                                          
                                                                                                                                                      
         public MarketOneRecyclerAdapter(Context context, ArrayList<MarketItems> marketArrayList,  OnItemClickListener listener) {                         
        this.listener = listener;                                                                                                                     
        this.context = context;                                                                                                                       
        this.marketArrayList = marketArrayList;                                                                                                       
    }                                                                                                                                                 
    @Override                                                                                                                                         
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {                                                                        
        View view = LayoutInflater.from(parent.getContext())                                                                                          
                .inflate(R.layout.market_item, parent, false);                                                                                        
        return new MarketOneRecyclerAdapter.MainViewHolder(view);                                                                                     
    }                                                                                                                                                 
                                                                                                                                                      
    @Override                                                                                                                                         
    public void onBindViewHolder(MainViewHolder holder, int position) {                                                                               
        holder.bind(marketArrayList,listener,position);                                                                                               
                                                                                                                                                      
    }                                                                                                                                                 
                                                                                                                                                      
    @Override                                                                                                                                         
    public int getItemCount() {                                                                                                                       
        return marketArrayList.size();                                                                                                                
    }                                                                                                                                                 

                                                                                                                                             
    public class MainViewHolder extends RecyclerView.ViewHolder{                                                                                      
                                                                                                                                                      
        ImageView imageItem;                                                                                                                          
        CardView cardView;                                                                                                                            
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
        public MainViewHolder(View itemView) {                                                                                                        
                                                                                                                                                      
            super(itemView);                                                                                                                          
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
            cardView= (CardView) itemView.findViewById(R.id.market_card_view);                                                                        
            imageItem= (ImageView)      itemView.findViewById(R.id.market_item_imageView);                                                                 
            imageItem.setOnClickListener(new View.OnClickListener() {                                                                                 
                @Override                                                                                                                             
                public void onClick(View view) {                                                                                                      
                                                                                                                                                      
                    //Want here call a fragment                                                                                                       
                                                                                                                                                      
                }                                                                                                                                     
            });                                                                                                                                       
                                                                                                                                                      
                                                                                                                                                      
        }                                                                                                                                             
        public void bind(final ArrayList<MarketItems> item, final OnItemClickListener listener , final int i){                                        
                            Picasso.with(context).load(marketArrayList.get(i).getImages()).networkPolicy(NetworkPolicy.OFFLINE).into(imageItem);                      
                                                                                                                                                      
                                                                                                                                                      
            imageItem.setTag(item.get(i).getImages());                                                                                                
                                                                                                                                                      
            itemView.setOnClickListener(new View.OnClickListener() {                                                                                  
                @Override                                                                                                                             
                public void onClick(View view) {                                                                                                      
                    listener.onItemClick(item,i);                                                                                                     
            `enter code here`       }                                                                                                                                     
            });                                                                                                                                       
        }                                                                                                                                             
    }                                                                                                                                                 
}     

public  class ImageItemFragment extends Fragment {
    ImageView imageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.fragment_image_item, container, false);

        imageView= (ImageView) view.findViewById(R.id.market_item_image);
        //Picasso.with(getActivity()).load(marketItems.getImages()).networkPolicy(NetworkPolicy.OFFLINE).into(imageView);


        return view;
    }
}
like image 892
a.shehata Avatar asked Jan 30 '17 20:01

a.shehata


2 Answers

holder is ViewHolder inOnBindViewHolder

holder.image_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AppCompatActivity activity = (AppCompatActivity) v.getContext();
            Fragment myFragment = new TaskApprovalFragmentDetails();
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.linearlayouttaskapproval, myFragment).addToBackStack(null).commit();


        }
    });
like image 165
Ajinkya Kalaskar Avatar answered Nov 01 '22 09:11

Ajinkya Kalaskar


In Kotlin you can use this

holder.setOnClickListener{it:View!
     val appCompatActivity = it.context as AppCompatActivity
                    appCompatActivity.supportFragmentManager.
                    beginTransaction()
                        .replace(R.id.frameLayout, YourFragment())
                        .addToBackStack(null)
                        .commit()
      }

`

like image 3
Rohit Jakhar Avatar answered Nov 01 '22 10:11

Rohit Jakhar