我尝试在ViewPager中使用Fragment,但ListView滚动不起作用。
这是我的代码:
public class MeuTreino extends FragmentDrawerModel
{ //1 editar, 2 novo, 3 remover
ViewPager Tab;
MeuTreinoTPA TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meu_treino);
getActionBar().setTitle("Meu treino");
init();
TabAdapter = new MeuTreinoTPA(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
}
@Override
public void onTabSelected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
}
};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Treinos").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Histórico").setTabListener(tabListener));
}
My TreinosFragment类
public class TreinosFragment extends Fragment{
private ArrayList<Treino> treinos;
private AdapterTreino adapter;
private View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_meu_treino, container, false);
configurarListaTreino();
return view;
}
private void carregarTreino()
{
treinos = Treino.getTreinos(getActivity());
adapter = new AdapterTreino(getActivity(), treinos);
ListView ltDados = (ListView)getView().findViewById(R.id.lvTreinos);
configurarListaTreino();
ltDados.setAdapter(adapter);
}
我的XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<Button
android:id="@+id/bCriarTreino"
style="@style/BotaoTabPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="criarTreino"
android:text="NOVO TREINO" />
</RelativeLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
第二个XML
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lvHistoricoTreino"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lvTreinos"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
你怎么看,我有一个FragmentActivity,每个都有一个ListView的两个片段,在fragmentActivity的布局中,我只有一个底部按钮。
有人可以帮助我吗?