如何从活动中更改GridView(按钮适配器)中不可见按钮的背景(How to change background of non

系统教程 行业动态 更新时间:2024-06-14 17:03:52
如何从活动中更改GridView(按钮适配器)中不可见按钮的背景(How to change background of non-visible buttons in GridView (Button adapter) from an activity)

我的代码仅适用于可见视图。

mGridView = (GridView) findViewById(R.id.gridView); mGridView.setAdapter(new ButtonAdapter(this, list));

计时器滴答时调用的方法:

public void setBackground(int i) { Button button = (Button) mGridView.getChildAt(i); button.setBackgroundResource(R.drawable.button_shape);

此方法导致NPE,因为getChildAt无法访问不可见的子级。 我尝试了一些解决方案,但到目前为止没有运气( android - listview按位置获取项目视图 - 此解决方案没有导致NPE但是在一个刻度中更改了更多按钮的背景)

我需要的是更改第一个刻度线上的第一个按钮背景,第二个刻度线上的第二个按钮以及最后一个刻度线上的最后一个按钮,并在滚动时保持一致。

我在ButtonAdapter中的getView方法:

public View getView(final int position, View convertView, ViewGroup parent) { Button btn; LayoutInflater inflater = LayoutInflater.from(mContext); if (convertView == null) { // if it's not recycled, initialize some attributes btn = (Button) inflater.inflate(R.layout.button, parent, false); int width = mContext.getResources().getDimensionPixelSize(R.dimen.gridView_param_width); int height = mContext.getResources().getDimensionPixelSize(R.dimen.gridView_param_height); GridView.LayoutParams params = new GridView.LayoutParams(width, height); btn.setLayoutParams(params); } else { btn = (Button) convertView; } btn.setText(list.get(position).getName()); btn.setId(position); btn.setTag(position); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SetGridViewListener activity = (SetGridViewListener) mContext; activity.onClickGridView(position); Button btn = (Button)v; btn.setBackgroundResource(R.drawable.button_shape_clicked); btn.setClickable(false); } }); return btn; }

我认为我的问题在于getView方法,这可能不适合我的目的。 提前致谢。

My code works only for visible views.

mGridView = (GridView) findViewById(R.id.gridView); mGridView.setAdapter(new ButtonAdapter(this, list));

Method called when chronometer ticks:

public void setBackground(int i) { Button button = (Button) mGridView.getChildAt(i); button.setBackgroundResource(R.drawable.button_shape);

This method causes NPE because getChildAt cannot access non-visible childs. I tried some solutions found here but no luck so far (android - listview get item view by position - this solution did not causes NPE but was changing background for more buttons in one tick)

What I need is to change first button background on first tick, second button on second tick.. and last button on last tick and stay consistent while scrolling.

My getView method in ButtonAdapter:

public View getView(final int position, View convertView, ViewGroup parent) { Button btn; LayoutInflater inflater = LayoutInflater.from(mContext); if (convertView == null) { // if it's not recycled, initialize some attributes btn = (Button) inflater.inflate(R.layout.button, parent, false); int width = mContext.getResources().getDimensionPixelSize(R.dimen.gridView_param_width); int height = mContext.getResources().getDimensionPixelSize(R.dimen.gridView_param_height); GridView.LayoutParams params = new GridView.LayoutParams(width, height); btn.setLayoutParams(params); } else { btn = (Button) convertView; } btn.setText(list.get(position).getName()); btn.setId(position); btn.setTag(position); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SetGridViewListener activity = (SetGridViewListener) mContext; activity.onClickGridView(position); Button btn = (Button)v; btn.setBackgroundResource(R.drawable.button_shape_clicked); btn.setClickable(false); } }); return btn; }

I think my problem is in getView method which is probably not recycling well for my purpose. Thanks in advance.

最满意答案

我已经解决了。 我使用ViewHolder模式,就像这里ListView子对象可点击的confilct和这个方法来访问我的活动按钮:

public View getViewByPosition(int pos, GridView gridView) { final int firstListItemPosition = listView.getFirstVisiblePosition(); final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; if (pos < firstListItemPosition || pos > lastListItemPosition ) { return gridView.getAdapter().getView(pos, null, listView); } else { final int childIndex = pos - firstListItemPosition; return gridView.getChildAt(childIndex); } }

( android - listview按位置获取项目视图 )

如果您想要我的具体解决方案,请写下评论,我会添加它。

I have solved it already. I used ViewHolder pattern like here ListView subobject clickable confilct and this method to access buttons from my activity:

public View getViewByPosition(int pos, GridView gridView) { final int firstListItemPosition = listView.getFirstVisiblePosition(); final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; if (pos < firstListItemPosition || pos > lastListItemPosition ) { return gridView.getAdapter().getView(pos, null, listView); } else { final int childIndex = pos - firstListItemPosition; return gridView.getChildAt(childIndex); } }

(android - listview get item view by position)

If you would like my concrete solution, write in comment and I will add it.

更多推荐

本文发布于:2023-04-24 12:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/c319ae0986a558ea26a849c1be298fc5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   适配器   中不   背景   活动中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!