昨天贴出了Animtion动画的 一些xml属性及具体相关代码,今天开始上
ViewPager+Fragment的代码。
MainActivity.java
public class MainActivity extends FragmentActivity implements OnPageChangeListener {
private ViewPager viewPager;
private PagerTitleStrip viewPagerTitle;
private List<Fragment> list;
private List<RadioButton> titlelist;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
getWindow().getDecorView().setBackgroundDrawable(new BitmapDrawable(FitTheScreenSizeImage(BitmapFactory.decodeResource(getResources(), R.drawable.scene),480,860)));
viewPager=(ViewPager)this.findViewById(R.id.viewPager);
list=new ArrayList<Fragment>();
list.add(new AsmeFragment());
list.add(new SFragment());
list.add(new FtpFragment());
//list.add(view2);
//list.add(view3);
titlelist=new ArrayList<RadioButton>();
titlelist.add((RadioButton)findViewById(R.id.t1));
titlelist.add((RadioButton)findViewById(R.id.t2));
titlelist.add((RadioButton)findViewById(R.id.t3));
titlelist.get(0).setTextColor(0xFF9999FF);
titlelist.get(0).setOnClickListener(new MyOnClickListener(0));
titlelist.get(1).setOnClickListener(new MyOnClickListener(1));
titlelist.get(2).setOnClickListener(new MyOnClickListener(2));
//titlelist.add("乐库");
//titlelist.add("FTP");
MyAdapter mAdapter = new MyAdapter(this.getSupportFragmentManager(), list);
viewPager.setAdapter(mAdapter);
viewPager.setOnPageChangeListener(this);
viewPager.setCurrentItem(0);// 设置成当前第一个
}
private class MyOnClickListener implements OnClickListener {
private int index;
public MyOnClickListener(int index) {
this.index = index;
}
@Override
public void onClick(View v) {
viewPager.setCurrentItem(index);// 把viewpager的视图切过去,实现title跟pager的联动
titlelist.get(index).setChecked(true);// 设置被选中,否则布局里面的背景不会切换
}
}
/**
*
* 这个方法让背景图适应屏幕大小,可以忽略
*/
public static Bitmap FitTheScreenSizeImage(Bitmap m,int ScreenWidth, int ScreenHeight)
{
float width = (float)ScreenWidth/m.getWidth();
float height = (float)ScreenHeight/m.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(width,height);
return Bitmap.createBitmap(m, 0, 0, m.getWidth(), m.getHeight(), matrix, true);
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public MyAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
list = fragments;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
super.destroyItem(container, position, object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
return super.instantiateItem(container, position);
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return super.isViewFromObject(view, object);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
/**
*页面被选中之后的回调
*/
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Log.i("slide", "onPageSelected+agr0=" + arg0);
for(int i=0;i<titlelist.size();i++){
titlelist.get(i).setChecked(false);
titlelist.get(i).setTextColor(Color.WHITE);
}
titlelist.get(arg0).setChecked(true);// 保持页面跟按钮的联动
//titlelist.get(arg0).setButtonDrawable(R.drawable.bar);
titlelist.get(arg0).setTextColor(0xFF9999FF);
}
}
main.xml
ps:这可以进行自定义的radioButton,比如加下划线,背景图,颜色等
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/scene"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioGroup
android:id="@+id/titlegroup"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginTop="3dp"
android:layout_marginRight="2dp"
android:button="@null"
android:checked="true"
android:text="我的 "
android:textSize="18sp" />
<RadioButton
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:button="@null"
android:text="乐库 "
android:textColor="@android:color/white"
android:textSize="18sp" />
<RadioButton
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:button="@null"
android:textSize="18sp"
android:textColor="@android:color/white"
android:text="FTP" />
</RadioGroup>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginTop="3dp"
android:background="#1f6495ED"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
</android.support.v4.view.ViewPager>
</LinearLayout>
这里就不把AsmeFragment等具体贴出来了,因为里面的代码涉及Volley,JSON解析之类,代码量挺多的,暂时写个简单demo Fragment上去,后面会把项目整个代码放上去
AsmeFragment.java
ps:Fragment的跳转用getActivity();其他的Fragment demo类似这个
public class AsmeFragment extends Fragment {
private View view;
/**
*
* 创建View
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(view==null){
view =inflater.inflate(R.layout.fragment_1, container, false);
/***
* 这里接下来可以进行这个fragement_1中的一些操作
* 使用view.findViewById(R.id.××);
*/
}
ViewGroup parent=(ViewGroup)view.getParent();
if(parent!=null){
parent.removeView(view);
}
return view;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
fragment_1.xml
<?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"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30sp"
android:text="AsmeFragment" />
</LinearLayout>
这个ViewPager+Fragment的主要用法写到这里,在后面的时候博主会分享Fragment嵌套的相关出错情况及应用,下次给大家带来
ContentResolver的相关用法