安卓Day06
轮波图的应用
是一个可以滑动的页面滚动控件
是一个需要适配器通知的一个控件
package com.example.day07lianxitwo;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
private RadioButton menu,mymenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu=findViewById(R.id.menu);
mymenu=findViewById(R.id.mymenu);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout,new MenuFrament());
fragmentTransaction.commit();
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
MenuFrament menuFrament = new MenuFrament();
fragmentTransaction.replace(R.id.frame_layout,menuFrament);
fragmentTransaction.commit();
}
});
mymenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.replace(R.id.frame_layout,myFragment);
fragmentTransaction.commit();
}
});
}
}
package com.example.day07lianxitwo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class MenuFrament extends Fragment {
private List<String> list=new ArrayList<>();
private List<ImageView> image=new ArrayList<>();
public MenuFrament() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_menu_frament, container, false);
ViewPager viewPager=inflate.findViewById(R.id.view_pager);
LinearLayout linearLayout=inflate.findViewById(R.id.ll);
for (int i = 0; i <5 ; i++) {
list.add("那个男孩爱上了隔壁的母猪");
}
for (int i = 0; i <list.size() ; i++) {
ImageView imageView = new ImageView(getActivity());
image.add(imageView);
linearLayout.addView(imageView);
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
for (int j = 0; j <image.size() ; j++) {
ImageView imageView = image.get(j);
imageView.setImageResource(R.drawable.is_flase);
}
ImageView imageView = image.get(i);
imageView.setImageResource(R.drawable.is_true);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
MyAdapter myAdapter = new MyAdapter(list, getActivity());
viewPager.setAdapter(myAdapter);
return inflate;
}
}
轮播图与fragment的结合应用
当你点击切换页面时轮播图消失用于配合
package com.example.day07lianxitwo;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
private RadioButton menu,mymenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu=findViewById(R.id.menu);
mymenu=findViewById(R.id.mymenu);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout,new MenuFrament());
fragmentTransaction.commit();
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
MenuFrament menuFrament = new MenuFrament();
fragmentTransaction.replace(R.id.frame_layout,menuFrament);
fragmentTransaction.commit();
}
});
mymenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.replace(R.id.frame_layout,myFragment);
fragmentTransaction.commit();
}
});
}
}
package com.example.day07lianxitwo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class MenuFrament extends Fragment {
private List<String> list=new ArrayList<>();
private List<ImageView> image=new ArrayList<>();
public MenuFrament() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_menu_frament, container, false);
ViewPager viewPager=inflate.findViewById(R.id.view_pager);
LinearLayout linearLayout=inflate.findViewById(R.id.ll);
for (int i = 0; i <5 ; i++) {
list.add("那个男孩爱上了隔壁的母猪");
}
for (int i = 0; i <list.size() ; i++) {
ImageView imageView = new ImageView(getActivity());
image.add(imageView);
linearLayout.addView(imageView);
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
for (int j = 0; j <image.size() ; j++) {
ImageView imageView = image.get(j);
imageView.setImageResource(R.drawable.is_flase);
}
ImageView imageView = image.get(i);
imageView.setImageResource(R.drawable.is_true);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
MyAdapter myAdapter = new MyAdapter(list, getActivity());
viewPager.setAdapter(myAdapter);
return inflate;
}
}
如果需要添加滚动显示球在选择器里写入方法,for循环添加页面数量的显示生成图片控件.加入轮播图的位置监听先循环所有变为你说设置的false,在选择单个的image设为true(前提是先创建一个集合把你所有的图片或控件放入)
如要让轮播图自已滚动使用一个定时器使用ViewPager的方法修改他的位置*(子线程不允许修改控件,在子线程中使一段时间后位置加1达到滚动效果,要判断到达最大值是要把所定义的位置索引归零
- List item
).所以必须开启一个Handler使用post方法修改控件