我要创建一个不可浏览的ViewPager。我已经写了这段代码
MainActivity
public class MainActivity extends FragmentActivity {
ViewPagerAdattatore adattatoreViewPager;
DrawerLayout drawerLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedIstanceState) {
super.onCreate(savedIstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.viewPager);
drawerLayout=(DrawerLayout)findViewById(R.id.navDrawer);
listaNavDrawer=(ListView)findViewById(R.id.listaDrawer);
adattatoreViewPager=new ViewPagerAdattatore(getSupportFragmentManager());
viewPager.setAdapter(adattatoreViewPager);
}
}
ViewPagerAdattatore
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdattatore extends FragmentPagerAdapter {
public final static int PAGINE=1;
Fragment fragment;
public ViewPagerAdattatore(FragmentManager fragmentManager) {
super(fragmentManager);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0: return new HomePage();
case 1: return new Informazioni();
// case 3: return new TerzaPagina();
}
fragment=new HomePage();
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGINE;
}
}
Informazioni
public class Informazioni extends Fragment {
TextView textView;
View view;
public View onCreateView(Bundle savedIstanceState, LayoutInflater inflater, ViewGroup parent) {
view=inflater.inflate(R.layout.home_page, parent, false);
textView=(TextView)view.findViewById(R.id.textView1);
textView.setText("welcome");
return view;
}
}
HomePage作为informazioni的相同代码
ViewPagerNoSwipe
public class ViewPagerNoSwipe extends ViewPager {
public ViewPagerNoSwipe(Context context) {
super(context);
}
public ViewPagerNoSwipe(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return false;
}
}
activity_main
<?XML version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/navDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mypackage.name.utils.ViewPagerNoSwipe
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ListView
android:id="@+id/listaDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@null"
android:dividerHeight="0dp"
android:background="#2F2F2F"/>
</android.support.v4.widget.DrawerLayout>
主页布局
<?XML version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:layout_marginTop="30dp"
android:fontFamily="sans-serif-light"/>
</RelativeLayout>
当我执行此代码时,我没有看到TextView!为什么?我的代码中的错误在哪里?
答案 0 :(得分:0)
试试这个 -
public class Informazioni extends Fragment {
TextView textView;
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.home_page, container, false);
textView = (TextView) view.findViewById(R.id.textView1);
textView.setText("welcome");
return view;
}
}