如何在适配器中使用 getFragmentManager()
Posted
技术标签:
【中文标题】如何在适配器中使用 getFragmentManager()【英文标题】:How use getFragmentManager() in adapter 【发布时间】:2018-05-09 22:56:55 【问题描述】:。如何在适配器中使用此代码? (我在 SecondFragment 中使用此代码)。
Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("OneFragment");
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
而且适配器中的这种方式不起作用:
Fragment frg = null;
frg = ((Activity) context).getFragmentManager().findFragmentByTag("OneFragment");
final FragmentTransaction ft = ((Activity) context).getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
并向我显示错误:
Required:
android.support.v4.app.Fragment
Found:
android.app.FragmentManage
在我的适配器中(我需要在我的适配器中使用getFragmentManager()
。):
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.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import me.bazaarche.Fragment.FilterFragment;
import me.bazaarche.Fragment.HomeFragment;
import me.bazaarche.app.R;
public class CategorySub1Adapter extends RecyclerView.Adapter<CategorySub1Adapter.ViewHolder>
private Context context;
private String[] categorySub1Name;
int categoryPosition;
int categorySub0Position;
FragmentManager fragmentManager;
public CategorySub1Adapter(Context context, FragmentManager fragmentManager, String[] categorySub1Name, int categoryPosition, int categorySub0Position)
this.context = context;
this.categorySub1Name = categorySub1Name;
this.categoryPosition = categoryPosition;
this.categorySub0Position = categorySub0Position;
this.fragmentManager = fragmentManager;
class ViewHolder extends RecyclerView.ViewHolder
View categorySub1Menu;
TextView categorySub1Name;
ViewHolder(View itemView)
super(itemView);
categorySub1Menu = itemView.findViewById(R.id.category_sub0_menu);
categorySub1Name = itemView.findViewById(R.id.category_sub1_name);
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_category_sub1, parent, false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(final ViewHolder holder, final int position)
holder.categorySub1Name.setText(categorySub1Name[position]);
Animation animation = AnimationUtils.loadAnimation(context, R.anim.open_menu);
animation.setStartOffset(30 * position);
holder.itemView.startAnimation(animation);
HomeFragment.adAdapter.ClearList();
HomeFragment.last_ad_id = 0;
FilterFragment.categoryPosition = categoryPosition;
FilterFragment.categorySub0Position = categorySub0Position;
FilterFragment.categorySub1Position = position;
HomeFragment.adAdapter.LoadMore();
Fragment frg = null;
frg = fragmentManager.getFragmentManager().findFragmentByTag("OneFragment");
final FragmentTransaction ft = fragmentManager.getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
@Override
public int getItemCount()
return categorySub1Name.length;
我需要在我的适配器中使用getFragmentManager()
。
【问题讨论】:
【参考方案1】:将您的导入更改为:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
尝试使用此更改Framgnet
:
Fragment frg = null;
frg = ((Activity) context).getSupportFragmentManager().findFragmentByTag("OneFragment");
final FragmentTransaction ft = ((Activity) context).getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
【讨论】:
适配器getActivity()
不可用
在适配器中:this.context.
之后,getSupportFragmentManager()
不可用。
向我展示你的导入,并检查你是否有我提供给你的导入,谢谢。
getFragmentManager()
不适用于this.context.
我打错了...改成((Activity) context)
【参考方案2】:
CategorySub1Adapter 构造函数的变化。
public CategorySub1Adapter(Context context, FragmentManager fragmentManager, String[] categorySub1Name, int categoryPosition, int categorySub0Position)
this.context = context;
this.categorySub1Name = categorySub1Name;
this.categoryPosition = categoryPosition;
this.categorySub0Position = categorySub0Position;
this.fragmentManager = fragmentManager;
到
private Context context;
private Activity mActivity;
public CategorySub1Adapter(Activity mActivity, FragmentManager fragmentManager, String[] categorySub1Name, int categoryPosition, int categorySub0Position)
this.mActivity = mActivity;
this.categorySub1Name = categorySub1Name;
this.categoryPosition = categoryPosition;
this.categorySub0Position = categorySub0Position;
this.fragmentManager = fragmentManager;
尝试改变
mYourAdapter = new CategorySub1Adapter(getActivity(),........);
并改变它
Fragment frg = null;
frg = mActivity.getSupportFragmentManager().findFragmentByTag("OneFragment");
final FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
如果你想要上下文
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_category_sub1, parent, false);
// get context
context=parent.getContext();
return new ViewHolder(view);
【讨论】:
【参考方案3】:在onBindViewHolder()
方法内
你可以使用这个 kotlin 代码的 java 等价物
val fm=(holder.itemView.context as FragmentActivity).supportFragmentManager
【讨论】:
请阅读How to answer并更新您的答案。以上是关于如何在适配器中使用 getFragmentManager()的主要内容,如果未能解决你的问题,请参考以下文章
如何在适配器中使用 getFragmentManager()