我正在尝试将当前片段的意图添加到另一个片段。为此,我在卡片中的ImageButton上添加了一个OnclickListener。
但是它不能正常工作,我无法在我的代码中找到错误。
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();
}
}
}
答案 0 :(得分:5)
对于那些使用 kotlin 的人,你可以简单地做这样的事情
val dialog = IntervModifFragment()//The fragment that u want to open for example
val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
dialog.show(ft, ContentValues.TAG)
答案 1 :(得分:4)
只需将您的活动传递给适配器并使用
即可activity.getSupportFragmentManager()
但这不是个好主意。在适配器中使用Listener,并在要设置适配器的活动中调用它。
答案 2 :(得分:3)
FragmentManager fm = new CardExpandFragmentmFragment ()
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.cardExpand, fm, "fragment_card");
ft.commit();
答案 3 :(得分:3)
最好将FragmentManager
从您的活动传递到您的适配器构造函数(如果您正在使用,则从片段传递)
FragmentManager fragmentManager;
public MyAccountsCardAdapter(Context context ,ArrayList<MyAccountsCard> cardData ,FragmentManager fragmentManager) {
this.cardArrayList=cardData;
this.fragmentManager = fragmentManager;
}
然后致电
Fragment mFragment = new CardExpandFragment();
fragmentManager .beginTransaction().replace(R.id.cardExpand, mFragment).commit();
答案 4 :(得分:1)
使用onClick
将Fragment/Activity
操作移至使用adapter
的相应interface
。最好不要在Fragment transactions
中执行adapter classes
。
如你所见:
itemView.setOnClickListener(this);
此代码行表示onClickListener
没有特定的image button
。您设置的click listener
适用于整个项目。
答案 5 :(得分:1)
不要在ViewHolder中分配上下文值。您已经从构造函数传递了上下文值。
public ViewHolder(View itemView, ImageButton imButton) {
super(itemView);
itemView.setOnClickListener(this);
iButton = imButton;
context = itemView.getContext();
}
您只需要将您的上下文转换为AppcompatActivity。只需确保您正在使用AppCompatActivity而不是Activity并从Adapter的构造函数中的Activity传递Context值。不仅仅是它可以施展
@Override
public void onClick(View v) {
Fragment mFragment = new CardExpandFragment();
((AppCompatActivity)context).getSupportFragmentManager().beginTransaction().replace(R.id.cardExpand, mFragment).commit();
}