pager java代码
package com.example.month7day11.Evening1;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.example.month7day11.R;
import java.util.ArrayList;
import java.util.List;
public class Evening1_ViewPager extends AppCompatActivity {
//存放布局的list集合
private List<E1_Fragment> list;
private TextView E1ViewPagerTextView;
private RadioGroup E1ViewPagerRadioGroup;
private RadioButton E1ViewPagerRadioButton1;
private RadioButton E1ViewPagerRadioButton2;
private RadioButton E1ViewPagerRadioButton3;
private RadioButton E1ViewPagerRadioButton4;
private ViewPager E1ViewPagerViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_evening1__view_pager);
//初始化控件
E1ViewPagerTextView = (TextView) findViewById(R.id.E1_ViewPager_TextView);
E1ViewPagerRadioGroup = (RadioGroup) findViewById(R.id.E1_ViewPager_RadioGroup);
E1ViewPagerRadioButton1 = (RadioButton) findViewById(R.id.E1_ViewPager_RadioButton1);
E1ViewPagerRadioButton2 = (RadioButton) findViewById(R.id.E1_ViewPager_RadioButton2);
E1ViewPagerRadioButton3 = (RadioButton) findViewById(R.id.E1_ViewPager_RadioButton3);
E1ViewPagerRadioButton4 = (RadioButton) findViewById(R.id.E1_ViewPager_RadioButton4);
E1ViewPagerViewPager = (ViewPager) findViewById(R.id.E1_ViewPager_ViewPager);
//存放图片的id
int[] arr={R.mipmap.image1,R.mipmap.image2,R.mipmap.image3,R.mipmap.image4};
//初始化
list=new ArrayList<>( );
for (int i = 0; i < arr.length; i++) {
//新建布局。
E1_Fragment e1_fragment = new E1_Fragment();
//新建bundle传递数据
Bundle bun = new Bundle();
//添加数据
bun.putString("text","这是第"+(i+1)+"个布局。");
bun.putInt("image",arr[i]);
//数据添加到视图。
e1_fragment.setArguments(bun);
//添加到集合
list.add(e1_fragment);
}
//添加适配器,通过匿名类部类。
E1ViewPagerViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
//获取对应的布局。
@Override
public Fragment getItem(int i) {
return list.get(i);
}
//循环的次数。
@Override
public int getCount() {
return list.size();
}
});
//添加寻呼机(pager)滑动事件
E1ViewPagerViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//滑动的距离。
@Override
public void onPageScrolled(int i, float v, int i1) {
}
//滑动到的布局。
@Override
public void onPageSelected(int i) {
//设置默认选中的按钮。
E1ViewPagerRadioButton1.setChecked(true);
switch(i){
case 0:
//滑动到哪里就选中哪个按钮。
E1ViewPagerRadioButton1.setChecked(true);
break;
case 1:
//滑动到哪里就选中哪个按钮。
E1ViewPagerRadioButton2.setChecked(true);
break;
case 2:
//滑动到哪里就选中哪个按钮。
E1ViewPagerRadioButton3.setChecked(true);
break;
case 3:
//滑动到哪里就选中哪个按钮。
E1ViewPagerRadioButton4.setChecked(true);
break;
}
}
//滑动状态的改变。
@Override
public void onPageScrollStateChanged(int i) {
}
});
//按钮组添加点击事件。
E1ViewPagerRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//点击按钮切换到对应的布局。
switch(checkedId){
case R.id.E1_ViewPager_RadioButton1:
E1ViewPagerViewPager.setCurrentItem(0);
break;
case R.id.E1_ViewPager_RadioButton2:
E1ViewPagerViewPager.setCurrentItem(1);
break;
case R.id.E1_ViewPager_RadioButton3:
E1ViewPagerViewPager.setCurrentItem(2);
break;
case R.id.E1_ViewPager_RadioButton4:
E1ViewPagerViewPager.setCurrentItem(3);
break;
}
}
});
}
}
pager 对应的xml布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_below="@id/E1_ViewPager_TextView"
android:layout_above="@id/E1_ViewPager_RadioGroup"
android:id="@+id/E1_ViewPager_ViewPager"
>
</android.support.v4.view.ViewPager>
fragment对应的Java代码
package com.example.month7day11.Evening1;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.month7day11.R;
/**
-
A simple {@link Fragment} subclass.
-
Activities that contain this fragment must implement the
-
{@link E1_Fragment.OnFragmentInteractionListener} interface
-
to handle interaction events.
-
Use the {@link E1_Fragment#newInstance} factory method to
-
create an instance of this fragment.
-
布局中的fragment
*/
public class E1_Fragment extends Fragment {
//初始化控件。
private ImageView E1FragmentImageView;
private TextView E1FragmentTextView;// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = “param1”;
private static final String ARG_PARAM2 = “param2”;// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;private OnFragmentInteractionListener mListener;
public E1_Fragment() {
// Required empty public constructor
}/**
- Use this factory method to create a new instance of
- this fragment using the provided parameters.
- @param param1 Parameter 1.
- @param param2 Parameter 2.
- @return A new instance of fragment E1_Fragment.
*/
// TODO: Rename and change types and number of parameters
public static E1_Fragment newInstance(String param1, String param2) {
E1_Fragment fragment = new E1_Fragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//填充器获取视图对象。
View fragment = inflater.inflate(R.layout.fragment_e1_, container, false);
E1FragmentImageView = fragment.findViewById(R.id.E1_fragment_imageView);
E1FragmentTextView = fragment.findViewById(R.id.E1_fragment_textView);
//获取传过来的数据。
Bundle bun = getArguments();
String text = bun.getString(“text”);
int image = bun.getInt(“image”);
//设置数据。
E1FragmentImageView.setImageResource(image);
E1FragmentTextView.setText(text);
return fragment;
}// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}@Override
public void onAttach(Context context) {
super.onAttach(context);
// if (context instanceof OnFragmentInteractionListener) {
// mListener = (OnFragmentInteractionListener) context;
// } else {
// throw new RuntimeException(context.toString()
// + " must implement OnFragmentInteractionListener");
// }}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}/**
- This interface must be implemented by activities that contain this
- fragment to allow an interaction in this fragment to be communicated
- to the activity and potentially other fragments contained in that
- activity.
-
- See the Android Training lesson <a href=
- “http://developer.android.com/training/basics/fragments/communicating.html”
-
Communicating with Other Fragments for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
fragment对应的布局
<?xml version="1.0" encoding="utf-8"?><!-- TODO: Update blank fragment layout -->
<!--显示图片-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:id="@+id/E1_fragment_imageView"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#9C27B0"
android:textSize="50sp"
android:gravity="center"
android:layout_below="@id/E1_fragment_imageView"
android:id="@+id/E1_fragment_textView"
/>