Click here to Skip to main content
15,915,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using viewpager as parent and have three fragments as child. Inside all fragments I have 12 edittext. Now I am able to scroll vertically within fragment but when I scroll horizontally fragments doesn't changing.


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



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

            }
        });
}


What I have tried:

I tries with different source code from internet. Also did some experiment in xml files but no luck.
Posted
Updated 18-Mar-18 16:07pm
v16

1 solution

To achieve both scrolling behaviors, use HorizontalScrollView

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:scrollbars="vertical">

    <HorizontalScrollView 
        android:layout_width="320px" android:layout_height="fill_parent">

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linlay" android:layout_width="320px"
            android:layout_height="fill_parent" android:stretchColumns="1"
            android:background="#000000"/>

    </HorizontalScrollView>

</ScrollView>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900