亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用ViewPager+Fragment實現選項卡切換效果

發布時間:2020-06-20 05:00:50 來源:網絡 閱讀:1996 作者:想站著的豬 欄目:移動開發

參考鏈接:http://www.jianshu.com/p/7bc9a1ff137e

          http://www.devstore.cn/code/info/245.html

         http://www.tuicool.com/articles/FfeiumV

本人參考了三篇文章來寫這個頁面,但是運行這上面給出的源碼,發現每個fragment的flag并不能實現切換。

要實現的效果如下:

使用ViewPager+Fragment實現選項卡切換效果


在activity_main.xml中設置布局。xml內容如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context="com.example.fengzhengapp.MainActivity" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/tv_hot"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:background="#ffEAEAEA"
            android:gravity="center"
            android:text="@string/tab_hot"
            android:textSize="18sp" />
        <TextView
            android:id="@+id/tv_news"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:background="#ffEAEAEA"
            android:gravity="center"
            android:text="@string/tab_news"
            android:textSize="18sp" />
        <TextView
            android:id="@+id/tv_fav"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:background="#ffEAEAEA"
            android:gravity="center"
            android:text="@string/tab_favorite"
            android:textSize="18sp" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:orientation="horizontal">
        <View
            android:id="@+id/activity_order_flag_all"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="36dp"
            android:layout_marginRight="36dp"
            android:background="@color/app_style_red"
            android:gravity="center"/>
        <View
            android:id="@+id/activity_order_flag_uncomplete"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="36dp"
            android:layout_marginRight="36dp"
            android:background="@color/app_style_red"
            android:gravity="center"
            android:visibility="invisible"/>
        <View
            android:id="@+id/activity_order_flag_complete"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="36dp"
            android:layout_marginRight="36dp"
            android:background="@color/app_style_red"
            android:gravity="center"
            android:visibility="invisible"/>
    </LinearLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/myViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:flipInterval="30" >
    </android.support.v4.view.ViewPager>
</LinearLayout>


接下來,增加3個Fragment布局頁 ,分別在里面填充簡單的內容

第一個 :

<?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/txtHot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="this is the hot tab" >
    </TextView>
</LinearLayout>


第二個 :

<?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/txtNews"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="frag_1"
        android:textSize="30dp"
        android:textStyle="bold"
        android:textColor="#000000" />
</LinearLayout>


第三個 :

<?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/txtFav"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="this is the Fav tab" >
    </TextView>
</LinearLayout>


以上3個Fragment的布局文件已創建完畢,每個文件中只顯示簡單的文本內容,用做演示。

加載3個Fragment到Activity中。

首先實現3個Fragment對應的后臺類

熱門布局頁對應的類:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.ganinfo.interfacepractice.R;

public class FragmentHot extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmenthot, container, false);
        return view;
    }
}

news布局頁對應的類:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.ganinfo.interfacepractice.R;
public class FragmentNews extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentnews, container, false);
        return view;
    }
}


收藏布局頁對應的類:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.ganinfo.interfacepractice.R;
public class FragmentFavorite extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentfav, container, false);
        return view;
    }
}


之后再activity中初始化這3個Fragment

注意要點:

MainActivity繼承自FragmentActivity

要實現一個FragmentPagerAdapter,內容如下:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
public class MyFragmentAdapter extends FragmentPagerAdapter {
    ArrayList<Fragment> list;
    public MyFragmentAdapter(FragmentManager fm,ArrayList<Fragment> list){
        super(fm);
        this.list = list;
    }
    @Override
    public Fragment getItem(int arg0) {
        return list.get(arg0);
    }
    @Override
    public int getCount() {
        return list.size();
    }
}


然后在MainActivity中實現切換和動畫效果,代碼如下:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import adapter.MyFragmentAdapter;
import fragment.FragmentFavorite;
import fragment.FragmentHot;
import fragment.FragmentNews;
public class MainActivity extends FragmentActivity implements View.OnClickListener{
    private ViewPager mViewPager;
    private ArrayList fragments;
    private TextView view1, view2, view3;
    private int mCurrentOption = 0;
    private View flag0, flag1, flag2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViewPager();
        initView();
    }
    @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void initView(){
        view1 = (TextView) findViewById(R.id.tv_hot);
        view2 = (TextView) findViewById(R.id.tv_news);
        view3 = (TextView) findViewById(R.id.tv_fav);
        flag0 = findViewById(R.id.activity_order_flag_all);
        flag1 = findViewById(R.id.activity_order_flag_uncomplete);
        flag2 = findViewById(R.id.activity_order_flag_complete);
        view1.setOnClickListener(this);
        view2.setOnClickListener(this);
        view3.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_hot:
                if(mCurrentOption == 0) return;
                mCurrentOption = 0;
                flag0.setVisibility(View.VISIBLE);
                flag1.setVisibility(View.INVISIBLE);
                flag2.setVisibility(View.INVISIBLE);
                mViewPager.setCurrentItem(mCurrentOption);
                break;
            case R.id.tv_news:
                if(mCurrentOption == 1) return;
                mCurrentOption = 1;
                flag0.setVisibility(View.INVISIBLE);
                flag1.setVisibility(View.VISIBLE);
                flag2.setVisibility(View.INVISIBLE);
                mViewPager.setCurrentItem(mCurrentOption);
                break;
            case R.id.tv_fav:
                if(mCurrentOption == 2) return;
                mCurrentOption = 2;
                flag0.setVisibility(View.INVISIBLE);
                flag1.setVisibility(View.INVISIBLE);
                flag2.setVisibility(View.VISIBLE);
                mViewPager.setCurrentItem(mCurrentOption);
                break;
            default:
                return;
        }
    }
    private void initViewPager(){
        mViewPager = (ViewPager)findViewById(R.id.myViewPager);
        fragments = new ArrayList<Fragment>();
        Fragment fragmentHot = new FragmentHot();
        Fragment fragmentNews = new FragmentNews();
        Fragment fragmentFav = new FragmentFavorite();
        fragments.add(fragmentHot);
        fragments.add(fragmentNews);
        fragments.add(fragmentFav);
        mViewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragments));
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
            }
            @Override
            public void onPageSelected(int i) {
                switch (i){
                    case 0:
                        flag0.setVisibility(View.VISIBLE);
                        flag1.setVisibility(View.INVISIBLE);
                        flag2.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        flag0.setVisibility(View.INVISIBLE);
                        flag1.setVisibility(View.VISIBLE);
                        flag2.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        flag0.setVisibility(View.INVISIBLE);
                        flag1.setVisibility(View.INVISIBLE);
                        flag2.setVisibility(View.VISIBLE);
                        break;
                    default:
                        return;
                }
            }
            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }
}


