OnItemClickListener不在gridview中工作relativelayout视图

时间:2014-07-13 05:50:01

标签: android android-animation android-gridview viewflipper android-relativelayout

我正在翻转编程。当我点击 GridView 项目不起作用时。我正在使用自定义GridView。我实施了OnItemClickListener。当我调试这个程序时不打印这个语句。我已经尝试了这么多天。请帮我。谢谢你的进步。

我的代码在这里:

public class MainActivity extends Activity implements OnItemClickListener {



    ArrayList<String> gridImagesLoad = new ArrayList<String>();

    GridView gview;
    public ArrayList<Integer> mContent=new ArrayList<Integer>();
    gridflip mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        gview=(GridView)findViewById(R.id.gridImage);

        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mContent.add(R.drawable.back);
        mAdapter=new gridflip(this, mContent);
        gview.setAdapter(mAdapter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        System.out.println("Test Item Click ");

    }

}

这是自定义gridview

class gridflip extends BaseAdapter  {
    //  private LayoutInflater mInflater;
    //  Context mContext;
    public Activity mActivity;

    public ArrayList<Integer> mContentId;
    FlipAnimation flipAnimation;
    ViewHolder holder;
    Integer Rposition;

    public gridflip(Activity context, ArrayList<Integer> contentid) {
        //      mInflater = LayoutInflater.from(context);
        //      this.mContext=context;
        this.mActivity=context;
        this.mContentId=contentid;



    }


    public gridflip(Activity context, ArrayList<Integer> contentid, Integer position) {
        //      mInflater = LayoutInflater.from(context);
        //      this.mContext=context;
        this.mActivity=context;
        this.mContentId=contentid;
        this.Rposition=position;


    }

    public int getCount() {
        return  mContentId.size(); //contentNames.size();
    }

    public Object getItem(int position) {
        return mContentId.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

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

        LayoutInflater mInflater = mActivity.getLayoutInflater();

        final int pos = position;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.flipimages, null);

            holder.rootLayout=(ViewFlipper)convertView.findViewById(R.id.main_activity_root);
            holder.cardFace =(View)convertView.findViewById(R.id.main_activity_card_face); //(ImageView)convertView.findViewById(R.id.imgFront);
            holder.cardBack =(View)convertView.findViewById(R.id.main_activity_card_back); //(ImageView) convertView.findViewById(R.id.imgBack);
            holder.Face=(ImageView)convertView.findViewById(R.id.imgFront);
            holder.Back=(ImageView)convertView.findViewById(R.id.imgBack);          
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(pos==0){
            FlipAnimation flipAnimation = new FlipAnimation(holder.cardFace, holder.cardBack);

            if (holder.cardFace.getVisibility() == View.GONE) {
                flipAnimation.reverse();
            } else {

            }
            holder.rootLayout.startAnimation(flipAnimation);

        }


        holder.cardFace.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                System.out.println("Flip " );



            }
        });


        return convertView;
    }



    public class ViewHolder {

        ViewFlipper rootLayout;
        View cardFace;
        View cardBack;
        ImageView Face;
        ImageView Back;
    } 

    public void onCardClick(View v) {
        try{

            FlipAnimation flipAnimation = new FlipAnimation(holder.cardFace, holder.cardBack);

            if (holder.Face.getVisibility() == View.GONE) {
                flipAnimation.reverse();
            } else {

            }
            holder.rootLayout.startAnimation(flipAnimation);


        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

flipimages.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ViewFlipper
        android:id="@+id/main_activity_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
          android:onClick="cardClick"
        android:layout_centerVertical="true" >

        <RelativeLayout
            android:id="@+id/main_activity_card_face"
            android:layout_width="210dip"
            android:layout_height="303dip"
            android:clickable="true"

            android:padding="5dp" >

            <ImageView
                android:id="@+id/imgFront"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/front" >
            </ImageView>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/main_activity_card_back"
            android:layout_width="210dip"
            android:layout_height="303dip"
            android:clickable="true"

            android:visibility="gone" >

            <ImageView
                android:id="@+id/imgBack"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/back" >
            </ImageView>
        </RelativeLayout>
    </ViewFlipper>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您忘记定义onItemClickListner

添加 gview.setOnItemClickListener(this);

public class MainActivity extends Activity implements OnItemClickListener {

ArrayList<String> gridImagesLoad = new ArrayList<String>();

GridView gview;
public ArrayList<Integer> mContent=new ArrayList<Integer>();
gridflip mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    gview=(GridView)findViewById(R.id.gridImage);

    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mContent.add(R.drawable.back);
    mAdapter=new gridflip(this, mContent);
    gview.setAdapter(mAdapter);
    gview.setOnItemClickListener(this);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    System.out.println("Test Item Click ");

}

}