如何在Gridview中设置随机位置的项目?

时间:2017-12-23 09:44:09

标签: android drag-and-drop android-adapter android-gridview

我正在开发一个启动器应用程序,我有一个疑问我想澄清一下。 如何设置GridView中随机位置的项目,当用户拖动一个应用程序并将其自动设置在任意位置时将其放入桌面。

我已经完成了大部分零件我无法设置位置,它始终将适配器设置在第一个位置。

应用列表屏幕

enter image description here

应用列表屏幕代码

AppDetailsAdapter adapter = new AppDetailsAdapter(list, longListener);
appList.setLayoutManager(new GridLayoutManager(getApplicationContext(), 4));
appList.setAdapter(adapter);

/*Inside AppDetailsAdapter*/

holder.appIconView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
    Log.d("AppsDrag", "onLongClick..." + v.getTag().toString());
    mListener.onItemLongClick(position, holder.appIconView);
    return true;
  }
});

holder.appIconView.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
      Log.d("AppsDrag", "onDrag..." + event.getAction());
      mListener.onItemDragListener(v, event);
      return true;
    }
});

public OnAppItemClickListener longListener = new OnAppItemClickListener() {
    @Override
    public void onItemLongClick(int position, View v) {
       ClipData data = ClipData.newPlainText("", "");
       View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
       v.startDrag(data, shadowBuilder, v, 0);
       v.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onItemDragListener(View v, DragEvent event) {
         mAppCardView.setVisibility(View.GONE);

        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                Log.d(TAG, "Action is DragEvent.ACTION_DRAG_STARTED");
            break;
            case DragEvent.ACTION_DRAG_ENTERED:
                Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENTERED");
            break;
            case DragEvent.ACTION_DRAG_EXITED:
                Log.d(TAG, "Action is DragEvent.ACTION_DRAG_EXITED");
            break;
            case DragEvent.ACTION_DRAG_LOCATION:
                Log.d(TAG, "Action is DragEvent.ACTION_DRAG_LOCATION ");
            break;
            case DragEvent.ACTION_DRAG_ENDED:
                Log.d(TAG, "Action is DragEvent.ACTION_DRAG_ENDED");
            break;
            case DragEvent.ACTION_DROP:
                Log.d(TAG, "ACTION_DROP event");
                DesktopApps desktopApps = new DesktopApps();
                desktopApps.setName(apps.get(mPosition).getName());
                desktopApps.setLabel(apps.get(mPosition).getLabel());
                desktopApps.setIcon(apps.get(mPosition).getIcon());
                desktopAppses.add(desktopApps);

                DesktopAppDetailsAdapter mDesktopAdapter = new 
 DesktopAppDetailsAdapter(desktopAppses, desktopListener);
                desktopList.setLayoutManager(new GridLayoutManager(getApplicationContext(), 5));
                desktopList.setAdapter(mDesktopAdapter);
             break;
            default:
              break;
    }
  }
};

桌面列表屏幕

enter image description here

您可以检查下面的图像,该项目设置为第一个位置,但在上面的图像中,我已经放在其他位置。

enter image description here

桌面列表屏幕代码

public class DesktopApps {
   public CharSequence label;
   public CharSequence name;
   public Drawable icon;

   public CharSequence getLabel() {
       return label;
   }

   public void setLabel(CharSequence label) {
       this.label = label;
   }

   public CharSequence getName() {
       return name;
   }

   public void setName(CharSequence name) {
       this.name = name;
   }

   public Drawable getIcon() {
       return icon;
   }

   public void setIcon(Drawable icon) {
       this.icon = icon;
   }
}

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

   private ArrayList<DesktopApps> mAppList;
   private MainActivity.OnDesktopAppItemClickListener mListener;

   public DesktopAppDetailsAdapter(ArrayList<DesktopApps> appList, MainActivity.OnDesktopAppItemClickListener listener) {
        this.mAppList = appList;
        this.mListener = listener;
   }

   @Override
   public DesktopAppDetailsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.adapter_desktop_app_list, parent, false);
       return new ViewHolder(itemView);
   }

   @Override
   public void onBindViewHolder(DesktopAppDetailsAdapter.ViewHolder holder, final int position) {
      holder.appIconView.setImageDrawable(mAppList.get(position).getIcon());
      holder.appNameView.setText(mAppList.get(position).getLabel());

      holder.appIconView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             mListener.onItemClick(position);
         }
      });
      holder.appIconView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            mListener.onItemLongClick(position);
            return true;
        }
    });
  }

  @Override
  public int getItemCount() {
      return mAppList.size();
  }

  public class ViewHolder extends RecyclerView.ViewHolder {
     private ImageView appIconView;
     private TextView appNameView;

     public ViewHolder(View itemView) {
         super(itemView);
         appIconView = (ImageView) itemView.findViewById(R.id.app_icon);
         appNameView = (TextView) itemView.findViewById(R.id.app_name);
     }
  }
}

请通过我的上述帖子告诉我。

由于

0 个答案:

没有答案