java - 在 scrollview 和 viewpager 之间触摸

标签 java android android-fragments android-viewpager android-scrollview

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

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 之间切换触摸事件

最佳答案

修改你的代码如下 也许问题已经解决了。从 TabLayout 中剪切并复制 viewpager。

<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>

关于java - 在 scrollview 和 viewpager 之间触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35449283/

相关文章:

android - 如何在 Fragments 中添加一个按钮

android - Google+ 登录 onConnected 未调用

java - 打印标签 Java、循环(?)

android - 支持 setTypeface(Typeface.Italic) 的三星设备?

java - 如何使用 EJB 3.1 在我的测试中注入(inject) PesistenceContext?

android - 如何在android中使用tagsoup解析xml中的html内容

android - 如何使用 gradle 对库进行依赖注入(inject)?

java - 处理android中的后退按钮操作

java - 卡夫卡 : could not find or load main class installation Windows

java - Spring javax.validation 注释未在 BindingResult 中捕获