Android安卓viewpager和Fragment的结合,多个Fragment视图之间切换等运用

2025-01-02 14:21:25
推荐回答(1个)
回答1:

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 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 list;

    public MyAdapter2(FragmentManager fm, List 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


    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/beijing"
    >

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="第一个"
        android:id="@+id/button"
        android:onClick="click2"
        />


***************************************************************

///pop  ///////

    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
            android:id="@+id/tvpop"
        android:text="popupwindow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

*************************************************
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;
    }
}