对于Android中的编码,我几乎是一个新手,我想知道如何使用滑动视图制作动作栏。
我的代码:http://pastebin.com/iHZn27H3
错误
Unknown entity 'ViewPager' on line 11
Unknown entity 'ViewPager' on line 21
Unknown type of field 'mViewPager' on line 21
Unknown type of field 'mViewPager' on line 22
Unknown method on line 22
Unknown entity 'ViewPager' on line 23
Unknown type of field 'mViewPager' on line 34
Unknown mrthod on line 34
如何解决这些错误?
编辑:错误现在已经消失,但我无法切换标签我的滑动,只需单击即可。代码:http://pastebin.com/iHZn27H3
答案 0 :(得分:1)
我不知道您使用的是哪个IDE,但在Intellij中您必须转到“模块设置”> '图书馆'>点击加号> Java> libs(文件夹)> Android的支持,v4.jar。在Eclipse中,您可能必须转到“构建路径”> '配置构建路径'> 'Java构建路径'> '添加JAR'> 'libs'>机器人支撑-v4.jar
扩展http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/作为示例:
<强> Tab1Fragment.java:强>
public class Tab1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.live, container,false);
TextView tv = (TextView) view.findViewById(R.id.status);
tv.setText("Fragment1");
return view;
}
}
<强> Tab2Fragment.java:强>
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.live, container,false);
TextView tv = (TextView) view.findViewById(R.id.status);
tv.setText("Fragment2");
return view;
}
}
<强> live.xml:强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/status"/>
</LinearLayout>
主要活动(ViewPagerFragmentActivity.java):
public class ViewPagerFragmentActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
//initialsie the pager
this.initialisePaging();
}
private void initialisePaging() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
}
}
<强> main_activity.xml:强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
<强> PagerAdapter.java:强>
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PagerAdapter(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();
}
}