主界面:
import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private HorizontalScrollView scrollView; private LinearLayout layout; private ViewPager viewPager; private String[] titles; private ArrayList<TextView> TextViewList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scrollView = (HorizontalScrollView) findViewById(R.id.scroll); layout = (LinearLayout) findViewById(R.id.linear); viewPager = (ViewPager) findViewById(R.id.viewpager); titles = new String[]{"头条", "娱乐", "科技", "信息", "八卦", "北京", "上海", "天津", "重庆", "大大燕网"}; initData(); viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return BlankFragment.getintances(titles[position]); } @Override public int getCount() { return titles.length; } }); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i = 0; i < TextViewList.size(); i++) { if (position == i) { TextViewList.get(i).setTextColor(Color.RED); } else { TextViewList.get(i).setTextColor(Color.BLACK); } } TextView textView = TextViewList.get(position); int width = textView.getWidth(); scrollView.scrollTo((width + 20) * position, 0); } @Override public void onPageScrollStateChanged(int state) { } }); } private void initData() { TextViewList = new ArrayList<TextView>(); for (int i = 0; i < titles.length; i++) { TextView textView = new TextView(MainActivity.this); textView.setText(titles[i]); textView.setTextSize(24); textView.setOnClickListener(this); textView.setId(i + 10000); if (i == 0) { textView.setTextColor(Color.RED); } else { textView.setTextColor(Color.BLACK); } LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(20, 10, 20, 10); layout.addView(textView, layoutParams); TextViewList.add(textView); } } @Override public void onClick(View view) { int id = view.getId(); viewPager.setCurrentItem(id-10000); } }
Fragment:
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * 作者:李治的爷爷 * 时间:2017/10/12 */ public class BlankFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); Bundle bundle = getArguments(); String name = bundle.getString("name"); textView.setText(name); return textView; } public static BlankFragment getintances(String name){ BlankFragment blankFragment = new BlankFragment(); Bundle bundle = new Bundle(); bundle.putString("name",name); blankFragment.setArguments(bundle); return blankFragment; } }
布局:
<LinearLayout 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" android:orientation="vertical" tools:context="com.bwie.scrollviewindicater.MainActivity"> <HorizontalScrollView android:id="@+id/scroll" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/linear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </HorizontalScrollView> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content"> </android.support.v4.view.ViewPager> </LinearLayout>