点击翻转视图或滑动视图的事件

时间:2013-09-13 04:54:42

标签: android

我正在尝试为显示图像或按钮的片段实现Click事件。

我能够实现翻转视图以获取另一个片段,但我无法让click事件处理这些片段。

我尝试使用android:onClick,甚至将clickable设置为true。我甚至尝试使用onClickListener和onTouch,但它并没有为我开火。

这是我尝试过的代码。

public class MainActivity extends Activity {
private FlipViewGroup contentView;
public int currentimageindex=0;
 private GestureDetector mDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
        // hde staitbar otef Andoroid
        // could also be usdne lar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setTitle(R.string.activity_title);
    contentView = new FlipViewGroup(this);
    contentView.addFlipView(View.inflate(this, R.layout.second_page, null));
    contentView.addFlipView(View.inflate(this, R.layout.first_page, null)); 
    setContentView(contentView);
//  setContentView(R.layout.second_page);
    contentView.startFlipping(); //make the first_page view flipping        
    ImageView imgFavorite =   (ImageView) findViewById(R.id.imageView1);
    imgFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,
                    "The favorite list would appear on clicking this icon",
                    Toast.LENGTH_LONG).show();
        }
    });      
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

}
@Override
protected void onResume() {
    super.onResume();
    contentView.onResume();
}
@Override
protected void onPause() {
    super.onPause();
    contentView.onPause();
}   
public void onclick(View v){
    ImageView imgFavorite =   (ImageView) findViewById(R.id.imageView1);
    imgFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,
                    "The favorite list would appear on clicking this icon",
                    Toast.LENGTH_LONG).show();
        }
    }); 
}
}

这是我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white">                
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:src="@drawable/android"
android:clickable="true"
android:onClick="onclick"/>    
</RelativeLayout>

是否有某种原因翻转视图中的片段不响应点击或触摸事件,或者我的代码中是否存在我做错的事情?

1 个答案:

答案 0 :(得分:0)

据我所知,FlipViewGroup是您的自定义视图组实现。 如果是这种情况,您需要onInterceptTouchEvent(),请在此处查看示例代码。 http://developer.android.com/training/gestures/viewgroup.html