这是我的Recycler视图适配器类,我希望只需单击一下即可编程上下文菜单。我希望这样的原因是我可以直接从文本字段中提取信息,在这里使用它们在上下文菜单中。
public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppViewHolder>
{
private List<UsageStats> appList;
public static HashMap<String,Boolean> checkAppList ;
private static Context context;
private PackageManager packageManager;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AppAdapter(List<UsageStats> appList, Context context) {
this.appList = appList;
this.context = context;
packageManager = context.getPackageManager();
checkAppList = new HashMap<>();
for(UsageStats us: appList){
checkAppList.put(us.getPackageName(), false);
}
Log.i("Hello", checkAppList+"");
}
@Override
public int getItemCount() {
return appList.size();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onBindViewHolder(final AppViewHolder appViewHolder, int i) {
UsageStats ai = appList.get(i);
if (ai != null) {
try {
appViewHolder.appName.setText(packageManager.getApplicationLabel(packageManager.getApplicationInfo(ai.getPackageName(), 0)));
appViewHolder.appDomain.setText(dateFromMiliSeconds(ai.getLastTimeUsed()));
appViewHolder.appIcon.setImageDrawable(packageManager.getApplicationIcon(ai.getPackageName()));
appViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("Hello", "");
if (appViewHolder.checkBox.isChecked()) {
appViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#FFD4DFFF"));
} else {
appViewHolder.cardView.setCardBackgroundColor(Color.WHITE);
}
}
});
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
//
//
//
public String dateFromMiliSeconds(long mili){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(mili);
return ("Last Used :: "+sdf.format(calendar.getTime()));
}
@Override
public AppViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_row, viewGroup, false);
return new AppViewHolder(itemView, context);
}
//Touch methods
//touch finishes
public static class AppViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
protected TextView appName;
protected TextView appDomain;
protected ImageView appIcon;
protected CheckBox checkBox;
protected Context context;
protected CardView cardView;
public AppViewHolder(final View vi, final Context context) {
super(vi);
appName = (TextView) vi.findViewById(R.id.app_name);
appDomain = (TextView) vi.findViewById(R.id.app_paackage);
appIcon = (ImageView) vi.findViewById(R.id.app_icon);
checkBox = (CheckBox) vi.findViewById(R.id.selectMe);
cardView = (CardView) vi.findViewById(R.id.card_view);
this.context = context;
vi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
vi.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, final View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderIcon(appIcon.getDrawable());
menu.setHeaderTitle(appName.getText());
menu.add(0, v.getId(), 0, "WhatsApp").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(v.getContext(), "Post to whatsapp", Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
}