Android安卓viewpager和Fragment的结合,多个Fragment视图之间切换等运用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android安卓viewpager和Fragment的结合,多个Fragment视图之间切换等运用相关的知识,希望对你有一定的参考价值。
android安卓viewpager和Fragment的结合,多个Fragment视图之间切换等运用
参考技术A package com.example.myapplication;import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity implements ViewPager.OnPageChangeListener
List<Fragment> list;
private ViewPager vp;
RadioButton[] rb;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
vp= (ViewPager) findViewById(R.id.vp);
rg=(RadioGroup)findViewById(R.id.rg);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
rb3=(RadioButton)findViewById(R.id.rb3);
rb=new RadioButton[3];
rb[0]=rb1;
rb[1]=rb2;
rb[2]=rb3;
list=new ArrayList<>();
list.add(new Fragment1());
list.add(new Fragment2());
list.add(new Fragment3());
MyAdapter2 adapter2=new MyAdapter2(getSupportFragmentManager(),list);
vp.setAdapter(adapter2);
vp.addOnPageChangeListener(this);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
for (int i=0;i<3;i++)
if(rb[i].getId()==checkedId)
vp.setCurrentItem(i);
break;
);
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
rb[position].setChecked(true);
@Override
public void onPageScrollStateChanged(int state)
@Override
protected void onDestroy()
super.onDestroy();
vp.removeOnPageChangeListener(this);
public void click2(View view)
View inflate = View.inflate(this, R.layout.pop, null);
PopupWindow window=new PopupWindow(inflate, AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.WRAP_CONTENT);
window.setOutsideTouchable(true);
window.setBackgroundDrawable(new ColorDrawable());
window.showAsDropDown(view,0,0);
public void click3(View view)
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent intent=PendingIntent.getActivity(this,11,
new Intent(this,Main2Activity.class),PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)//设置小图标
//.setContentText("要下课了,去哪玩啊")//设置通知的内容,内容靠左
.setContentTitle("新消息通知")
.setContentInfo("要下课了,去哪玩啊")//设置通知的内容,内容靠右
.setContentIntent(intent)//设置延迟意图,从通知栏跳到哪
.setDefaults(Notification.DEFAULT_ALL)//设置通知的提示方式
.setAutoCancel(true)//设置点击自动取消通知
;//设置标题
manager.notify(1,builder.build());
public void click4(View view)
Intent intent=new Intent(this,MyService.class);
intent.putExtra("msg","我就是服务,已经开始了");
startService(intent);
****************************************************
package com.example.myapplication;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
/**
* Created by 猴嫂 on 2017/11/18.
*/
public class MyAdapter2 extends FragmentStatePagerAdapter
List<Fragment> list;
public MyAdapter2(FragmentManager fm, List<Fragment> list)
super(fm);
this.list = list;
@Override
public Fragment getItem(int position)
return list.get(position);
@Override
public int getCount()
return list.size();
**********************************************************
//package com.example.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by 猴嫂 on 2017/11/18.
*/
public class Fragment1 extends Fragment
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View inflate = inflater.inflate(R.layout.fra1, null);
return inflate;
//如上可多个
***********************
// fra1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/beijing"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="第一个"
android:id="@+id/button"
android:onClick="click2"
/>
</LinearLayout>
***************************************************************
///pop ///////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvpop"
android:text="popupwindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
*************************************************
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
/**
* Created by 猴嫂 on 2017/11/18.
*/
public class MyService extends Service
public MyService()
int i=0;
//开始执行服务内容
@Override
public int onStartCommand(Intent intent, int flags, int startId)
String msg = intent.getStringExtra("msg");
Log.i("onStartCommand","onStartCommand:"+msg);
new Thread(new Runnable()
@Override
public void run()
for (i=0;i<=100;i++)
Log.i("onStartCommand","onStartCommand:"+i);
try
Thread.sleep(200);
catch (InterruptedException e)
e.printStackTrace();
).start();
return START_STICKY;
@Override
public IBinder onBind(Intent intent)
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
//销毁服务
@Override
public void onDestroy()
super.onDestroy();
i=101;
以上是关于Android安卓viewpager和Fragment的结合,多个Fragment视图之间切换等运用的主要内容,如果未能解决你的问题,请参考以下文章
安卓界面之Viewpager和Tablayout实现滑动界面
Android中使用ViewPager实现屏幕页面切换和页面轮播效果
android 安卓异步加载网络图片,与viewpager结合使用示例