我现在正在使用viewpagerindicator。当我使用titlePageIndicator时,一切正常。但是当我尝试使用tabpageindicator时,我看不到它。
它不会出现在HierarchyView中。我尝试了很多方法,比如实现getTitle和getPageTitle,但都没有成功。当我将布局高度设置为100px时,我可以看到条,但没有选项卡。很高兴能得到帮助。
提前谢谢你。
这是我的代码:

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

这里是片段:
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);
    }
}

我的适配器:
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;

    }
}``

最佳答案

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

10-08 19:13