如何在适配器

时间:2017-11-26 16:37:03

标签: android fragment adapter fragmentmanager

如何在适配器中使用getFragmentManager()。如何在适配器中使用此代码? (我在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()

3 个答案:

答案 0 :(得分:2)

将导入更改为:

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();

答案 1 :(得分:0)

onBindViewHolder() 方法中 你可以使用相当于这个 kotlin 代码的 java

val fm=(holder.itemView.context as FragmentActivity).supportFragmentManager

答案 2 :(得分:0)

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);
}