OnTouchEvent在RelatiVeLayout中不起作用

时间:2015-04-27 08:02:07

标签: android android-layout ontouchevent touch-event

这是我的布局:

    <?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"
                android:id="@+id/relativeLayoutHelper"
                android:focusableInTouchMode="true"
                android:focusable="true"
                android:clickable="true">
<LinearLayout
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/LinearForPager"
              android:focusableInTouchMode="true"
              android:clickable="true"
              android:focusable="true">

</LinearLayout>

</RelativeLayout>

在programm中我在LinearLayout中添加了对象ScreenPager,它扩展了ViewPager类,我用TextViews添加了另一个LinearLayout:

public class ModuleHelpOfGuide extends Activity {
    private  final String PATH_IMAGE = "helper";
    float x1,y1, x2,y2;
    ScreenPager screenPagerHelper;
    LinearLayout linearLayoutHelper,linearLayoutHorizontalHelper;
    RelativeLayout relativeLayoutHelper;
    TextView pageTextView [];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_pager_layout);

        LayoutInflater.from(this).setFactory(ScreenPager.getShortNameFactory());
        screenPagerHelper = new ScreenPager(this);
        addInScreen();

        relativeLayoutHelper = (RelativeLayout)findViewById(R.id.relativeLayoutHelper);

        linearLayoutHelper = (LinearLayout) findViewById(R.id.LinearForPager);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        screenPagerHelper.setLayoutParams(layoutParams);
        linearLayoutHelper.addView(screenPagerHelper);
        relativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper);

        linearLayoutHorizontalHelper = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParamsHorizontal = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayoutHorizontalHelper.setLayoutParams(layoutParamsHorizontal);

        LinearLayout.LayoutParams layoutParamsPageText = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParamsPageText.gravity = Gravity.BOTTOM;

        String [] listAssets;
        try {
            listAssets = getAssets().list(PATH_IMAGE);
            int count = listAssets.length;
             pageTextView  = new TextView [count];
            for (int i = 0; i < count; i++) {
                pageTextView[i] = createTexViewHelper();
                pageTextView[i].setLayoutParams(layoutParamsPageText);
                linearLayoutHorizontalHelper.addView(pageTextView[i]);
            }
            pageTextView[screenPagerHelper.getCurrentItem()].setTextColor(Color.WHITE);
        } catch (IOException e) {
            e.printStackTrace();
        }      

       relativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper);
        relativeLayoutHelper.addView(linearLayoutHorizontalHelper);
    }



    private void addInScreen (){
        String [] listAssets;
        try {
            listAssets = getAssets().list(PATH_IMAGE);
            if (listAssets.length > 0){
                for (String pathFile : listAssets){
                    InputStream file = getAssets().open(PATH_IMAGE + "/" + pathFile);
                    Drawable drawable = Drawable.createFromStream(file,null);
                    ImageView imageView = new ImageView(this);
                    LinearLayout.LayoutParams layoutParams = new
                            LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT);
                    imageView.setLayoutParams(layoutParams);
                    imageView.setImageDrawable(drawable);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    screenPagerHelper.addScreen(imageView);
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }

    }

    private TextView createTexViewHelper (){
        TextView textViewHelper = new TextView(getApplicationContext());
        textViewHelper.setText(".");
        textViewHelper.setTextSize(65);
        textViewHelper.setTypeface(null, Typeface.BOLD);
        textViewHelper.setTextColor(android.graphics.Color.GRAY);
        return textViewHelper;
    };

    View.OnTouchListener touchListenerRelativeHelper = new View.OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
         if (event.getAction() == MotionEvent.ACTION_DOWN){
                    int current =  screenPagerHelper.getCurrentItem();
                    for (int i = 0; i < pageTextView.length; i++){
                        pageTextView[i].setTextColor(android.graphics.Color.GRAY);
                    }
                    pageTextView[current].setTextColor(getResources().getColor(R.color.white));
             return true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP){
                int current =  screenPagerHelper.getCurrentItem();
                for (int i = 0; i < pageTextView.length; i++){
                    pageTextView[i].setTextColor(android.graphics.Color.GRAY);
                }
                pageTextView[current].setTextColor(getResources().getColor(R.color.white));
                return false;
            }
            return true;
        }
    };

}

TouchEvent中的问题,如果在相对或线性Layots上使用它,如果我在ScreenPager上使用它而不是事件开始工作,则它不起作用,但是textView滞后于slaider中的1个图像。如果我在ScreenPager中使用TouchEvent并返回true,则滑块不起作用。如何在RelativeLayout上跟踪事件touchlistener?

1 个答案:

答案 0 :(得分:0)

我解决了问题并不好,但它有效,怎么可能,在ScreenPager监听器上设置TouchListener和touchlistener:

View.OnTouchListener touchListenerHelper = new View.OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int current;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x1 = event.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        x2 = event.getX();
                        if (x1 > x2 ) {
                            current = screenPagerHelper.getCurrentItem()+1;
                            current = current == pageTextView.length ?  pageTextView.length - 1 : current;
                            for (int i = 0; i < pageTextView.length; i++) {
                                pageTextView[i].setTextColor(android.graphics.Color.GRAY);
                            }
                            pageTextView[current].setTextColor(getResources().getColor(R.color.white));
                        } else if ( x1 < x2){
                            current = screenPagerHelper.getCurrentItem()-1;
                            current = current < 0 ?  0 : current;
                                for (int i = 0; i < pageTextView.length; i++) {
                                    pageTextView[i].setTextColor(android.graphics.Color.GRAY);
                                }
                                pageTextView[current].setTextColor(getResources().getColor(R.color.white));
                        }
                        break;
            }
            return false;
        }
    };

因为ViewPAger事件在我的服装事件之后开始,我加入当前的1.