对于该示例,使用以下图像,第一个图像表示我的简单GrIDVIEw,其中button在Actionbar中.当我按下按钮时,SubMenu会显示三行,例如:“All”,“Open”& “关闭”.当我按下“打开”按钮时,我只想显示“打开”类别的项目:
我发现了搜索Editext的几个东西,其中包含了Filterable,但我不认为这是实现我想要的好方法.我不需要TextVIEw / EditText作为过滤器,只需要一个Actionbar中的button.
更新:
注意:我在使用@ana01’s answer修改后更新了我的适配器.
似乎getVIEw()被调用太多了.我添加了3个整数来计算具有类别的项目数(nValues = total,nopen = nb的Open item,nClosed = nb of Closed item).我使用notifyDataSetChanged()来更新我的适配器.
这是我对BaseAdapter的活动:
public class MainActivity extends SherlockActivity { Actionbar actionbar; static GrIDVIEw grIDvIEw; static MyAdapter adapter; String[] values = new String[] { "Item 1","Item 2","Item 3","Item 4","Item 5" }; // 1 for Open items,2 for Closed items int[] vstatus = new int[] { 1,2,1,1 }; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); grIDvIEw = (GrIDVIEw) findVIEwByID(R.ID.grID); adapter = new MyAdapter(this); grIDvIEw.setAdapter(adapter); grIDvIEw.setonItemClickListener(new OnItemClickListener() { // new Intent to another Activity // ... }); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.main,menu); return true; } // call an adapter method filterVIEw() // with the integer sort by category // 0 = All items | 1 = Open | 2 = Closed @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { switch (item.getItemID()) { case androID.R.ID.home: return false; case R.ID.action_Listfilter_all : adapter.filterVIEw(0); return true; case R.ID.action_Listfilter_open : adapter.filterVIEw(1); return true; case R.ID.action_Listfilter_closed : adapter.filterVIEw(2); return true; default: return super.onoptionsItemSelected(item); } } public class MyAdapter extends BaseAdapter { private Context mContext; // Initialize the category's integer "visibleFlag" private int visibleFlag = 0; // Initialize the counters' categorIEs int nValues,nopen,nClosed; public MyAdapter(Context c) { mContext = c; } // filterVIEw method called by option selected item menu public voID filterVIEw(int i) { visibleFlag = i; // refresh the content notifyDataSetChanged(); /* grIDvIEw.invalIDateVIEws(); */ } protected class VIEwHolder { TextVIEw text,vIEw,like,user,coms; ImageVIEw imagevIEw,imageflag; } // return the number of items regarding by category selected public int getCount() { switch(visibleFlag) { case 0: nValues = values.length; break; case 1: nValues = nopen; break; case 2: nValues = nClosed; break; } return nValues; } public Object getItem(int position) { return null; } public long getItemID(int position) { return position; } public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEwHolder vIEwHolder; if (convertVIEw == null) { LayoutInflater inflater = ((MainActivity) mContext).getLayoutInflater(); convertVIEw = inflater.inflate(R.layout.item_main,parent,false); vIEwHolder = new VIEwHolder(); vIEwHolder.text = (TextVIEw) convertVIEw.findVIEwByID(R.ID.text); vIEwHolder.imagevIEw = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.image); // set the tag of the category and // augment the selected category (by + 1) switch(vstatus[position]) { case 1: vIEwHolder.imagevIEw.setTag(1); nopen++; break; case 2: vIEwHolder.imagevIEw.setTag(2); nClosed++; break; } // set the tag of the item's position vIEwHolder.text.setTag(position); convertVIEw.setTag(vIEwHolder); } else { vIEwHolder = (VIEwHolder) convertVIEw.getTag(); } // display the text with the position's tag vIEwHolder.text.setText(values[(Integer) vIEwHolder.text.getTag()]); // display the image with the position's tag switch((Integer) vIEwHolder.imagevIEw.getTag()) { case 1: vIEwHolder.imagevIEw.setimageResource(R.drawable.ic_open); break; case 2: vIEwHolder.imagevIEw.setimageResource(R.drawable.ic_closed); break; } return convertVIEw; } }}
但是这不会显示所选类别的正确项目!
有人可以帮我搞清楚吗?
我不认为您需要AsynkTask来刷新您的集合,只需修改您的适配器以了解项类型:当设置过滤器时(一个表示3种状态之一的整数:全部,关闭,打开),您的适配器应报告正确的计数并返回正确的项目(您可以使用3个不同的项目集合,但这意味着重复您的数据).
grIDVIEw.notifyDataChanged()在您的适配器中触发一个新的getCount()调用,然后多次调用getVIEw(..). grIDVIEw.invalIDateVIEws()表示需要重绘子视图.您不需要重新创建grIDVIEw.
如果您的项目看起来与您绘制的项目类似,我认为您可以使用ListView(使用相同的适配器).
更新:
如果getCount()被多次调用是可以的,但是你应该避免在这种方法中执行繁重的任务(目前不是这种情况).
在getVIEw(..)中,无论是否重用以前创建的视图,都应该提供与相应位置完全同步的视图.所以问题是你只是在新创建时才更新视图(convertVIEw == null),尽管你应该更新标题的文本和CO.在视图返回之前.
与您的“鬼项目”相关,我不知道您的布局项目有多大,如果它们一次可见等,但您的适配器应创建至少与可见的视图一样多的视图.
UPDATE2:
修改你的适配器,如下所示,在适配器的构造函数中调用prepareData(),看看它是否正常工作:
private ArrayList<Integer> closedindexToRealindex;private ArrayList<Integer> openedindexToRealindex;private voID prepareData(){ nopen = nClosed = 0; closedindexToRealindex = new ArrayList<Integer>(); openedindexToRealindex = new ArrayList<Integer>(); for(int i = 0; i < values.count; i++){ if(vstatus[i] == 0){ openedInexToRealindex.add(Integer.valueOf(i)); nopen++; } else{ closedindexToRealindex.add(Integer.valueOf(i)); nClosed++; } }}public VIEw getVIEw(int position,false); vIEwHolder = new VIEwHolder(); vIEwHolder.text = (TextVIEw) convertVIEw.findVIEwByID(R.ID.text); vIEwHolder.imagevIEw = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.image); convertVIEw.setTag(vIEwHolder); } else { vIEwHolder = (VIEwHolder) convertVIEw.getTag(); } int actualindex = 0; switch(visibleFlag){ case 0://all actualindex = position; break; case 1://opened actualindex = openedindexToRealindex.get(position).intValue(); break; case 2://closed actualindex = closedindexToRealindex.get(position).intValue(); break; default: break; } vIEwHolder.text.setText(values[acutalindex]); if(vstatus[position] == 1) vIEwHolder.imagevIEw.setimageResource(R.drawable.ic_open); else vIEwHolder.imagevIEw.setimageResource(R.drawable.ic_closed); return convertVIEw; }总结
以上是内存溢出为你收集整理的Android中带有过滤器菜单按钮的GridView全部内容,希望文章能够帮你解决Android中带有过滤器菜单按钮的GridView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)