Autore Topic: Problema SlidingTabLayout  (Letto 219 volte)

Offline Dreww

  • Nuovo arrivato
  • *
  • Post: 16
  • Respect: 0
    • Mostra profilo
Problema SlidingTabLayout
« il: 29 Dicembre 2015, 20:00:01 CET »
0
Salve a tutti, da un po ho un problema con lo SlidingTabLayout. Ho seguito un video su Youtube come tutorial ma mi esce così con l'ombra tra le due barre:

i codici che uso sono i seguenti:

MyAdapter


Codice (Java): [Seleziona]
package com.example.livechannels;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;

/**
 * Created by Ayoub on 20/12/2015.
 */

public class MyAdapter extends FragmentPagerAdapter {
    private Context mContext;
    private String [] titles = {"CANALI", "DOWNLOAD"};
    int[] icons = new int[]{R.mipmap.ic_ondemand_video_white_48dp,R.mipmap.ic_file_download_white_48dp};
    private int heightIcon;
    public MyAdapter(FragmentManager fm,Context c) {
        super(fm);

        mContext = c;
        double scale=c.getResources().getDisplayMetrics().density;
        heightIcon=(int)(24*scale+0.5f);

    }

    @Override
    public Fragment getItem(int position) {
        Fragment frag= null;

        if (position == 0) {
            frag = new FragmentSatu();
        }   else if (position == 1) {
            frag = new FragmentDua();
        }

        Bundle b = new Bundle();
        b.putInt("position", position);
        frag.setArguments(b);
        return frag;

    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Drawable d = mContext.getResources().getDrawable(icons[position]);
        d.setBounds(0,0,heightIcon,heightIcon);

        ImageSpan is  = new ImageSpan(d);

        SpannableString sp = new SpannableString(" ");
        sp.setSpan(is,0,sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return (sp);
    }
}


MainActivity.java

Codice (Java): [Seleziona]
package com.example.livechannels;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.example.livechannels.Tab.SlidingTabLayout;

public class MainActivity extends AppCompatActivity {
    private SlidingTabLayout mSlidingTabLayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        mViewPager=(ViewPager)findViewById(R.id.vp_tabs);
        mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(),this));

        mSlidingTabLayout=(SlidingTabLayout)findViewById(R.id.stl_tabs);
        mSlidingTabLayout.setDistributeEvenly(true);
        mSlidingTabLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
        mSlidingTabLayout.setCustomTabView(R.layout.tab_view, R.id.tv_tab);
        mSlidingTabLayout.setViewPager(mViewPager);
    }
}

FragmentDua.java

Codice (Java): [Seleziona]
package com.example.livechannels;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */

public class FragmentDua extends android.support.v4.app.Fragment {


    public FragmentDua() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_dua, container, false);
    }

FragmentSatu

Codice (Java): [Seleziona]
package com.example.livechannels;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentSatu extends android.support.v4.app.Fragment{


    public FragmentSatu() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_satu, container, false);
    }

}


tab_view.xml

Codice (XML): [Seleziona]
<?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"
   android:paddingTop="12dp"
   android:paddingBottom="12dp"
   android:gravity="center">

<TextView
   android:id="@+id/tv_tab"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

    </LinearLayout>

Activity_main
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

    <ImageView android:id="@+id/splashscreen"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:src="@drawable/ic_launcher"
       android:layout_gravity="center|top"
       android:contentDescription="@string/app_name"
       android:scaleType="fitXY" />

    <android.support.v7.widget.Toolbar
       android:id="@+id/my_awesome_toolbar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@color/colorPrimary"
       android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <com.example.livechannels.Tab.SlidingTabLayout
       android:id="@+id/stl_tabs"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></com.example.livechannels.Tab.SlidingTabLayout>
   
    <android.support.v4.view.ViewPager
       android:id="@+id/vp_tabs"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1" ></android.support.v4.view.ViewPager>

</LinearLayout>

Fragment_dua
Codice (XML): [Seleziona]
<FrameLayout 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"
   tools:context="com.example.livechannels.FragmentDua">

    <!-- TODO: Update blank fragment layout -->
    <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="@string/fragment_dua" />

</FrameLayout>

Fragment_satu
Codice (XML): [Seleziona]
<FrameLayout 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"
   tools:context="com.example.livechannels.FragmentSatu">

    <!-- TODO: Update blank fragment layout -->
    <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="@string/fragment_satu" />

</FrameLayout>


main_splash.xml

Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#ffffff"
   android:clickable="false">


    <com.example.livechannels.Tab.SlidingTabLayout
       android:id="@+id/stl_tabs"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></com.example.livechannels.Tab.SlidingTabLayout>

    <android.support.v4.view.ViewPager
       android:id="@+id/vp_tabs"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"></android.support.v4.view.ViewPager>

</RelativeLayout>


Come faccio a risolvere questo problemone?
Grazie mille
« Ultima modifica: 29 Dicembre 2015, 20:03:38 CET da Dreww »