在 scrollview 和 viewpager 之间触摸

Touch between scrollview and viewpager

我将 viewpager 用作父级,并使用三个片段作为子级。在所有片段中,我有 12 个编辑文本。现在我可以在片段内垂直滚动,但是当我水平滚动时,片段不会改变。

activity.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed" />


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />



    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

fragment.xml

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fffbfbfb"
    android:padding="1dp">
     ............12 edittext
</scrollview>

activity.java

public class MainActivity extends AppCompatActivity {


    private TabLayout tabLayout;
    public  ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
            }
        });
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new OneFragment(), "ONE");
        adapter.addFragment(new TwoFragment(), "TWO");
        adapter.addFragment(new ThreeFragment(), "THREE");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

fragment.java

public class OneFragment extends Fragment {

    private EditText etA, etB, etC, etD, etE, etF, etG, etH, etI, etJ, etK, etL;
    private TextView tvProfit;
    private ScrollView scrollView;

    public OneFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_one, container, false);

        etA = (EditText) view.findViewById(R.id.etboxA);
        etB = (EditText) view.findViewById(R.id.etboxB);
        etC = (EditText) view.findViewById(R.id.etboxC);
        etD = (EditText) view.findViewById(R.id.etboxD);
        etE = (EditText) view.findViewById(R.id.etboxE);
        etF = (EditText) view.findViewById(R.id.etboxF);
        etG = (EditText) view.findViewById(R.id.etboxG);
        etH = (EditText) view.findViewById(R.id.etboxH);
        etI = (EditText) view.findViewById(R.id.etboxI);
        etJ = (EditText) view.findViewById(R.id.etboxJ);
        etK = (EditText) view.findViewById(R.id.etboxK);
        etL = (EditText) view.findViewById(R.id.etboxL);

        tvProfit = (TextView) view.findViewById(R.id.tvprofit);

        scrollView = (ScrollView) view.findViewById(R.id.scrollView);

        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                scrollView.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
        return view;


    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume(){

        super.onResume();


        etB.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (!etB.getText().toString().equals("")) {
                    float ans = (float) (Float.parseFloat(etB.getText().toString()) / 355.6164);
                    etC.setText(String.valueOf(ans));
                } else {
                    etC.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
}

所以建议我如何在 scrollview 和 viewpager 之间切换触摸事件

修改你的代码如下可能问题是resolved.Cut把viewpager复制出TabLayout。

<android.support.design.widget.CoordinatorLayout       xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
     <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

我认为您正在删除 ViewPager 处理触摸事件的能力,请尝试删除:

viewPager.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        return false;
    }
});