import android.graphics.Color;
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.os.Bundle;
import android.support.v7.widget.ContentFrameLayout;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private LinearLayout linMenu;
private List<String> tabMenus=new ArrayList<>();
private ViewPager viewPager;
private List<TextView> menus=new ArrayList<>();
private HorizontalScrollView horizontalScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
horizontalScrollView = findViewById(R.id.hs_menu);
//x:水平方向的滚动 y:垂直方向的滚动
// horizontalScrollView.scrollTo(x,y);
linMenu = findViewById(R.id.lin_menu);
viewPager = findViewById(R.id.vp);
initData();
//设置vp的适配器 PagerAdapter FragmentPagerAdapter
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
Contentfragment contentfragment=new Contentfragment();
//传值
Bundle bundle=new Bundle();
bundle.putString("data",tabMenus.get(position));//每次得到菜单的值
contentfragment.setArguments(bundle);
return contentfragment;
}
//返回页面的数据
@Override
public int getCount() {
return tabMenus.size();
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {//position:页面的下标
//改变菜单的字体颜色
setMenuColor(position);
//滚动horizontalScrollView
moveMenu(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void moveMenu(int index){
TextView textView = menus.get(index);
float x = textView.getX();//在x轴上的坐标
//滚动到指定的位置
horizontalScrollView.scrollTo((int)x,0);
}
/**
* 设置菜单的颜色
* @param index
*/
public void setMenuColor(int index){
for (int i = 0; i <menus.size() ; i++) {
if(i==index){
menus.get(i).setTextColor(Color.GREEN);//选中
}else{
menus.get(i).setTextColor(Color.BLACK);
}
}
}
private void initData() {
tabMenus.add("推荐");
tabMenus.add("新时代");
tabMenus.add("视频");
tabMenus.add("体育");
tabMenus.add("娱乐");
tabMenus.add("国际");
tabMenus.add("国内");
tabMenus.add("社会");
tabMenus.add("问答");
tabMenus.add("科技");
tabMenus.add("图片");
//动态添加子菜单
for (int i = 0; i <tabMenus.size() ; i++) {
TextView textView=new TextView(this);
textView.setText(tabMenus.get(i));
if(i==0){
textView.setTextColor(Color.GREEN);
}else{
textView.setTextColor(Color.BLACK);
}
//记录值 textView的下标值
textView.setTag(i);
//设置布局参数
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0,5,10,5);
//添加点击事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//传值
// String menu=((TextView)view).getText().toString();
// Toast.makeText(MainActivity.this,(int)(view.getTag())+":"+menu,Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem((int)(view.getTag()));
}
});
//添加子视图
linMenu.addView(textView,params);
//将生成的TextView对象 添加到集合中
menus.add(textView);
}
}
}
// MianActity的副本
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="zhanghaijiao.bawei.com.horizon_mene.MainActivity">
<!--横向菜单-->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hs_menu">
<!--只能有一个子孩子-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/lin_menu">
</LinearLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/hs_menu"
android:id="@+id/vp">
</android.support.v4.view.ViewPager>
</RelativeLayout>
//自创一个类
public class Contentfragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.content,container,false);
TextView textView=v.findViewById(R.id.tv);
//接收参数
Bundle arguments = getArguments();
String data = arguments.getString("data");
textView.setText(data);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
//在创建副本
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/tv"
android:textColor="#0f0"
android:textSize="42sp"
android:text="123"/>
</RelativeLayout>