Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OnTouchEvent不';I don’我不在相对的地方工作_Android_Android Layout_Touch Event - Fatal编程技术网

Android OnTouchEvent不';I don’我不在相对的地方工作

Android OnTouchEvent不';I don’我不在相对的地方工作,android,android-layout,touch-event,Android,Android Layout,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"

这是我的布局:

    <?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,并添加了另一个带有文本视图的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;
        }
    };

}
公共类模块helpofguide扩展活动{
私有最终字符串路径\u IMAGE=“helper”;
浮球x1、y1、x2、y2;
屏幕寻呼机屏幕寻呼机;
LinearLayout linearLayoutHelper、linearLayoutHorizontalHelper;
RelativeLayout relativeLayoutHelper;
TextView页面TextView[];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.screen\u pager\u布局);
LayoutInflater.from(this.setFactory)(ScreenPager.getShortNameFactory());
screenPagerHelper=新的ScreenPager(此);
addInScreen();
relativeLayoutHelper=(RelativeLayout)findViewById(R.id.relativeLayoutHelper);
linearLayoutHelper=(LinearLayout)findViewById(R.id.LinearForPage);
LinearLayout.LayoutParams LayoutParams=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父级,LinearLayout.LayoutParams.MATCH_父级);
screenPagerHelper.setLayoutParams(layoutParams);
linearLayoutHelper.addView(screenPagerHelper);

relativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper); linearLayoutHorizontalHelper=新的LinearLayout(此); LinearLayout.LayoutParams LayoutParamsShorizontal=新的LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.MATCH_父级); linearLayoutHorizontalHelper.setLayoutParams(layoutParamsHorizontal); LinearLayout.LayoutParams layoutParamsPageText=新建LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容); layoutParamsPageText.gravity=gravity.BOTTOM; 字符串[]列出资产; 试一试{ listAssets=getAssets().list(路径\图像); int count=listAssets.length; pageTextView=新文本视图[计数]; for(int i=0;irelativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper); relativeLayoutHelper.addView(linearLayoutHorizontalHelper); } 专用void addInScreen(){ 字符串[]列出资产; 试一试{ listAssets=getAssets().list(路径\图像); 如果(listAssets.length>0){ 对于(字符串路径文件:listAssets){ InputStream file=getAssets().open(路径\图像+“/”+路径文件); Drawable Drawable=Drawable.createFromStream(文件,null); ImageView ImageView=新的ImageView(此); LinearLayout.LayoutParams LayoutParams=新建 LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项, LinearLayout.LayoutParams.MATCH_PARENT); setLayoutParams(layoutParams); imageView.setImageDrawable(可绘制); imageView.setScaleType(imageView.ScaleType.FIT_XY); screenPagerHelper.addScreen(imageView); } } }捕获(IOE异常){ e、 printStackTrace(); } } 私有TextView CreateTextViewHelper(){ TextView textViewHelper=新的TextView(getApplicationContext()); textViewHelper.setText(“.”); textViewHelper.setTextSize(65); textViewHelper.setTypeface(null,Typeface.BOLD); textViewHelper.setTextColor(android.graphics.Color.GRAY); 返回textViewHelper; }; View.OnTouchListener touchListenerRelativeHelper=新视图。OnTouchListener(){ @凌驾 公共布尔onTouch(视图v,运动事件){ if(event.getAction()==MotionEvent.ACTION\u向下){ int current=screenPagerHelper.getCurrentItem(); 对于(int i=0;i

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

我解决了一个不好的问题,但它可以工作,如何在ScreenPager listener 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;
        }
    };
View.OnTouchListener touchListenerHelper=new View.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
电流;
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
x1=event.getX();
打破
案例Mot