Detailed explanation of Android HorizontalScrollView sliding and ViewPager switching cases
- 2021-12-13 17:20:33
- OfStack
layout layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<HorizontalScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/line_sc"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sc"
/>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/view" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
MainActivity.java
package com.bwie.test;
import java.util.ArrayList;
import com.bwie.adapter.MyAdapter;
import com.bwie.fragment.F1;
import com.bwie.utils.Myutil;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private LinearLayout line_sc;
private HorizontalScrollView sc;
private String[] column = new String[] { " Important news in the house ", " Introduced by our agency ", " Perform one's functions ", " Self-construction ",
" Members' elegant demeanour ", " Introduce oneself ", " School anecdotes ", " Break the news " };
private ArrayList<Fragment> list;
private ViewPager vp;
private int widthPixels;
private ArrayList<TextView> list_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* Object of the window Width
*/
// Create DisplayMetrics Class object
DisplayMetrics dm = new DisplayMetrics();
// The of the current window 1 Some information is put in DisplayMetrics Class
getWindowManager().getDefaultDisplay().getMetrics(dm);
widthPixels = dm.widthPixels;
// Initialization data
init();
// Getting data for a column
getColumn();
// Create fragment Data
setFragment();
// For vp Setting data
FragmentManager fm = getSupportFragmentManager();
vp.setAdapter(new MyAdapter(fm, list));
setTitle_1(vp.getCurrentItem());
// For vp Set up page sliding monitoring
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// Set the cursor
setTitle_1(arg0);
// Set sliding
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
// Gets the cursor currently to be displayed
TextView textView = list_view.get(p);
// Get its width
int w = textView.getMeasuredWidth();
int left = textView.getLeft();
int address=left-widthPixels/2;
sc.smoothScrollTo(address, 0);
}
private void setFragment() {
// Create a collection store fragment Object
list = new ArrayList<Fragment>();
for (int i = 0; i < column.length; i++) {
F1 f1 = new F1();
// Value transfer
Bundle b = new Bundle();
b.putInt("column", i);
f1.setArguments(b);
// The that will be created fragment Add to the collection
list.add(f1);
}
}
private void setTitle_1(int arg0) {
// Sets the currently displayed textview Control
TextView textView = list_view.get(arg0);
// Will the current textview Display
textView.setVisibility(View.VISIBLE);
for(int i=0;i<list_view.size();i++)
{
TextView tv = list_view.get(i);
if(tv!=textView)
{
tv.setVisibility(View.GONE);
}
}
}
private void getColumn() {
// TODO Auto-generated method stub
list_view = new ArrayList<TextView>();
for (int i = 0; i < column.length; i++) {
// Create a linear layout as a whole as a horizontal scrolling sub-layout
LinearLayout sc = new LinearLayout(MainActivity.this);
// Set the arrangement of linear layout ( Vertical )
sc.setOrientation(1);
sc.setPadding(30, 0, 0, 0);
// 1. First, add to the linear layout textview
// Create textview
TextView tv = new TextView(MainActivity.this);
// For tv Setting data
tv.setText(column[i]);
LinearLayout.LayoutParams params = new LayoutParams(
widthPixels / 5, LayoutParams.WRAP_CONTENT);
sc.addView(tv, params);
// 2. Add the following horizontal line to the linear layout
TextView tv_line = new TextView(MainActivity.this);
LinearLayout.LayoutParams params_2 = new LayoutParams(
widthPixels / 5, 10);
tv_line.setBackgroundColor(Color.GRAY);
tv_line.setVisibility(View.INVISIBLE);
list_view.add(tv_line);
sc.addView(tv_line, params_2);
// Add data to the In linear layout
line_sc.addView(sc);
}
}
private void init() {
line_sc = (LinearLayout) findViewById(R.id.line_sc);
sc = (HorizontalScrollView) findViewById(R.id.sc);
vp = (ViewPager) findViewById(R.id.vp);
}
}
horizontalScrollView viewpager Font Color Sliding Distance
FragmentManager fm = getSupportFragmentManager();
viewPager.setAdapter(new MyFragmentAdapter(fm, getData()));
viewPager.setOnPageChangeListener(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<Fragment> getData() {
ArrayList<Fragment> list = new ArrayList<Fragment>();
for (int i = 0; i < str.length; i++) {
list.add(new F1(str[i]));
}
return list;
}
private void setData() {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
for (int i = 0; i < str.length; i++) {
View view = View.inflate(MainActivity.this,
android.R.layout.simple_list_item_1, null);
TextView textView = (TextView) view
.findViewById(android.R.id.text1);
list.add(str[i]);
textView.setText(list.get(i));
scroll_ly.addView(view);
textView.setTag(i);
textView.setOnClickListener(this);
}
TextView childAt = (TextView) scroll_ly.getChildAt(0);
// childAt.setTextColor(Color.RED);
childAt.setBackgroundColor(Color.RED);
childAt.setTextColor(Color.WHITE);
}
// Settings scrollview Medium textview Change of
public void setTag(int position) {
for (int i = 0; i < scroll_ly.getChildCount(); i++) {
/**
* textView Sliding position
*/
// Get textview Adj. viewpager Sliding current subclass
TextView textView = (TextView) scroll_ly.getChildAt(position);
int k = textView.getMeasuredWidth();
int l = textView.getLeft();
int s = k / 2 + l - wid / 2;
// Settings scrollView x Axis distance
scrollView.smoothScrollTo(s, 0);
/**
* textview Sliding discoloration
*/
TextView textView2 = (TextView) scroll_ly.getChildAt(i);
if (position == i) {
textView2.setBackgroundColor(Color.RED);
textView2.setTextColor(Color.WHITE);
} else {
textView2.setTextColor(Color.BLACK);
textView2.setBackgroundColor(Color.WHITE);
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getTag() != null) {
int i = (Integer) v.getTag();
View childAt = scroll_ly.getChildAt(i);
if (v == childAt) {
viewPager.setCurrentItem(i);
}
}
}