0

我是新来的ViewPager。所以我参加了一个教程,现在一切正常。教程结束后,我有ViewPager一个 TextView 。现在,我想在ImageView这个TextView. 我必须从哪里开始?

这是我的代码:

public static final MyFragment newInstance(String message){
    MyFragment f  = new MyFragment();
    Bundle bdl = new Bundle(1);
    bdl.putString(EXTRA_MESSAGE, message);
    f.setArguments(bdl);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
    String message = getArguments().getString(EXTRA_MESSAGE);
    View v = inflater.inflate(R.layout.activity_my_fragment, container, false);
    TextView messageTextView = (TextView)v.findViewById(R.id.tvTest);
    messageTextView.setText(message);

return v;
}

 public class Bilderbuch extends FragmentActivity {
        MyPageAdapter pageAdapter;
        int id;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_bilderbuch);
            id = getIntent().getIntExtra("kinderid", 0);
            List<Fragment> fragments = getFragments();

            pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

            ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
            pager.setAdapter(pageAdapter);

        }

        private List<Fragment> getFragments(){
            List<Fragment> fList = new ArrayList<Fragment>();

            DBHelper dbh = new DBHelper(this);
            Cursor c = dbh.selectEreignis(id);
            int i = 0;
             while(c.moveToNext())
             { 
                 fList.add(MyFragment.newInstance(c.getString(c.getColumnIndex("ereignisname"))));
                 i++;
             }
            fList.add(MyFragment.newInstance("Fragment 1"));
            fList.add(MyFragment.newInstance("Fragment 2"));
            fList.add(MyFragment.newInstance("Fragment 3"));

            return fList;
        }

        private class MyPageAdapter extends FragmentPagerAdapter {
            private List<Fragment> fragments;

            public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
                super(fm);
                this.fragments = fragments;
            }
            @Override
            public Fragment getItem(int position) {
                return this.fragments.get(position);
            }

            @Override
            public int getCount() {
                return this.fragments.size();
            }
        }
4

2 回答 2

0

你必须写一个新的片段。在 Fragment 的布局中,您必须添加一个 ImageView。比你不能在你的 MyPagerAdapter 中做类似下面的事情:

   private class MyPageAdapter extends FragmentPagerAdapter {
            private List<Fragment> fragments;

            public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
                super(fm);
                this.fragments = fragments;
            }
          @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return new MyFragment();
            case 1:
                return new ImageFragment(R.drawable.my_image); // The second view will return your ImageFragment

            default:
                return null;
            }
        }

            @Override
            public int getCount() {
                return this.fragments.size();
            }
        }
于 2013-06-06T10:21:16.320 回答
0

你有“activity_my_fragment.xml” inisde 它把这个代码和图像 src 放在一起。

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/your_image_name_inside_resDrawable_folder"    
    />  

编辑:

使用这篇文章:http ://architects.dzone.com/articles/android-tutorial-using

于 2013-06-06T10:32:50.917 回答