1.TabLayout
- tab1 (Fragment1) - tab2 (Fragment2) - tab3 (Fragment3) * RecyclerView + CardView (OnClick)
上 CardView
ClickListner上打开tab3中的另一个片段.那么如何在tab3中打开片段.
错误在于getFragmentManager()
:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
而不是这样,我试过:
FragmentTransaction transaction = activity.getFragmentManager().beginTransaction(); FragmentTransaction transaction = itemview.getContext().getFragmentManager().beginTransaction();
但错误并未解决.
这是我的代码:
public class CardAdapter extends RecyclerView.Adapter{ List mItems; private int lastPosition = -1; Context context; TaskFragment main; public CardAdapter(Context context,TaskFragment ma) { this.context=context; main=ma; } public CardAdapter() { super(); mItems = new ArrayList (); NatureItem nature = new NatureItem(); nature.setName("The Paris Attack 2015"); nature.setDes("Lorem ipsum dolor sit amet."); nature.setThumbnail(R.drawable.news1); mItems.add(nature); } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) { View v = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.custom_list, viewGroup, false); ViewHolder viewHolder = new ViewHolder(v); return viewHolder; } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { NatureItem nature = mItems.get(i); viewHolder.tvNature.setText(nature.getName()); viewHolder.tvDesNature.setText(nature.getDes()); viewHolder.imgThumbnail.setImageResource(nature.getThumbnail()); // setAnimation(viewHolder.card,i); } @Override public int getItemCount() { return mItems.size(); } class ViewHolder extends RecyclerView.ViewHolder { private int lastPosition = -1; public ImageView imgThumbnail; public TextView tvNature; public TextView tvDesNature; // Button btnclear,btncancle; CardView card; Activity activity; Context co; public ViewHolder(final View itemView) { super(itemView); imgThumbnail = (ImageView) itemView.findViewById(R.id.img_thumbnail); tvNature = (TextView) itemView.findViewById(R.id.tv_nature); tvDesNature = (TextView) itemView.findViewById(R.id.tv_des_nature); card = (CardView) itemView.findViewById(R.id.card); card.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(itemView.getContext(), "Clicked Card...", Toast.LENGTH_LONG).show(); ShareFragment newFragment = new ShareFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.viewFragments, newFragment); transaction.addToBackStack(null); transaction.commit(); } }); } } }
Parinda Raja.. 25
在你的onclick中打开如下所示的新片段
@Override public void onClick(View view){ AppCompatActivity activity = (AppCompatActivity) view.getContext(); Fragment myFragment = new MyFragment(); activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit(); }
Piyush.. 15
问题:
Error:cannot find symbol method getSupportFragmentManager()
方案:
当您使用adapter
上下文然后访问片段管理器时,您需要投射您的context
.所以你应该用这种方式.
YourParentActivity myActivity = (YourParentActivity)context
现在您可以访问片段管理器的方法了,
myActivity.getSupportFragmentManager();
但请记住,您Fragment
应该导入,android.support.app.v4.Fragment
否则可能会出现投射问题.
如果您为特定的一个选项卡打开片段,那么您应该使用getChildSupportFragmentManager()
而不是getSupportFragmentManager()
注意:如果fragment
要从adapter
类调用,则应创建interface
并将侦听器传递给按钮单击方法,并使用该接口实现您的活动.
更新:
您也可以传递FragmentManager
给适配器构造函数.喜欢,
public FragmentManager f_manager; public CardAdapter(Context context,TaskFragment ma , FragmentManager f_manager) { this.context = context; this.f_manager = f_manager; main=ma; }
之后,您可以this.f_manager
在适配器类getView()
方法中使用.
在你的onclick中打开如下所示的新片段
@Override public void onClick(View view){ AppCompatActivity activity = (AppCompatActivity) view.getContext(); Fragment myFragment = new MyFragment(); activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit(); }
问题:
Error:cannot find symbol method getSupportFragmentManager()
方案:
当您使用adapter
上下文然后访问片段管理器时,您需要投射您的context
.所以你应该用这种方式.
YourParentActivity myActivity = (YourParentActivity)context
现在您可以访问片段管理器的方法了,
myActivity.getSupportFragmentManager();
但请记住,您Fragment
应该导入,android.support.app.v4.Fragment
否则可能会出现投射问题.
如果您为特定的一个选项卡打开片段,那么您应该使用getChildSupportFragmentManager()
而不是getSupportFragmentManager()
注意:如果fragment
要从adapter
类调用,则应创建interface
并将侦听器传递给按钮单击方法,并使用该接口实现您的活动.
更新:
您也可以传递FragmentManager
给适配器构造函数.喜欢,
public FragmentManager f_manager; public CardAdapter(Context context,TaskFragment ma , FragmentManager f_manager) { this.context = context; this.f_manager = f_manager; main=ma; }
之后,您可以this.f_manager
在适配器类getView()
方法中使用.
如果您使用了支持库片段或默认片段,请务必在每个位置使用相同的片段.并仔细使用"getSupportFragmentManager"或"getFragmentManager".
public CardAdapter(Context context) { super(); mItems = new ArrayList(); NatureItem nature = new NatureItem(); nature.setName("The Paris Attack 2015"); nature.setDes("Lorem ipsum dolor sit amet."); nature.setThumbnail(R.drawable.news1); mItems.add(nature); }
....Your Code ShareFragment newFragment = new ShareFragment(); FragmentTransaction transaction = /* Here is the change.*/context.getFragmentManager().beginTransaction(); transaction.replace(R.id.viewFragments, newFragment); ...Your Code
Inside onClick call mainActivity setFragment method to replace fragment. ((MainActivity) context).setFragment(yourFragment); public void setFragment(Fragment frag){ FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.viewFragments, frag); }