在onItemClick之后,Flip Animation在GridView中无法正常工作

时间:2015-01-06 07:48:31

标签: java android gridview android-animation android-adapter

我有一个网格视图。我有一个row.xml,我在Adapter类中充气。我的行xml有两个布局(一个是可见的,另一个是不可见的。)我正在做翻转动画。当我点击gridview的任何项目时,就会发生翻转动画,我正在看隐藏的布局,并隐藏那个可见的布局。 动画效果很好。但问题是,当我点击任何项目时,滚动其他项目也会翻转。所以当我意识到我没有得到具体的立场。 我尝试了以下代码。

      @Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row;

    final RecordHolder holder = new RecordHolder();

    animation1 = AnimationUtils.loadAnimation(context, R.anim.to_middle);
    animation2 = AnimationUtils.loadAnimation(context, R.anim.from_middle);

    if (convertView == null) {
        row = View.inflate(context, R.layout.row_phone_and_tablet1, null);
    } else {
        //row = View.inflate(context, R.layout.row_phone_and_tablet1, null);
        row = convertView;
    }

    holder.frontImage = (ImageView) row.findViewById(R.id.myimage_imgvw);
    holder.frontMobileName = (TextView) row.findViewById(R.id.name_txtvw);
    holder.frontPrice = (TextView) row.findViewById(R.id.price_txtvw);
    holder.backMobileName = (TextView) row
            .findViewById(R.id.tv_device_name);
    holder.descOne = (TextView) row.findViewById(R.id.tv_desc_first);
    holder.descTwo = (TextView) row.findViewById(R.id.tv_desc_second);
    holder.descThree = (TextView) row.findViewById(R.id.tv_desc_third);
    holder.descFour = (TextView) row.findViewById(R.id.tv_desc_fourth);
    holder.ccEmi = (TextView) row.findViewById(R.id.tv_price_ccemi);
    holder.financeEmi = (TextView) row.findViewById(R.id.tv_price_finance);
    holder.frontlayout = (LinearLayout) row.findViewById(R.id.frontlayout);
    holder.backlayout = (LinearLayout) row.findViewById(R.id.backlayout);
    holder.viewdetails = (Button) row.findViewById(R.id.viewdetails);
    holder.tv_front_inr = (TextView) row.findViewById(R.id.tv_front_inr);
    holder.tv_description = (TextView) row
            .findViewById(R.id.tv_description);
    holder.tv_price_ccemi_inr = (TextView) row
            .findViewById(R.id.tv_price_ccemi_inr);
    holder.tv_ccemi_price = (TextView) row
            .findViewById(R.id.tv_ccemi_price);
    holder.tv_price_finance_inr = (TextView) row
            .findViewById(R.id.tv_price_finance_inr);
    holder.tv_financeemi_price = (TextView) row
            .findViewById(R.id.tv_financeemi_price);
    holder.parentLayout = (LinearLayout) row
            .findViewById(R.id.parentLayout);




    final PhoneAndTabletItem phoneAndTableItem = data.get(position);
    holder.viewdetails.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, ViewPhoneInformation.class);
            i.putExtra("SKU_CODE", phoneAndTableItem.getSku_code());
            context.startActivity(i);
        }
    });

    // row.setTag(holder);



    holder.frontMobileName.setText(phoneAndTableItem.getModel_name());
    holder.frontPrice.setText(phoneAndTableItem.getCc_emi());
    holder.backMobileName.setText(phoneAndTableItem.getModel_name());
    holder.descOne.setText(phoneAndTableItem.getKey_feat_1());
    holder.descTwo.setText(phoneAndTableItem.getKey_feat_2());
    holder.descThree.setText(phoneAndTableItem.getKey_feat_3());
    holder.descFour.setText(phoneAndTableItem.getKey_feat_4());
    holder.ccEmi.setText(phoneAndTableItem.getCc_emi());
    holder.financeEmi.setText(phoneAndTableItem.getFinance_emi());

    holder.frontMobileName.setTypeface(tvHalvetica);
    holder.frontPrice.setTypeface(tvHalvetica);
    holder.backMobileName.setTypeface(tvHalvetica);
    holder.descOne.setTypeface(tvHalvetica);
    holder.descTwo.setTypeface(tvHalvetica);
    holder.descThree.setTypeface(tvHalvetica);
    holder.descFour.setTypeface(tvHalvetica);
    holder.ccEmi.setTypeface(tvHalvetica);
    holder.financeEmi.setTypeface(tvHalvetica);
    holder.tv_front_inr.setTypeface(tvHalvetica);
    holder.tv_description.setTypeface(tvHalvetica);
    holder.tv_price_ccemi_inr.setTypeface(tvHalvetica);
    holder.tv_ccemi_price.setTypeface(tvHalvetica);
    holder.tv_price_finance_inr.setTypeface(tvHalvetica);
    holder.tv_financeemi_price.setTypeface(tvHalvetica);

    // here we are rotating the LinearLayout...
    holder.parentLayout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            v.setTag(position);
            // Start Array Check...
            Toast.makeText(context, "onClick id: " + v.getTag(), Toast.LENGTH_SHORT).show();

            if (selected_views.contains((Integer) v.getTag())) {

                holder.frontlayout.clearAnimation();
                holder.frontlayout.setAnimation(animation1);
                holder.frontlayout.startAnimation(animation1);

                HashSet hs = new HashSet();
                hs.addAll(selected_views);
                selected_views.clear();
                selected_views.addAll(hs);
                selected_views.remove((Integer) v.getTag());
                notifyDataSetChanged();

                isBackOfCardShowing = true;
            } else {
                if (selected_views.size() == 0) {
                    holder.frontlayout.clearAnimation();
                    holder.frontlayout.setAnimation(animation1);
                    holder.frontlayout.startAnimation(animation1);
                    selected_views.add((Integer) v.getTag());
                    notifyDataSetChanged();
                    array_size = selected_views.size();
                    isBackOfCardShowing = false;
                }
                else {
                    holder.frontlayout.clearAnimation();
                    holder.frontlayout.setAnimation(animation1);
                    holder.frontlayout.startAnimation(animation1);
                    selected_views.add((Integer) v.getTag());
                    notifyDataSetChanged();
                    isBackOfCardShowing = false;
                }
            }
            // End Array Check...

            animation1.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                        if (isBackOfCardShowing) {
                            holder.frontlayout.setVisibility(View.VISIBLE);
                            holder.backlayout.setVisibility(View.GONE);
                            // holder.frontImage.setImageResource(R.drawable.accesories_dashboard);
                        } else {
                            holder.backlayout.setVisibility(View.VISIBLE);
                            holder.frontlayout.setVisibility(View.GONE);
                            // holder.frontImage.setImageResource(R.drawable.vas_dashboard);
                        }
                        holder.frontlayout.clearAnimation();
                        holder.frontlayout.setAnimation(animation2);
                        holder.frontlayout.startAnimation(animation2);
                }
            });
        }
    });
    holder.parentLayout.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            String inserted="";
            final String SKV_CODE = phoneAndTableItem.getSku_code().toString();
            handler.open();
                inserted = handler.insertData(phoneAndTableItem.getModel_name(),    
                                              phoneAndTableItem.getKey_feat_1(),
                                              phoneAndTableItem.getKey_feat_2(),
                                              phoneAndTableItem.getKey_feat_3(),    
                                              phoneAndTableItem.getKey_feat_4(),
                                              phoneAndTableItem.getCc_emi(),        
                                              phoneAndTableItem.getFinance_emi(),
                                              SKV_CODE);
                handler.close();

                if (inserted.equals("inserted")) {
                    Toast.makeText(context, " Added to Shortlist " , Toast.LENGTH_SHORT).show();
                } else if(inserted.equals("error")){
                    Toast.makeText(context, " Already present in Shortlist " , Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(context, " Try again " , Toast.LENGTH_SHORT).show();
                }

            return true;
        }
    });

    return row;
}
static class RecordHolder {
    ImageView frontImage;
    TextView frontMobileName;
    TextView frontPrice;
    TextView backMobileName;
    TextView descOne;
    TextView descTwo;
    TextView descThree;
    TextView descFour;
    TextView ccEmi;
    TextView financeEmi;
    LinearLayout frontlayout, backlayout, parentLayout;
    Button viewdetails;
    TextView tv_front_inr, tv_description, tv_price_ccemi_inr,
            tv_ccemi_price, tv_price_finance_inr, tv_financeemi_price;

}

此代码显示正确的位置,但在我滚动时下方,然后我仍然会翻转其他项目。请帮帮我。

1 个答案:

答案 0 :(得分:0)

要获得在GridView中单击的项目的确切位置,您需要在GridView参考上使用setOnItemClickListener,例如:

       GridView g = (GridView) findViewById(R.id.gridView1);

       g.setAdapter(new ImageAdopterShow(this)); // your custom adaptor object goes here


       g.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {


            Toast.makeText(getBaseContext(), "position of item is " +position, Toast.LENGTH_SHORT).show();


        }
    });

这是我获取GridView中任何项目位置的方式。希望这能回答你的问题。

您的问题有点误导如果您可以获得项目位置,那么您需要更改您在GridView中无法正常工作的动画问题。