-------------------------------------------------------------------------------------

package com.ganinfo.collect.activity;

import java.util.ArrayList;

import com.ganinfo.collect.R;
import com.ganinfo.collect.fragment.GzMyFragment;
import com.ganinfo.collect.fragment.GzTaskFragment;
import com.ganinfo.collect.utils.GzLog;
import com.ganinfo.collect.utils.GzNetwork;
import com.ganinfo.collect.widget.MyFragmentAdapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("ResourceAsColor") public class GzMainActivity extends GzBaseActivity {
	
	private String TAG = "GzMainActivity";
	
	private long lastTime = 0; //再按一次退出的時間標志
	private ArrayList<android.support.v4.app.Fragment> fragments;
	private int mCurrentOption = 0;
	private ViewPager mViewPager;
	private ImageView mTaskIV,mTaskMyIV;
	private TextView mTaskTV,mTaskMyTV;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		GzLog.d(TAG, "onCreate");
		initview();
		initViewPager();
		CheckNetwork();
	}
	
	 private void initview() {
		 mTaskIV = (ImageView) findViewById(R.id.main_iv_shop);
		 mTaskMyIV = (ImageView) findViewById(R.id.main_iv_my);
		 mTaskTV = (TextView) findViewById(R.id.main_tv_shop);
		 mTaskMyTV = (TextView) findViewById(R.id.main_tv_my);
		 
		 
	}

	@Override
     public boolean onOptionsItemSelected(MenuItem item) {

       int id = item.getItemId();
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }
	
	private void CheckNetwork() {
		if(!GzNetwork.isNetworkAvailable()){
			new AlertDialog.Builder(this).
			setMessage(R.string.network_not_avail).
			setPositiveButton(R.string.ok, null).
			create().show();
		}
	}

	 private void initViewPager(){
	        mViewPager = (ViewPager)findViewById(R.id.main_viewpager);
	        fragments = new ArrayList<android.support.v4.app.Fragment>();
	        GzTaskFragment fragmentTask = new GzTaskFragment();
	        GzMyFragment fragmentMy = new GzMyFragment();
	        fragments.add(fragmentTask);
	        fragments.add(fragmentMy);
	        mViewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragments));
	        mViewPager.setCurrentItem(mCurrentOption);
	        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
				
				@Override
				public void onPageSelected(int i) {
					if(mCurrentOption == i){
					}
				}
				
				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {
				}
				
				@Override
				public void onPageScrollStateChanged(int arg0) {
				}
			});
	    }
	
	 @SuppressLint("ResourceAsColor") 
	 public void onClickTab(View view){
			switch (view.getId()) {
			case R.id.main_fg_task:
				if(mCurrentOption == 0) return;
                mCurrentOption = 0;
                mViewPager.setCurrentItem(mCurrentOption);
                mTaskTV.setTextColor(R.color.app_style_normal);
				break;
			case R.id.main_fg_my:
				if(mCurrentOption == 1) return;
                mCurrentOption = 1;
                mViewPager.setCurrentItem(mCurrentOption);
                mTaskMyTV.setTextColor(R.color.app_style_normal);
				break;
			default:
	            return;
			}
			
		}
	 
	@Override
	public void onBackPressed() {
		if(System.currentTimeMillis() - lastTime > 2000){
			showToast(R.string.back_hint);
			lastTime = System.currentTimeMillis();
			return;
		}
		super.onBackPressed();
	}
}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

罗平县| 二连浩特市| 临汾市| 永宁县| 衢州市| 个旧市| 成安县| 镇宁| 五华县| 恩施市| 孟村| 桐柏县| 介休市| 昭平县| 平陆县| 保山市| 太湖县| 嘉黎县| 原平市| 嘉鱼县| 梁平县| 玛多县| 水城县| 仙游县| 石阡县| 中宁县| 关岭| 汝城县| 长岭县| 诏安县| 凌云县| 德江县| 浑源县| 中山市| 南华县| 昆明市| 修文县| 新田县| 佛山市| 乌兰县| 牡丹江市|