gpt4 book ai didi

java - 如何获取适配器创建的所有 View 的实例?

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:07 25 4
gpt4 key购买 nike

我有一个 ArrayAdapter对于 GridView ,正如您在代码中看到的那样,我填充了 GridView使用 getView带有自定义 View 的方法称为:GridViewCell .

现在单击此自定义 View 会出现一个 FlipAnimation方法运行非常适合单击的 View 。我想要实现的是为适配器中当前所有其他自定义 View 运行相同的动画方法,因此我需要获取所有其他 View 的实例(适配器中有 4 个 View ,因此我需要获取实例其他 3 个)。

public class AnswersGridAdapter extends ArrayAdapter<Answer>
{
private static final String TAG = AnswersGridAdapter.class.getSimpleName();
private ArrayList<Answer> answersList;
private Context context;
private MainActivity activity;
private QuestionGridFragment fragment;
private View view;

public AnswersGridAdapter(QuestionGridFragment fragment, Context context, int textViewResourceId, ArrayList<Answer> answersList)
{
super(context, textViewResourceId, answersList);
this.answersList = new ArrayList<Answer>();
this.answersList.addAll(answersList);
this.context = context;
this.activity = (MainActivity)context;
this.fragment = fragment;
}

public View getView(final int position, View convertView, ViewGroup parent)
{
final View view = new GridViewCell(context);

if (answersList.get(position).isChecked())
{
((GridViewCell)view).FlipToBackWithoutAnimation();
}

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
((GridViewCell)view).FlipAnmiation();
if (answersList.get(position).isChecked())
{
answersList.get(position).setChecked(false);
}
else
{
answersList.get(position).setChecked(true);
}

activity.getEstimatedCostTextView().setText(String.valueOf(activity.getEstimatedCost()));
fragment.flipOtherChosenAnswer(position);
}
});

((GridViewCell)view).setTextToTitleTextView(answersList.get(position).getAnswerText());
return view;
}
}

我正在尝试做这样的事情:

public void flipOtherChosenAnswer(int position) 
{
int cellCount = answersAdapter.getCount();
for (int i=0; i < cellCount; i++ )
{
((GridViewCell)answersAdapter.getView(i, null, null)).FlipAnmiation();
}
}

但这对我没有帮助。如何做到这一点?

最佳答案

如果您管理 onItemClick 中的点击,第一个参数应该是 AdapterView,并且相信您可以访问它的子项。

int count = adpaterViewInstance.getChildCount();
for (int i = 0; i < count; i++) {
View childAt = adpaterViewInstance.getChildAt(i);
}

编辑:

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adpaterViewInstance, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});

关于java - 如何获取适配器创建的所有 View 的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19616427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com