开发者

Android: Touch Events not Working

This is my Main activity code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Display display = getWindowManager().getDefaultDisplay();        

    //Setting the FileBrowser FrameLayout Settings.
    FrameLayout fileBrowserLayout =  (FrameLayout) findViewById(R.id.flFBrowser);
    FrameLayout previewLayout = (FrameLayout) findViewById(R.id.flPreview);
    TextView instructions = (TextView)findViewById(R.id.tvInstructions);

    fileBrowserLayout.getLayoutParams().width = display.getWidth()*WidthFileBrowser/100;
    previewLayout.getLayoutParams().width = display.getWidth()*WidthPreview/100;
    previewLayout.getLayoutParams().height = display.getHeight()*PreviewHeight/100;
    instructions.getLayoutParams().width = display.getWidth()*WidthPreview/100;
    instructions.getLayoutParams().height = display.getHeight()*InstructionsHeight/100;
    instructions.setText(Instructions);
    instructions.setTextSize(InstructionTextSize);
    instructions.setTextColor(InstructionTextColor);
    instructions.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
    instructions.setGravity(Gravity.CENTER_HORIZONTAL);

    //Attempting to set the Fragment        
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();       
    FileBrowser fb = new FileBrowser();
    preview = new Preview();
    ft.add(R.id.flFBrowser,fb);
    ft.add(R.id.flPreview, preview);
    ft.commit();

}

Then this is my code for Preview Fragment

public class Preview extends Fragment implements ViewFactory,OnTouchListener{

@Override
public boolean onTouch(View v, MotionEvent event) {
    System.err.println("Something touched me!");
    return true;
}
}

There is more i开发者_运维百科ntialization code on the onActivityStarted but it has to do with intializing an imageswitcher.

However I can't figure out why my touch event is not registering.

Can anyone help me, please?


The problem is that you're not actually registering your onTouchListener that is implemented in your Preview class. I don't know and I don't think (based on the API) you can register a touch listener for Fragments but you can for any of the views you have.

Try registering the onTouchListener for one of your FrameLayouts. You can do this after initializing your preview object:

preview = new Preview();
previewLayout.setOnTouchListener(preview);

That should do the trick.


In my case, I had children that were intercepting touch events. I wanted to attach a recognizer to a parent layout, so to do that, I needed to intercept the touch events (http://developer.android.com/training/gestures/viewgroup.html#intercept). I had to subclass the layout and overriding onInterceptTouchEvent() to pass the event to any touch listener I may attach to my layout. I implemented my listener and detector in a fragment, with the listener forwarding the event to the detector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