SoFunction
Updated on 2025-04-07

Detailed explanation of Android HorizontalScrollView swiping and ViewPager switching case

layout

<RelativeLayout xmlns:andro
    xmlns:tools="/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <HorizontalScrollView
        android:
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <LinearLayout
            android:
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </LinearLayout>
    </HorizontalScrollView>

    <LinearLayout
        android:
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/sc"
      />

    <.
        android:
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/view" >
    </.>

</RelativeLayout>

 

package ;
import ;
import ;
import .F1;
import ;
import ;
import ;
import ;
import .;
import .;
import .;
import .;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class MainActivity extends FragmentActivity {

    private LinearLayout line_sc;
    private HorizontalScrollView sc;
    private String[] column = new String[] { "Best News", "Introduction to our company", "Perform functions", "Own construction",
            "Member style", "Self-introduction", "Interesting News on School", "Revelation" };
    private ArrayList&lt;Fragment&gt; list;
    private ViewPager vp;
    private int widthPixels;
    private ArrayList&lt;TextView&gt; list_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_main);

        /**
          * Get the width of the window
          */
        // Create DisplayMetrics class object        DisplayMetrics dm = new DisplayMetrics();
        // Put some information in the current window in the DisplayMetrics class        getWindowManager().getDefaultDisplay().getMetrics(dm);
        widthPixels = ;     
        // Initialize the data        init();
        // Get column data        getColumn();
        // Create fragment data        setFragment();
        // Set data for vp        FragmentManager fm = getSupportFragmentManager();
        (new MyAdapter(fm, list));        
        setTitle_1(());

        // Set page sliding monitoring for vp        (new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {               
                //Set cursor                setTitle_1(arg0);               
                //Set swipe                setAutoScroll(arg0);
            }
        
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
        });

    }

    protected void setAutoScroll(int p) {
        // TODO Auto-generated method stub
        
        //Get the cursor you want to display currently        TextView textView = list_view.get(p);
        //Get its width        int w = ();
        int left = ();
        int address=left-widthPixels/2;       
        (address, 0);                  
    }

    private void setFragment() {
        // Create a collection to store fragment objects        list = new ArrayList&lt;Fragment&gt;();
        for (int i = 0; i &lt; ; i++) {
            F1 f1 = new F1();
            // Pass the value            Bundle b = new Bundle();
            ("column", i);
            (b);
            // Add the created fragment to the collection            (f1);
        }
    }
    private void setTitle_1(int arg0) {
        //Set the currently displayed textview control        TextView textView = list_view.get(arg0);
        //Show the current textview        ();     
        for(int i=0;i&lt;list_view.size();i++)
        {
            TextView tv = list_view.get(i);
            
            if(tv!=textView)
            {
                ();
            }
        }
    }
    private void getColumn() {
        // TODO Auto-generated method stub
        list_view = new ArrayList&lt;TextView&gt;();
        for (int i = 0; i &lt; ; i++) {
            // Create a linear layout as a sub-layout for horizontal scrolling as a whole            LinearLayout sc = new LinearLayout();
            // Set the arrangement method of linear layout (vertical)            (1);
            (30, 0, 0, 0);
            // 1. First add textview to the linear layout            // Create textview            TextView tv = new TextView();
            // Set data for tv            (column[i]);
             params = new LayoutParams(
                    widthPixels / 5, LayoutParams.WRAP_CONTENT);
            (tv, params);

            // 2. Add the following horizontal lines to the linear layout            TextView tv_line = new TextView();
             params_2 = new LayoutParams(
                    widthPixels / 5, 10);
            tv_line.setBackgroundColor();
            tv_line.setVisibility();
            list_view.add(tv_line);
            (tv_line, params_2);

            // Add data to linear layout            line_sc.addView(sc);
        }

    }

    private void init() {
        line_sc = (LinearLayout) findViewById(.line_sc);
        sc = (HorizontalScrollView) findViewById();
        vp = (ViewPager) findViewById();
    }
}

horizontalScrollView viewpager Font color Sliding distance

FragmentManager fm = getSupportFragmentManager();
        (new MyFragmentAdapter(fm, getData()));
        (new OnPageChangeListener() {

            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                setTag(arg0);
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
            }

            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

public ArrayList&lt;Fragment&gt; getData() {

        ArrayList&lt;Fragment&gt; list = new ArrayList&lt;Fragment&gt;();
        for (int i = 0; i &lt; ; i++) {
            (new F1(str[i]));
        }
        return list;
    }

    private void setData() {
        // TODO Auto-generated method stub
        List&lt;String&gt; list = new ArrayList&lt;String&gt;();
        for (int i = 0; i &lt; ; i++) {
            View view = (,
                    .simple_list_item_1, null);
            TextView textView = (TextView) view
                    .findViewById(.text1);
            (str[i]);
            ((i));
            scroll_ly.addView(view);

            (i);
            (this);
        }
        TextView childAt = (TextView) scroll_ly.getChildAt(0);
        // ();
        ();
        ();
    }

// Set changes in textview in scrollview    public void setTag(int position) {
        for (int i = 0; i &lt; scroll_ly.getChildCount(); i++) {
            /**
              * The position of textView sliding
              */
            // Get the current subclass of viewpager sliding of the textview            TextView textView = (TextView) scroll_ly.getChildAt(position);
            int k = ();
            int l = ();
            int s = k / 2 + l - wid / 2;
            // Set the distance of scrollView x-axis            (s, 0);
            /**
              * textview slides and changes color
              */
            TextView textView2 = (TextView) scroll_ly.getChildAt(i);
            if (position == i) {
                ();
                ();
            } else {
                ();
                ();
            }

        }
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (() != null) {
            int i = (Integer) ();
            View childAt = scroll_ly.getChildAt(i);
            if (v == childAt) {
                (i);
            }
        }
    }

This is the article about the detailed explanation of the Android HorizontalScrollView Sliding and ViewPager Switching Cases. This is the end of this article. For more related Android HorizontalScrollView Sliding and ViewPager Switching Content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!