我有片段,我称之为自定义视图类(Dragger)

public class CanvasFragment extends Fragment {

    private Dragger dragger;

    public CanvasFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_canvas, container, false);

        dragger = (Dragger) rootView.findViewById(R.id.dragger);

        int[] symbolIndex = {};

        dragger.setElementsArray(symbolIndex);
        return rootView;
    }

    public void addIconToCanvas(int id){
        dragger.addIcon(id);
    }
}


我还有一个带有RecyclerView和适配器的片段,其中通过主机CanvasFragment's的活动调用addIconToCanvas()方法CanvasFragment

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

    private int[] data;
    private Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView itemView;
        public ViewHolder(View v) {
            super(v);
            itemView = (ImageView) v.findViewById(R.id.im_item);
        }
    }

    public ItemAdapter(Context context, int[] myDataset) {
        data = myDataset;
        this.context = context;
    }

    @Override
    public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.itemView.setImageResource(data[position]);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            ((MainActivity)context).addItemToCanvas(data[position]);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.length;
    }
}


我发现在dragger中旋转CanvasFragment后获得新参考,但是当我从适配器调用addIconToCanvas()时-dragger为空。

我真的不明白为什么领域得到参考,然后失去它)))

有人知道吗,这是怎么回事?

提前致谢!

附注:主持人活动

public class MainActivity extends AppCompatActivity {

    private CanvasFragment canvasFragment = new CanvasFragment();
    private ItemsFragment itemsFragment = new ItemsFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container_canvas, canvasFragment)
                .commit();
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container_items, itemsFragment)
                .commit();
        }

    }

    public void addItemToCanvas(int id){
        canvasFragment.addIconToCanvas(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

最佳答案

用线


  private CanvasFragment canvasFragment = new CanvasFragment();


每次创建Fragment时,就将Activity的新实例分配给变量canvasFragment。但是只有在savedInstanceState == null时,才将此CanvasFragment实例添加到UI中。

Fragment方法onCreateView()仅在需要显示Fragment时才被调用,因此在更改配置后,新创建的实例的拖动器保持为空。

你能做什么?

如果要将CanvasFragment实例添加到UI,则仅创建它。在savedInstanceState != null的情况下,找到已经存在的Fragment就像这样:

canvasFragment = (CanvasFragment)getSupportFragmentManager().findFragmentById(R.id.container_canvas);

10-08 17:26