ViewPagerIndicator: TabPagerIndicator not visible
Asked Answered
C

1

3

im struggeling with the viewpagerindicator right now. When i use TitlePageIndicator, everything works fine. But when i try to use the TabPageIndicator I cant see it.

It does not appear in the hierarchieview. I tried a lot of things like implement getTitle and getPagetitle, but it didnt work. When i set the layout_height to 100px i can see the bar, but there are no tabs. Really happy about any help.

Thank you in advance.

Here is my code:

<?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:orientation="vertical" >

    <com.viewpagerindicator.TabPageIndicator
    android:id="@+id/titles"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

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


</RelativeLayout>

Here the Fragment:

package register;

import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.smartpos.R;
import com.example.smartpos.database.SmartPOSContentProvider;
import com.example.smartpos.database.TablesContract.Category;
import com.viewpagerindicator.CirclePageIndicator;
import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitlePageIndicator;

public class AritcleButtonsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{


    private CursorPagerAdapter<CategoryHolderFragment> adap;

    private ViewPager pager;

    public AritcleButtonsFragment(){

    }

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState){

        String[] projection = {Category.COLUMN_NAME_CATEGORY_ID,Category.COLUMN_NAME_TITLE };


        View view = inflater.inflate(R.layout.article_buttons,container, false);

        adap = new CursorPagerAdapter<CategoryHolderFragment>(getChildFragmentManager(), CategoryHolderFragment.class, projection, null);

        pager = (ViewPager)view.findViewById(R.id.pager);
        pager.setAdapter(adap);
        pager.setOffscreenPageLimit(3);
        pager.setBackgroundColor(Color.GRAY);

        TabPageIndicator indicator =(TabPageIndicator)view.findViewById(R.id.titles);
        indicator.setViewPager(pager);
        indicator.setBackgroundColor(Color.BLUE);

        getLoaderManager().initLoader(0, savedInstanceState, this);

        return view;        
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        String[] projection = { Category.COLUMN_NAME_CATEGORY_ID, Category.COLUMN_NAME_TITLE };
        CursorLoader cursorLoader = new CursorLoader(getActivity().getApplicationContext(),
        SmartPOSContentProvider.CONTENT_URI_CATEGORIES, projection, null, null, null);

          return cursorLoader;
    }
    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
        // TODO Auto-generated method stub
        adap.swapCursor(arg1);

    }
    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // TODO Auto-generated method stub
        adap.swapCursor(null);
    }
}

And my adapter:

package register;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class CursorPagerAdapter<F extends Fragment> extends
        FragmentStatePagerAdapter {
    private final Class<F> fragmentClass;
    private final String[] projection;
    private Cursor cursor;

    public CursorPagerAdapter(FragmentManager fm, Class<F> fragmentClass,
            String[] projection, Cursor cursor) {
        super(fm);
        this.fragmentClass = fragmentClass;
        this.projection = projection;
        this.cursor = cursor;
    }

    @Override
    public F getItem(int position) {
        if (cursor == null) 
            return null;

        cursor.moveToPosition(position);
        F frag;
        try {
            frag = fragmentClass.newInstance();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        Bundle args = new Bundle();
        for (int i = 0; i < projection.length; ++i) {
            args.putString(projection[i], cursor.getString(i));
        }
        frag.setArguments(args);
        return frag;
    }

    @Override
    public int getCount() {
        if (cursor == null)
            return 0;
        else
            return cursor.getCount();
    }

    public void swapCursor(Cursor c) {
        if (cursor == c)
            return;

        this.cursor = c;
        notifyDataSetChanged();
    }

    public Cursor getCursor() {
        return cursor;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        cursor.moveToPosition(position);
        String title = cursor.getString(1);
        return title;

    }


    public String getTitle(int position) {
        cursor.moveToPosition(position);
        String title = cursor.getString(1);
        return title;

    }
}``
Coenzyme answered 8/4, 2014 at 8:27 Comment(0)
L
4

try this on your xml :

<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">

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

<com.viewpagerindicator.TabPageIndicator
        android:id="@+id/titles"
        android:padding="10dip"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        />
</LinearLayout>
Liverpool answered 8/4, 2014 at 11:58 Comment(3)
Thank you for your suggestion. When i use your xml-Layout the application crashes. It says that the ViewPager cannot be cast to the TabPageIndicator. But I didnt change the Id so it is a bit confusing why this happens.Coenzyme
can you clean your project in eclipseLiverpool
I cleaned it now and now it works. The bar for the tabpageindicator is visible now. But there are still no "Pagetitles". I assume it could be something with the styling. But i really have no clue what i could change here.Coenzyme

© 2022 - 2024 — McMap. All rights reserved.