https://github.com/lorensiuswlt/NewQuickAction |
经过一晚上的测试楼主最后总算是可以动态更改了。根本方法就是把以前写在OnCreate的如下代码全部放到了listview Adapter的getView 里面实现。
之前是这么写的(已经注释掉了)
public class MainActivity extends Activity { private int mSelectedRow = 0; private MyAdapter myAdapter; private ListView listview; public ImageView mMoreIv = null; CursorLoader loader=null; public QuickAction mQuickAction =null; public QuickAction getmQuickAction() { return mQuickAction; } public void setmQuickAction(QuickAction mQuickAction) { this.mQuickAction = mQuickAction; } public List<QuickAction> quickList=null; private List<Map<String,Object>> list=null; ActionBar actionBar=null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Set up the action bar. actionBar = (ActionBar) findViewById(R.id.actionbar); actionBar .setTitle("首页"); // actionBar.setHomeAction(new IntentAction(this, createIntent(this), R.drawable.ic_title_home_demo)); //使用loagermanager //这个以后写到收藏activity里 /* LoaderManager loadermanager=getLoaderManager(); loadermanager.initLoader(1000, null, callback);*/ //------------->>>>>>> mQuickAction = new QuickAction(this); listview=(ListView) findViewById(R.id.listview1); registerForContextMenu(listview); new Mytask().execute("") ;//服务器地址 或得json 包括 logo 的url,shopname, 地址 还有 视频的url /* // fu chuang ActionItem addItem = new ActionItem(1, "", getResources().getDrawable(R.drawable.ic_add)); ActionItem addItem1 = new ActionItem(1, "Add", getResources().getDrawable(R.drawable.ic_add)); ActionItem addItem2 = new ActionItem(1, "Add", getResources().getDrawable(R.drawable.ic_add)); ActionItem addItem3 = new ActionItem(1, "Add", getResources().getDrawable(R.drawable.ic_add)); ActionItem addItem4 = new ActionItem(1, "Add", getResources().getDrawable(R.drawable.ic_add)); mQuickAction.addActionItem(addItem); mQuickAction.addActionItem(addItem1); mQuickAction.addActionItem(addItem2); mQuickAction.addActionItem(addItem3); mQuickAction.addActionItem(addItem4);*/ //setup the action item click listener /*mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { @Override public void onItemClick(QuickAction quickAction, int pos, int actionId) { ActionItem actionItem = quickAction.getActionItem(pos); Toast.makeText(getApplicationContext(), actionItem.getTitle() + " item selected on row " + mSelectedRow, Toast.LENGTH_SHORT).show(); Intent i = new Intent(); i.setClass(MainActivity.this, MvplayActivity123.class); String mv_url=(String) list.get(mSelectedRow).get("mv_url"); i.putExtra("mv_url", mv_url); startActivity(i); } }); //setup on dismiss listener, set the icon back to normal mQuickAction.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { mQuickAction.deleteActionItem(); mMoreIv.setImageResource(R.drawable.ic_list_more); } });*/ //fangdao adpter shixian /* listview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mSelectedRow = position; //set the selected row mQuickAction.show(view); //change the right arrow icon to selected state mMoreIv = (ImageView) view.findViewById(R.id.item_list_guankan); mMoreIv.setImageResource(R.drawable.ic_list_more_selected); } });*/
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; final MainActivity ma=(MainActivity) context; final QuickAction mQuickAction=new QuickAction(context); final int pos=position; final Button btcollect; //@@@@@@@@@@@@@@@@@@@@@@@2 在这里控制个数(关键) for(int n=0;n<pos+1;n++){ ActionItem addItem5 = new ActionItem(1, String.valueOf(pos), context.getResources().getDrawable(R.drawable.ic_add)); mQuickAction.addActionItem(addItem5);} if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate( R.layout.item_list, null); holder.logo = (ImageView) convertView .findViewById(R.id.item_list_logo); holder.text_bellow = (TextView) convertView .findViewById(R.id.item_list_text_bellow); holder.text_top = (TextView) convertView .findViewById(R.id.item_list_text_top); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } {btcollect = (Button) convertView .findViewById(R.id.collect_listbutton); btcollect.setTag((String) list.get(position).get("logo_url")); } // 为每一个view项设置触控监听 convertView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { final ViewHolder holder = (ViewHolder) v.getTag(); TranslateAnimation mShowAction = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); mShowAction.setDuration(500); mHiddenAction = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 2.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); mHiddenAction.setFillAfter(true); mHiddenAction.setDuration(500); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 设置背景为选中状态 // v.setBackgroundResource(android.R.color.holo_blue_light); // 获取按下时的x轴坐标 x = event.getX(); // 判断之前是否出现了删除按钮如果存在就隐藏 if (curDel_btn != null) { curDel_btn.startAnimation(mHiddenAction); // curDel_btn.setVisibility(View.GONE); curDel_btn = null; // curAdd_btn.setVisibility(View.GONE); } break; case MotionEvent.ACTION_UP: // 设置背景为未选中正常状态 //v.setBackgroundResource(android.R.color.background_light); // 获取松开时的x坐标 ux = event.getX(); onclick_flag = true; // 判断当前项中按钮控件不为空时 if (btcollect != null) { // 按下和松开绝对值差当大于20时显示删除按钮,否则不显示 if (x - ux > 12) { btcollect.startAnimation(mShowAction); btcollect.setVisibility(View.VISIBLE); // holder.addbut.setVisibility(View.VISIBLE); curDel_btn = btcollect; // curAdd_btn= holder.addbut; onclick_flag = false; } } break; default: // v.setBackgroundResource(R.drawable.mm_listitem_simple); break; } return false; } }); mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { @Override public void onItemClick(QuickAction quickAction, int pos, int actionId) { ActionItem actionItem = quickAction.getActionItem(pos); Toast.makeText(context, actionItem.getTitle() + " item selected on row " + ma.getmSelectedRow(), Toast.LENGTH_SHORT).show(); /* Intent i = new Intent(); i.setClass(context, MvplayActivity123.class); String mv_url=(String) list.get(ma.getmSelectedRow()).get("mv_url"); i.putExtra("mv_url", mv_url); ma.startActivity(i);*/ } }); //setup on dismiss listener, set the icon back to normal mQuickAction.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { // mQuickAction.deleteActionItem(); ma.getmMoreIv().setImageResource(R.drawable.ic_list_more); } }); // llllllllllllllll wei fuchuang fuzhi convertView.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (onclick_flag) { ma.setmSelectedRow(pos); // set the selected row mQuickAction.show(v); // change the right arrow icon to selected state ma.mMoreIv = (ImageView) v .findViewById(R.id.item_list_guankan); ma.mMoreIv.setImageResource(R.drawable.ic_list_more_selected); } } }); btcollect.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(curDel_btn!=null) curDel_btn.setVisibility(View.GONE); curDel_btn=null; //################ collect Shop shop=new Shop(); shop.setAddress(((Map) list.get(pos)).get("address").toString()); shop.setLogo_url(((Map) list.get(pos)).get("logo_url").toString()); shop.setMsg(((Map) list.get(pos)).get("msg").toString()); shop.setMv_url(((Map) list.get(pos)).get("mv_url").toString()); shop.setShopname(((Map) list.get(pos)).get("shopname").toString()); String shop_id = ((Map) list.get(pos)).get("shop_id") .toString(); shop.setShop_id(shop_id); if(shop_id==null) Toast.makeText(context,"收藏失败", Toast.LENGTH_SHORT).show(); if(!(new DBManager(context).exits(shop_id))){ save2db(shop); } else{ Toast.makeText(context,"已收藏", Toast.LENGTH_SHORT).show(); } //#################### } private void save2db(Shop shop) { ContentResolver resolver=context.getContentResolver(); ContentValues values =new ContentValues(); // System.out.println("baocun11前--------"); values.put("shopname",shop.getShopname()); values.put("address",shop.getAddress()); values.put("mv_url",shop.getMv_url()); values.put("logo_url",shop.getLogo_url()); values.put("msg",shop.getMsg()); values.put("shop_id",shop.getShop_id()); Uri url=Uri.parse("content://fang.ContentProvider.MyContentProvider_media/"+ChangLiang.TABLE); resolver.insert(url, values); // System.out.println("baocun11成功-----------"); Toast.makeText(context,"收藏成功", Toast.LENGTH_SHORT).show(); } }); holder.text_top.setText((String) list.get(position).get("shopname")); holder.text_bellow.setText((String) list.get(position).get("address")); // 给 ImageView 设置一个 tag btcollect.setTag((String) list.get(position).get("logo_url")); holder.logo.setTag((String) list.get(position).get("logo_url")); final String logo_url = (String) list.get(position).get("logo_url"); if (logo_url != null && !logo_url.equals("")) { Bitmap bitmap = imageLoader.loadImage(holder.logo, logo_url, new ImageDownloadCallBack() { @Override public void onImageDownloaded(ImageView imageView, Bitmap bitmap) { // 通过 tag 来防止图片错位 if (imageView.getTag() != null && imageView.getTag().equals(logo_url)) { imageView.setImageBitmap(bitmap); } } }); if (bitmap != null) { holder.logo.setImageBitmap(bitmap); } } return convertView;