Reputation: 397
I am trying to add an intent from my current fragment to another fragment.For that I've added an OnclickListener over an ImageButton in the card.
But its not Working properly, I couldn't find the fault in my code.
package com.example.kgb.homescreen.myaccount;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import com.example.kgb.R;
import com.example.kgb.components.UpdatedTextView;
import java.util.ArrayList;
public class MyAccountsCardAdapter extends RecyclerView.Adapter<MyAccountsCardAdapter.ViewHolder> {
private static Context context;
public MyAccountsCardAdapter(Context context) {
this.context=context;
}
private ArrayList<MyAccountsCard>cardArrayList;
public MyAccountsCardAdapter(ArrayList<MyAccountsCard> cardData) {
this.cardArrayList=cardData;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.accounts_card_layout, parent, false);
ImageButton imButton = (ImageButton) itemView.findViewById(R.id.shareButton);
return new ViewHolder(itemView,imButton);
}
@Override
public void onBindViewHolder(MyAccountsCardAdapter.ViewHolder holder, int position ){
//holder.accountBalance.setTextColor(Color.parseColor("#000000"));
Log.d("ABBB","haiii" + holder.accountType.getText());
if(holder.accountType.getText().equals("Savings"))
{
holder.accountBalance.setTextColor(Color.parseColor("#000000"));
}
holder.accountNo.setText(cardArrayList.get(position).getAccountNo());
holder.scheme.setText(cardArrayList.get(position).getScheme());
holder.accountType.setText(cardArrayList.get(position).getAccountType());
holder.accountBalance.setText(cardArrayList.get(position).getAccountBalance());
}
@Override
public int getItemCount(){return cardArrayList.size();}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageButton iButton;
UpdatedTextView accountNo=(UpdatedTextView) itemView.findViewById(R.id.acc_no);
UpdatedTextView scheme=(UpdatedTextView) itemView.findViewById(R.id.acc_scheme);
UpdatedTextView accountType=(UpdatedTextView) itemView.findViewById(R.id.acc_type);
UpdatedTextView accountBalance=(UpdatedTextView) itemView.findViewById(R.id.acc_bal);
public ViewHolder(View itemView, ImageButton imButton) {
super(itemView);
itemView.setOnClickListener(this);
iButton = imButton;
context = itemView.getContext();
}
@Override
public void onClick(View v) {
Fragment mFragment = new CardExpandFragment();
context.getSupportFragmentManager().beginTransaction().replace(R.id.cardExpand, mFragment).commit();
}
}
}
Upvotes: 4
Views: 9576
Reputation: 8408
For those who use kotlin you can simply do something like this
val dialog = IntervModifFragment()//The fragment that u want to open for example
val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
dialog.show(ft, ContentValues.TAG)
Upvotes: 4
Reputation: 75788
FragmentManager fm = new CardExpandFragmentmFragment ()
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.cardExpand, fm, "fragment_card");
ft.commit();
Upvotes: 3
Reputation: 51
Move the onClick
actions to the respective Fragment/Activity
where the adapter
is being used by using an interface
. It is better to not perform the Fragment transactions
in adapter classes
.
As you see:
itemView.setOnClickListener(this);
This code line indicates there is no specific onClickListener
to the image button
. The click listener
you have set is for the entire item.
Upvotes: 1
Reputation: 728
Just don't the assign context values in ViewHolder. You are already passing context value from constructor.
public ViewHolder(View itemView, ImageButton imButton) {
super(itemView);
itemView.setOnClickListener(this);
iButton = imButton;
context = itemView.getContext();
}
You just need to do Cast your Context to AppcompatActivity. Just make sure that You are using AppCompatActivity instead of Activity and Passing the Context value from Activity in Adapter's constructor. Than only it can cast
@Override
public void onClick(View v) {
Fragment mFragment = new CardExpandFragment();
((AppCompatActivity)context).getSupportFragmentManager().beginTransaction().replace(R.id.cardExpand, mFragment).commit();
}
Upvotes: 3
Reputation: 18923
It's better to pass FragmentManager
to your adapter constructor from your activity(or from fragment if you'r using)
FragmentManager fragmentManager;
public MyAccountsCardAdapter(Context context ,ArrayList<MyAccountsCard> cardData ,FragmentManager fragmentManager) {
this.cardArrayList=cardData;
this.fragmentManager = fragmentManager;
}
Then call
Fragment mFragment = new CardExpandFragment();
fragmentManager .beginTransaction().replace(R.id.cardExpand, mFragment).commit();
Upvotes: 5
Reputation: 938
Just pass your activity to your adapter and use
activity.getSupportFragmentManager()
But its not good idea. Use Listener in the adapter and call it in activity where you are setting the adapter.
Upvotes: 4