我的选项卡视图Fragement显示空白屏幕

时间:2015-10-29 08:53:18

标签: android gridview tabs

首先抱歉我的英文不好,在标签视图中我的片段没有显示任何内容它显示空白屏幕任何人帮助我,如果我试图显示任何文本它工作正常我试图显示网格视图它会显示空白页,我猫日志没有错误

Tab1.java

public class Tab1 extends Fragment {
private GridviewAdapter mAdapter;
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;

private GridView gridView;

@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.tab_1,container,false);
    gridView = (GridView)v. findViewById(R.id.gridview);
    gridView.setAdapter(mAdapter);


    gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

        }
    });

    return v;
}


public void prepareList()
{
      listCountry = new ArrayList<String>();

      listCountry.add("india");
      listCountry.add("Brazil");
      listCountry.add("Canada");
      listCountry.add("China");



      listFlag = new ArrayList<Integer>();
      listFlag.add(R.drawable.india);
      listFlag.add(R.drawable.brazil);
      listFlag.add(R.drawable.canada);
      listFlag.add(R.drawable.china);

}

}

我的adapter.java

public class GridviewAdapter extends BaseAdapter{

private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private LayoutInflater inflater;
private static Context mcontext;


public GridviewAdapter(Context context,ArrayList<String> listCountry, ArrayList<Integer> listFlag) {
    super();
    this.listCountry = listCountry;
    this.listFlag = listFlag;
    this.mcontext = context;
}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listCountry.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return listCountry.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}
public static class ViewHolder
{
    public ImageView imgViewFlag;
    public TextView txtViewTitle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder view;


    if(convertView==null)
    {
         inflater =  (LayoutInflater)mcontext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        view = new ViewHolder();
        convertView = inflater.inflate(R.layout.grid_one, null);

        view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
        view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);

        convertView.setTag(view);
    }
    else
    {
        view = (ViewHolder) convertView.getTag();
    }

    view.txtViewTitle.setText(listCountry.get(position));
    view.imgViewFlag.setImageResource(listFlag.get(position));

    return convertView;
}

tab.xml

android:layout_height="match_parent">
<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"

    android:numColumns="2" />

gridviewrow.xml

<ImageView 
    android:layout_height="64dp" 
    android:id="@+id/imageView1" 
    android:layout_width="64dp" 

    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true">
</ImageView>

<TextView 
    android:text="TextView" 
    android:layout_height="wrap_content"
    android:id="@+id/textView1" 
    android:layout_width="wrap_content"
    android:layout_below="@+id/imageView1" 
    android:layout_marginTop="2dp"
    android:layout_centerHorizontal="true"
    android:textSize="18sp"
    android:ellipsize="marquee"></TextView>

我的标签适配器类

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created


// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
    super(fm);

    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;

}

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        Tab1 tab1 = new Tab1();
        return tab1;
    }
    else             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
        Tab2 tab2 = new Tab2();
        return tab2;
    }

}

// This method return the titles for the Tabs in the Tab Strip

@Override
public CharSequence getPageTitle(int position) {
    return Titles[position];
}

// This method return the Number of tabs for the tabs Strip

@Override
public int getCount() {
    return NumbOfTabs;
}

} 在我的空屏幕下面这是我的问题请任何人帮助解决此问题

enter image description here

0 个答案:

没有答案