橫向滑動選單
阿新 • • 發佈:2019-02-06
public class MainActivity extends AppCompatActivity {
//所有的選單內容
private List<String> menus=new ArrayList<>();
private HorizontalScrollView hsv;
private LinearLayout linMenu;
private List<TextView> listMenus;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hsv = findViewById(R.id.hsv);
linMenu = findViewById(R.id.linmenu);
viewPager = findViewById(R.id.vp);
//初使化
initMenus();
addMenus();
//設定介面卡
MyAdapter myAdapter=new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(myAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//position:當前頁面的下標
@Override
public void onPageSelected(int position) {
//設定選中的選單
setSelectMenu(position);
//設定橫向滾動條滾動
setHsvScorll(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
//設定HorizontalScrollView 滾動
private void setHsvScorll(int menuIndex){
//得到對應的選單textview
TextView textView = listMenus.get(menuIndex);
//得到它的座標
float x = textView.getX();
//滾動
hsv.scrollTo((int)x,0);
}
//定義viewPager的介面卡
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
//用來返回每個頁面
@Override
public Fragment getItem(int position) {
Log.d("zzz","getItem:"+position);
//傳值
ContentFragment contentFragment=new ContentFragment();
Bundle bundle=new Bundle();
bundle.putString("key",menus.get(position));
contentFragment.setArguments(bundle);
return contentFragment;
}
//返回頁面的數量
@Override
public int getCount() {
return menus.size();
}
}
private void addMenus() {
for (int i = 0; i <menus.size() ; i++) {
TextView textView=new TextView(this);
textView.setText(menus.get(i));
//預設第一項為紅色,其它為黑色字型
if(i==0){
textView.setTextColor(Color.RED);
}else{
textView.setTextColor(Color.BLACK);
}
//將新增時的下標,賦值給tag
textView.setTag(i);
//設定點選事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//呼叫方法
setSelectMenu(Integer.parseInt(view.getTag()+""));
//切換viewPager的頁面
viewPager.setCurrentItem(Integer.parseInt(view.getTag()+""));
}
});
//設定控制元件的寬度與高度
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//設定控制元件的margin
params.setMargins(0,5,10,5);
//新增元件
linMenu.addView(textView,params);
//將新創建出來的textview元件新增到集合中
listMenus.add(textView);
}
}
/**
* 設定當前選中的選單 文字為紅色,其它為黑色
* @param tvIndex 選單的下標
*/
private void setSelectMenu(int tvIndex){
for (int i = 0; i <listMenus.size() ; i++) {
if(i==tvIndex){
listMenus.get(i).setTextColor(Color.RED);
}else {
listMenus.get(i).setTextColor(Color.BLACK);
}
}
}
private void initMenus() {
listMenus=new ArrayList<>();
menus.add("新聞");
menus.add("社會");
menus.add("娛樂");
menus.add("體育");
menus.add("財經");
menus.add("汽車");
menus.add("科技");
menus.add("軍事");
menus.add("小說");
menus.add("汽車1");
menus.add("科技1");
menus.add("軍事1");
menus.add("小說1");
}
}
//所有的選單內容
private List<String> menus=new ArrayList<>();
private HorizontalScrollView hsv;
private LinearLayout linMenu;
private List<TextView> listMenus;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hsv = findViewById(R.id.hsv);
linMenu = findViewById(R.id.linmenu);
viewPager = findViewById(R.id.vp);
//初使化
initMenus();
addMenus();
//設定介面卡
MyAdapter myAdapter=new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(myAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//position:當前頁面的下標
@Override
public void onPageSelected(int position) {
//設定選中的選單
setSelectMenu(position);
//設定橫向滾動條滾動
setHsvScorll(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
//設定HorizontalScrollView 滾動
private void setHsvScorll(int menuIndex){
//得到對應的選單textview
TextView textView = listMenus.get(menuIndex);
//得到它的座標
float x = textView.getX();
//滾動
hsv.scrollTo((int)x,0);
}
//定義viewPager的介面卡
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
//用來返回每個頁面
@Override
public Fragment getItem(int position) {
Log.d("zzz","getItem:"+position);
//傳值
ContentFragment contentFragment=new ContentFragment();
Bundle bundle=new Bundle();
bundle.putString("key",menus.get(position));
contentFragment.setArguments(bundle);
return contentFragment;
}
//返回頁面的數量
@Override
public int getCount() {
return menus.size();
}
}
private void addMenus() {
for (int i = 0; i <menus.size() ; i++) {
TextView textView=new TextView(this);
textView.setText(menus.get(i));
//預設第一項為紅色,其它為黑色字型
if(i==0){
textView.setTextColor(Color.RED);
}else{
textView.setTextColor(Color.BLACK);
}
//將新增時的下標,賦值給tag
textView.setTag(i);
//設定點選事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//呼叫方法
setSelectMenu(Integer.parseInt(view.getTag()+""));
//切換viewPager的頁面
viewPager.setCurrentItem(Integer.parseInt(view.getTag()+""));
}
});
//設定控制元件的寬度與高度
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//設定控制元件的margin
params.setMargins(0,5,10,5);
//新增元件
linMenu.addView(textView,params);
//將新創建出來的textview元件新增到集合中
listMenus.add(textView);
}
}
/**
* 設定當前選中的選單 文字為紅色,其它為黑色
* @param tvIndex 選單的下標
*/
private void setSelectMenu(int tvIndex){
for (int i = 0; i <listMenus.size() ; i++) {
if(i==tvIndex){
listMenus.get(i).setTextColor(Color.RED);
}else {
listMenus.get(i).setTextColor(Color.BLACK);
}
}
}
private void initMenus() {
listMenus=new ArrayList<>();
menus.add("新聞");
menus.add("社會");
menus.add("娛樂");
menus.add("體育");
menus.add("財經");
menus.add("汽車");
menus.add("科技");
menus.add("軍事");
menus.add("小說");
menus.add("汽車1");
menus.add("科技1");
menus.add("軍事1");
menus.add("小說1");
}
}