public class Fragment1 extends Fragment { private LinearLayout mGallery; private int[] mImgIds; private LayoutInflater mInflater; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, container, false); //存图片到数组 mImgIds=new int[]{ R.drawable.ad, R.drawable.as,R.drawable.cc,R.drawable.ddd }; mGallery=(LinearLayout)view.findViewById(R.id.id_gallery); // mInflater = LayoutInflater.from(getActivity()); // View view1 = mInflater.inflate(R.layout.fragment1item, null, false); for (int i=0;i<mImgIds.length;i++){ View view1 = View.inflate(getActivity(), R.layout.fragment1item, null); ImageView img = (ImageView) view1.findViewById(R.id.fm1_image); img.setImageResource(mImgIds[i]); mGallery.addView(view1); } return view; } }public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private TextView baozhi,mulu,daohang; private Fragment fm1,fm2; private ArrayList<TextView> list=new ArrayList<>(); private ArrayList<Fragment> mlist=new ArrayList<>(); private ViewPager mvp; private FragmentPagerAdapter mad; private String[] datas = {"选项1", "选项2", "选项3", "选项4", "选项5"}; private ImageView you; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 setContentView(R.layout.activity_main); initview(); } private void initview() { mvp=(ViewPager)findViewById(R.id.view_pager); you=(ImageView)findViewById(R.id.you) ; you=(ImageView)findViewById(R.id.you) ; daohang=(TextView)findViewById(R.id.daohang) ; baozhi=(TextView)findViewById(R.id.baozhi); mulu=(TextView)findViewById(R.id.mulu); list.add(baozhi); list.add(mulu); fm1=new Fragment1(); fm2=new Fragment2(); mlist.add(fm1);mlist.add(fm2); FragmentManager fm = getSupportFragmentManager(); mad=new Fragmentadapter(fm); mvp.setAdapter(mad); mvp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //当页面为选中页时设置TextView颜色为蓝,否则为黑 for (int i=0;i<list.size();i++){ if(i==position){ list.get(i).setTextColor(Color.BLUE); }else { list.get(i).setTextColor(Color.BLACK); } } } @Override public void onPageScrollStateChanged(int state) { } }); //目录导航的点击事件 daohang.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO: 2016/5/17 构建一个popupwindow的布局 View popupView = MainActivity.this.getLayoutInflater().inflate(R.layout.popupwindow, null); // TODO: 2016/5/17 为了演示效果,简单的设置了一些数据,实际中大家自己设置数据即可,相信大家都会。 ListView lsvMore = (ListView) popupView.findViewById(R.id.lsvMore); lsvMore.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, datas)); // TODO: 2016/5/17 创建PopupWindow对象,指定宽度和高度 PopupWindow window = new PopupWindow(popupView, 200, 700); // TODO: 2016/5/17 设置动画 // window.setAnimationStyle(R.style.popup_window_anim); // TODO: 2016/5/17 设置背景颜色 window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8"))); // TODO: 2016/5/17 设置可以获取焦点 window.setFocusable(true); // TODO: 2016/5/17 设置可以触摸弹出框以外的区域 window.setOutsideTouchable(true); // TODO:更新popupwindow的状态 window.update(); // TODO: 2016/5/17 以下拉的方式显示,并且可以设置显示的位置 window.showAsDropDown(daohang, 0, 20); } }); //右上角头像的点击事件 you.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.baozhi: mvp.setCurrentItem(0); break; case R.id.mulu: mvp.setCurrentItem(1); break; } } }
popupwindow_bg.xml<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- dashWidth指的是边线的长度 dashGap 指的是每条线之间的间距 width指的是边线的宽度 --> <stroke android:width="2dp" android:color="#00AAEE" android:dashGap="2dp" android:dashWidth="10dp" /> <corners android:bottomLeftRadius="2dp" android:bottomRightRadius="2dp" android:topLeftRadius="2dp" android:topRightRadius="2dp" /> </shape><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:background="#AA444444" android:scrollbars="none"> <LinearLayout android:id="@+id/id_gallery" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:orientation="horizontal" > </LinearLayout> </HorizontalScrollView> </LinearLayout>
Android--------在fragment滚动播放
最新推荐文章于 2018-12-30 17:31:43 发布