綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 我使用ViewPager 相關問題 求幫助

[複製連結] 查看: 2536|回覆: 6|好評: 0
跳轉到指定樓層
樓主
GUO0630 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2018-6-2 00:57

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
本帖最後由 GUO0630 於 2018-6-6 17:23 編輯

我怕我表達得不是很好我的意思是指如圖

這是我主頁的程式碼
package com.example.wells.ig;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class ighome extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

       // GridView gridView;
       // String letterList [] ={};
       // int lettersIcon[] ={R.drawable.dog,R.drawable.dog_0,R.drawable.dog_1,
         //       R.drawable.dog_2,R.drawable.dog_3,R.drawable.dog_4};

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ighome);

       // final Button like=(Button)findViewById(R.id.like);
       // like.setOnClickListener(
         //       new View.OnClickListener()
        //{
          //          public void onClick(View v)
            //       {
              //        like.setBackground(getResources().getDrawable(R.drawable.dog));
                //    }
               // });

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

       // gridView =(GridView)findViewById(R.id.gridview);

        //gridView.setAdapter(adapter);

    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_ighome, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            switch (position){
                case 0:
                     tab1  t1 = new tab1();
                     return  t1;
                case 1:
                    tab2 t2 =new tab2();
                    return  t2;
                case 2:
                    tab3 t3=new tab3();
                    return  t3;
                case 3:
                    tab4 t4=new tab4();
                    return t4;
                case 4:
                    tab5 t5=new tab5();
                    return t5;
                    default:
                        return  null;
            }
        }

        @Override
        public int getCount() {
            // Show 5 total pages.
            return 5;
        }
        }
    }
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
muska1116 | 收聽TA | 只看該作者
發表於 2018-6-2 10:59
不太懂你的意思,而且好像也沒看到圖QQ
這裡大概提供一些方法給 你參考看看…


如果你希望你的 ViewPager 不能用手勢來左右滑動的話,可以使用自定義的 ViewPage 來覆寫 touch 事件


public class ViewPageWithoutSwipe extends ViewPager {
   
    private boolean mSwipeEnable = false;


    public ViewPageWithoutSwipe(Context context) {
        super(context);
    }


    public ViewPageWithoutSwipe(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mSwipeEnable;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return mSwipeEnable;
    }


    @Override
    public boolean canScrollHorizontally(int direction) {
        return mSwipeEnable;
    }
}



如果你要的是點上面的 tab 單純切換頁面不要有滑動動畫的話,使用自定義的 OnTabSelectedListener,覆寫 onTabSelected,在 setCurrentItem 裡面多加一個 false 的參數


tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        mViewPager.setCurrentItem(tab.getPosition(), false);
    }
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }
    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }
});

以上應該就符合你的需求了吧(我猜啦XD)

評分

參與人數 1碎鑽 +2 幫助 +2 收起 理由
woolala + 2 + 2 很給力!

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
 樓主| GUO0630 | 收聽TA | 只看該作者
發表於 2018-6-5 20:50
謝謝你 我用TAB做可以了:))
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
 樓主| GUO0630 | 收聽TA | 只看該作者
發表於 2018-6-5 21:00
還有個問題  我想在點擊中間那個++時讓Tabs 消失  我只想出現 圖庫 照相 影片 這些   該怎麼寫

這是我的JAVA
package com.example.wells.ig;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class tab3  extends Fragment {
    private TabLayout tabLayout;

    private ViewPager viewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab3, container, false);
        viewPager =rootView.findViewById(R.id.viewpaper_id);
        tabLayout =rootView.findViewById(R.id.tablayout_id);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

      adapter.AddFragment(new FragmentPhoto(),"圖庫");
      adapter.AddFragment(new FragmentPhoto_all(), "拍照");
      adapter.AddFragment(new FragmentVod(), "影片");
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        return rootView;
    }


}

XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpaper_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout_id"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabTextColor="@color/colorgary_"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/viewpaper_id">

    </android.support.design.widget.TabLayout>

</android.support.constraint.ConstraintLayout>

最後總tab xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ighome"
    tools:layout_editor_absoluteY="25dp">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent">

        <android.support.design.widget.TabItem
            android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/home_pic"
            android:icon="@drawable/home_pic" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/search_pic" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/photo_pic" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/love_pic" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/person_pic" />
    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/tabs"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
 樓主| GUO0630 | 收聽TA | 只看該作者
發表於 2018-6-6 13:42
如果我想讓這個畫面的tab消失 就是房子、放大鏡那條  剩拍照相簿那些 我要改哪??
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
 樓主| GUO0630 | 收聽TA | 只看該作者
發表於 2018-6-6 13:43
這是我tab3的 java
package com.example.wells.ig;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class tab3  extends Fragment {
    private TabLayout tabLayout;

    private ViewPager viewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab3, container, false);
        viewPager =rootView.findViewById(R.id.viewpaper_id);
        tabLayout =rootView.findViewById(R.id.tablayout_id);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

      adapter.AddFragment(new FragmentPhoto_all(), "圖庫");
        adapter.AddFragment(new FragmentPhoto(),"拍照");
      adapter.AddFragment(new FragmentVod(), "影片");
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        return rootView;
    }


}
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

7
muska1116 | 收聽TA | 只看該作者
發表於 2018-6-6 22:49
本帖最後由 muska1116 於 2018-6-6 22:54 編輯

不知道你的 ++ 有沒有要保留,兩種都幫你寫了

private final static int TAB_PLUS_INDEX = 2;

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        // hideAndShowOtherTab(tab, TAB_PLUS_INDEX);
        hideAllTab(tab, TAB_PLUS_INDEX);
    }
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }
    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        // hideAndShowOtherTab(tab, TAB_PLUS_INDEX);
        hideAllTab(tab, TAB_PLUS_INDEX);
    }
});

private void hideAndShowOtherTab(TabLayout.Tab tab, int keepIndex) {
    if (tab.getPosition() == keepIndex) {
        for (int i = 0; i < ((ViewGroup) tabLayout.getChildAt(0)).getChildCount(); i++) {
            View tabView = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
            if (i != keepIndex) {
                tabView.setVisibility(tabView.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
            }
        }
    }
}
private void hideAllTab(TabLayout.Tab tab, int tabIndex) {
    if (tab.getPosition() == tabIndex) {
        tabLayout.setVisibility(View.GONE);
    }
}

評分

參與人數 1碎鑽 +2 經驗 +1 幫助 +2 收起 理由
woolala + 2 + 1 + 2 讚一個!

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則