带有片段的android登录界面?

时间:2016-06-01 01:39:40

标签: android android-fragments login

我的应用程序包含:

  • 工具栏。
  • viewpager(fragments)。
  • 标签(slidingtablayout)。

现在我想实现登录屏幕。 但我不想为此做第二次活动。我想用片段来做,

为此我做了:

  • 第二个ViewPager(涵盖我上面提到的所有视图|让它称之为loginViewPager)
  • loginfragment

登录后我想将loginViewPager设置为不可见或只是将其删除。

有更好的方法吗? |我确定有一个更好的方法,但我找不到一个。请不要关闭这个:)。

MainActivity:

public class MainActivity extends ActionBarActivity {

ViewPager viewPager; 
ViewPager loginViewPager;   
MyPagerAdapter pageAdapter;
LoginPagerAdapter loginPagerAdapter;
List<Fragment> fragments;
List<Fragment> loginfragments;
SlidingTabLayout tabLayout;
Toolbar toolbar;
android.support.v7.app.ActionBar actionBar;
DrawerLayout drawerLayoutGesamt;
ActionBarDrawerToggle drawerToggle;



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                    .setDefaultFontPath("fonts/proximanova.otf")
                    .setFontAttrId(R.attr.fontPath)
                    .build()
    );


    drawerLayoutGesamt = (DrawerLayout) findViewById(R.id.drawerlayoutgesamt);
    drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayoutGesamt, R.string.auf, R.string.zu);
    drawerLayoutGesamt.setDrawerListener(drawerToggle);

    toolbar = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar);

    SpannableString s = new SpannableString("App");
    s.setSpan(new TypefaceSpan(this, "proximanova.otf"), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    drawerToggle.syncState();
    actionBar.setTitle(s);

    tabLayout = (SlidingTabLayout) findViewById(R.id.tabLayout);
    tabLayout.setCustomTabView(R.layout.custom_tab, 0);
    tabLayout.setDistributeEvenly(true);

    fragments = getFragments();
    pageAdapter = new MeinPageAdapter(getSupportFragmentManager(), fragments, this);

    viewPager = (CustomViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(pageAdapter);
    viewPager.setPagingEnabled(false);
    viewPager.setCurrentItem(1);
    tabLayout.setViewPager(viewPager);

    loginfragments = getLoginFragments();

    loginViewPager = (CustomViewPager) findViewById(R.id.viewPagerLogin);
    loginPagerAdapter = new LoginPagerAdapter(getSupportFragmentManager(),loginfragments);
    loginViewPager.setAdapter(loginPagerAdapter);
    loginViewPager.setPagingEnabled(false);
    loginViewPager.setCurrentItem(0);


}

public List<Fragment> getFragments() {

    List<Fragment> newFragments = new ArrayList<Fragment>();

    newFragments.add(MyFragment.newInstance());
    newFragments.add(MyFragment.newInstance());
    newFragments.add(MyFragment.newInstance());

    return newFragments;
}

public List<Fragment> getLoginFragments() {

    List<Fragment> newLoginFragment = new ArrayList<>();

    NewFragment.add(LoginFragment.newInstance());

    return newLoginFragment;
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.ahmed.ahmed.MainActivity"
    android:background="@drawable/footerlayout"
    android:id="@+id/layout">


<android.support.v4.widget.DrawerLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:id="@+id/drawerlayoutgesamt">

    <!-- Activitylayout -->

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/activitylayout"
        android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:layout_width="fill_parent"
            android:layout_height="55dp"
            android:id="@+id/toolbar1"
            app:titleTextAppearance="@style/Toolbar.TitleText"
            android:background="@drawable/titlebar3">
        </android.support.v7.widget.Toolbar>


            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/toolbar1">


                <android.support.v4.view.ViewPager
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" >

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

            </FrameLayout>

    </RelativeLayout>

    <!-- Drawerlayout -->

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#041513"
        android:id="@+id/drawerlayoutsingle"
        android:layout_gravity = "start">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Drawer Layout"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>


<com.example.user.user.SlidingTabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabLayout"
    android:elevation="2dp"
    android:tabStripEnabled="false"
    android:gravity="center"
    android:background="#30a094"
    android:layout_alignParentBottom="true" />


<!-- LoginViewPager -->

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

MyPagerAdapter:

public class MyPagerAdapter扩展了FragmentStatePagerAdapter {

private final List<Fragment> fragments;
private CharSequence Titles[];
private Context context;

int icons [] = {R.drawable.sstar, R.drawable.u8, R.drawable.pperson};
Drawable drawable;

public MeinPageAdapter(FragmentManager fm, List<Fragment> fragments, Context context) {
    super(fm);
    this.fragments = fragments;
    this.context = context;
}

@Override
public CharSequence getPageTitle(int position) {
    drawable = context.getResources().getDrawable(icons[position]);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    SpannableString sb = new SpannableString(" ");
    ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

@Override
public int getCount() {
    return this.fragments.size();
}


}

LoginPagerAdapter

public class LoginPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments;


public LoginPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

@Override
public int getCount() {
    return this.fragments.size();
}
}

LoginFragment

public class LoginFragment extends Fragment {

public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
Button btn;
MeinPageAdapter pageAdapter;
CustomViewPager viewPager;
SlidingTabLayout tabLayout;

public static final LoginFragment newInstance()
{
    LoginFragment mf = new LoginFragment();
    Bundle bd = new Bundle(1);
    mf.setArguments(bd);
    return mf;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
    View v = inflater.inflate(R.layout.fragment_login, container, false);

    btn = (Button) v.findViewById(R.id.buttonChangeView);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //  v.findViewById(R.id.viewPagerLogin).setVisibility(View.GONE);

        }
    });

    return v;
}



}

login_fragment.xml

<?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="#000000">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Login"
    android:id="@+id/textView2"
    android:layout_marginTop="200dp"
    android:layout_gravity="center"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Kill loginViewPager"
    android:id="@+id/buttonChangeView"
    android:layout_below="@+id/textView2"
    android:layout_gravity="center" />

</LinearLayout>

0 个答案:

没有答案