我是ViewPager
的新手。所以我接受了一个教程,现在一切正常。在教程之后,我有一个带有TextView的ViewPager
。
现在,我想向此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();
}
}
答案 0 :(得分: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();
}
}
答案 1 :(得分: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