将图像从一个适配器传递到另一个适配器

时间:2017-11-26 19:44:14

标签: java android android-fragments android-adapter android-gridview

我的mainScreen片段上有一个gridView,它包含由我的ImageAdapter.java类初始化的imageView。我还有另一个片段,它也有一个gridView,它遵循相同的初始化过程。我想要的是将选定的imaveView图标(存储在Integer []数组中)传递给另一个片段的gridView。

以下是我的课程:

MainFragment.java [处理图像传递的部分]:

  // When an item in the context menu gets selected, call a method
@Override
public boolean onContextItemSelected(MenuItem item) {

    // Get some extra info about the contextMenu
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    int position = info.position; // clicked view's position

    if(item.getTitle().equals("Add Card to GLB")) {
        addCardMessage(position, "added to GLB");
        addSelectedCardToGlobalUserBox(position);
    } else if (item.getTitle().equals("Add Card to JP")) {
        addCardMessage(position , "added to JP");
    } else
    {
        return false;
    }
    return false;
}

/**
 * Creates a snackbar message, telling the user which card was added to which box
 * @param id The position of the chosen card
 * @param text Defines into which User Box the card was added
 */
private void addCardMessage(int id, String text) {
      final Snackbar snackbar = Snackbar.make(gridView, id + " " + text ,Snackbar.LENGTH_LONG);

      snackbar.setAction("Dismiss", new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            snackbar.dismiss();
        }
    });
    snackbar.setActionTextColor(Color.MAGENTA);
    snackbar.show();
}


private void addSelectedCardToGlobalUserBox(int position) {
    ImageAdapter imageAdapter = new ImageAdapter(getContext());
    UserBoxGlbImageAdapter userBoxGlbImageAdapter = new UserBoxGlbImageAdapter(getContext());
    userBoxGlbImageAdapter.getGLBIconsList().add(imageAdapter.getmThumbIds(position));
    int glbiconSize = userBoxGlbImageAdapter.getCount();
    Toast.makeText(getActivity(), "Selected icon: " + imageAdapter.getmThumbIds(position), Toast.LENGTH_SHORT).show();
}

ImageAdapter.java [上面的片段&#39适配器]:

public class ImageAdapter extends BaseAdapter {
Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    // If it's not recycled, initialize some attributes
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(225, 225));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

public Integer getmThumbIds(int index) {
    return mThumbIds[index];
}

// References to our images
private Integer[] mThumbIds = {
        R.mipmap.turvegitossj_phy,
        R.mipmap.goget_ur_int,
        R.mipmap.turgogetassj4_teq,
        R.mipmap.turgotenksssj_uragl,
        R.mipmap.lr_phy_trunks_ssj
};
}

SecondFragment.java:

public class UserBoxGLBFragment extends Fragment {

GridView globalGridView;

public UserBoxGLBFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_user_box_glb, container, false);

    globalGridView = view.findViewById(R.id.userBoxGlbGridView);
    globalGridView.setAdapter(new UserBoxGlbImageAdapter(getContext()));
    return view;
}

}

SecondFragmentAdapter [接收图像的那个]:

public class UserBoxGlbImageAdapter extends BaseAdapter {

Context mContext;

public UserBoxGlbImageAdapter(Context c) {
    mContext = c;
}

@Override
public int getCount() {
    return mGLBIcons.size();
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

// References to the images via a List
private List<Integer> mGLBIcons = new ArrayList<>();

// Used to add card icons from the mainScreenFragment
public List<Integer> getGLBIconsList() {
    return mGLBIcons;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    // If it's not recycled, initialize some attributes
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(225, 225));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mGLBIcons.get(position));
    return imageView;
}
}

到目前为止,当我长按并打开上下文菜单时,我点击将卡添加到GLB,但图像没有传递到第二个fragmnet的gridView。据我所知,我用于将imageResource添加到第二个网格的Integer无法正常工作。我需要做什么?我是否需要为收件人列表使用不同的var类型?

0 个答案:

没有答案