3

我有一个ArrayAdapterfor a GridView,正如您在代码中看到的那样,我GridView使用该getView方法填充了一个名为: 的自定义视图GridViewCell

现在单击此自定义视图时,有一个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();
    }
}

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

4

2 回答 2

4

如果您管理单击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();
        }
    });
于 2013-10-27T09:21:14.047 回答
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();
        }
    }
}
于 2013-10-27T10:10:32.730 回答