android viewpager不显示任何内容

时间:2014-08-28 09:55:59

标签: java android android-viewpager fragment

我正在片段中使用viewpager制作一些Android应用程序。它显示标签正确,但我无法在代码中看到任何内容。我试图在stackoverflow和谷歌搜索和搜索,但我不知道为什么这个代码不起作用。代码看起来很奇怪,我很抱歉。我是这里的第一个问题。谢谢我帮忙: - )

这是MainActivity.java

package com.yeungnam.food;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;

List<DrawerItem> dataList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initializing
    dataList = new ArrayList<DrawerItem>();
    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

   /* mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START); */

    dataList.add(new DrawerItem(true));

    dataList.add(new DrawerItem("Message", R.drawable.ic_action_camera));
    dataList.add(new DrawerItem("Likes", R.drawable.ic_action_cloud));
    dataList.add(new DrawerItem("Games", R.drawable.ic_action_email));

    adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
            dataList);

    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer,  R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        SelectItem(1);
    }
}

public void SelectItem(int possition) {

    Fragment fragment = null;
    Bundle args = new Bundle();
    switch (possition) {
        case 1:
            fragment = new FragmentFood();
      /*      args.putString(FragmentFood.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentFood.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID()); */
            break;
        case 2:
            fragment = new FragmentFood();
      /*      args.putString(FragmentFood.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentFood.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID()); */
            break;
        case 3:
            fragment = new FragmentFood();
      /*      args.putString(FragmentFood.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentFood.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());  */
            break;
        default:
            break;
    }

    fragment.setArguments(args);
    FragmentManager frgManager = getFragmentManager();
    frgManager.beginTransaction().replace(R.id.content_frame, fragment)
            .commit();

    mDrawerList.setItemChecked(possition, true);
    setTitle(dataList.get(possition).getItemName());
    mDrawerLayout.closeDrawer(mDrawerList);

}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return false;
}

private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        if(dataList.get(position).isFirst == false)
            SelectItem(position);

    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
} */
}

这是FragmentFood.java

 package com.yeungnam.food;

 import android.app.Fragment;
 import android.content.Context;
 import android.os.Bundle;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentPagerAdapter;
 import android.support.v4.view.PagerAdapter;
 import android.support.v4.view.ViewPager;
 import android.util.TypedValue;
 import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;

 import com.astuetz.PagerSlidingTabStrip;

 public class FragmentFood extends Fragment {


private PagerSlidingTabStrip tabs;
private ViewPager pager;
private FoodAdapter adapter;



public FragmentFood() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_food, container,
            false);

    tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
    pager = (ViewPager) view.findViewById(R.id.pager);
    adapter = new FoodAdapter(getActivity());

    pager.setAdapter(adapter);


    tabs.setViewPager(pager);


    return view;
}


public class FoodAdapter extends PagerAdapter {

    private final String[] TITLES = { "breafkast", "lunch", "dinner" };
    private LayoutInflater mInflater;

    public FoodAdapter(Context c) {
        super(); mInflater = LayoutInflater.from(c);
    }

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

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Object instantiateItem(ViewGroup pager, int position) {
        // looks a little bit messy here
        TextView v = new TextView(getActivity());
        v.setBackgroundResource(R.color.background_window);
        v.setText("PAGE " + (position + 1));
        final int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources()
                .getDisplayMetrics());
        v.setPadding(padding, padding, padding, padding);
        v.setGravity(Gravity.CENTER);
        pager.addView(v, 0);
        return v;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object view) {
        container.removeView((View) view);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return false;
    }



}



}

1 个答案:

答案 0 :(得分:0)

我也遇到了类似的问题,但我使用的是FragmentAdapter。你可以参考以下内容:

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

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

    textViewMesuemInfo = (TextView) view.findViewById(R.id.textViewMesuemInfo);
    textViewOpenInfo = (TextView) view.findViewById(R.id.textViewOpenInfo);

    fragmentAdapter = new IntroPagerAdapter(**getChildFragmentManager()**);
    sliViewPager = (ViewPager) view.findViewById(R.id.viewPagerMain);
    sliViewPager.setOnPageChangeListener(pagerChangeListener);
    sliViewPager.setAdapter(fragmentAdapter);
    sliViewPager.setCurrentItem(0);


    return view;
}




private class IntroPagerAdapter extends FragmentPagerAdapter {

    public IntroPagerAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public Fragment getItem(int position) {
        Log.i("position=",position+"");
        switch (position) {
        case 0:
            return new IntroFragment().setLayout(R.layout.fragment_colckbuild);
        case 1:
            return new IntroFragment().setLayout(R.layout.fragment_playclock);
        case 2:
            return new IntroFragment().setLayout(R.layout.fragment_measureinfo);
        case 3:
            return new IntroFragment().setLayout(R.layout.fragment_playclock);
        default:
            return null;
        }
    }