在viewpager中放置textview而不是layout

时间:2015-06-15 16:15:47

标签: android android-viewpager

我想将Textview放在viewpager内,而不是布局。我将textview的ID传递给了instantiateItem方法中的适配器。但它没有用。

我该怎么办?

这是我的CustomPagerAdapeter代码:

package com.example.pager;

import android.content.Context;
import android.graphics.Color;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created with IntelliJ IDEA.
 * User: marzieh
 * Date: 6/15/15
 * Time: 8:00 PM
 * To change this template use File | Settings | File Templates.
 */
public class CustomPagerAdapeter extends PagerAdapter {

    public Object instantiateItem(View collection,int position) {
        LayoutInflater inflater= (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int reId=0;
        TextView []txts=new TextView[8];
        String[] names=new String[]{"aaaaaaaa","bbbbbbb","ccccccc","ddddddd","eeeeeee","fffffff","gggggggg","hhhhhhhh"};
        for(int i=0;i<txts.length;i++)
        {
            txts[i].setText(names[i]);
            txts[i].setTextColor(Color.parseColor("#ffffff"));
            txts[i].setBackgroundColor(Color.parseColor("#000000"));
            txts[i].setId(i);
        }

        reId=txts[position].getId();
       /* switch (position)
        {
            case 0:
                reId=R.layout.p1;
                break;
            case  1:
                reId = R.layout.p2;
                break;
        }*/



        View view=inflater.inflate(reId,null);
        ((ViewPager)collection).addView(view,0);
        return  view;



    }

    @Override
    public int getCount() {
        return 8;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
       return  view==((View)o);
    }
    @Override
    public Parcelable saveState()
    {

        return null;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }
}

0 个答案:

没有答案