如何获取适配器创建的所有视图的实例?

时间:2013-10-27 09:17:52

标签: java android gridview instance android-arrayadapter

ArrayAdapter我有一个GridView,正如您在代码中看到的那样,我使用GridView方法使用getView方法填充GridViewCell并调用自定义视图:{ {1}}。

现在点击此自定义视图,有一个FlipAnimation方法运行,对于单击的视图非常有用。我想要实现的是为适配器中当前的所有其他自定义视图运行相同的动画方法,所以我需要获取所有其他视图的实例(适配器中有4个视图,所以我需要获取实例其他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();
    }
}

但这对我没有帮助。怎么办呢?

2 个答案:

答案 0 :(得分:3)

如果你管理点击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();
        }
    });

答案 1 :(得分:0)

这就是我最终做的,从包含我运行此方法的GridView的片段中获取GridView的所有子节点并在每个节点上运行动画方法。

public void flipOtherChosenAnswer(int position) 
{
    int cellCount = gvQuestionAnswers.getChildCount();
    for (int i=0; i < cellCount; i++ )
    {
        if (i != position && qustionAnswersList.get(i).isChecked())
        {
            ((GridViewCell)gvQuestionAnswers.getChildAt(i)).FlipAnmiation();
        }
    }
}