ViewPagerIndicator: TabPagerIndicator not visible

百般思念 提交于 2019-12-10 22:07:00

问题


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;

    }
}``

回答1:


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>


来源:https://stackoverflow.com/questions/22931555/viewpagerindicator-tabpagerindicator-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!