Я использую viewpager в качестве родителя и имею три дочерних фрагмента. Внутри всех фрагментов у меня 12 edittext. Теперь я могу выполнять вертикальную прокрутку внутри фрагмента, но при горизонтальной прокрутке фрагменты не меняются.

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

1
Parsania Hardik 17 Фев 2016 в 09:19

2 ответа

Лучший ответ

Измените код следующим образом. Возможно, проблема решена. Вырежьте и скопируйте 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>
1
Ahmad Aghazadeh 17 Фев 2016 в 07:26

Я думаю, вы удаляете возможность ViewPager обрабатывать событие касания, попробуйте удалить:

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

        return false;
    }
});
0
tuanna 17 Фев 2016 в 06:44