gpt4 book ai didi

java - Android SwipeViews + TitleStrip + ListView

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:47 25 4
gpt4 key购买 nike

我的新项目由一个带有 6 个 fragment 的抽屉导航组成(奥迪/宝马/起亚...汽车品牌)。我希望每个 fragment 都有 2 个其他 fragment ( Models ,其中将有一个带有汽车模型的 ListView ,第二个 fragment 称为 Pictures ,很明显它包含什么)。我不喜欢选项卡,这就是为什么我选择在 fragment 之间进行滑动导航的 TitleStrip

宝马.java

public class BMW extends Fragment {

CollectionPagerAdapter mCollectionPagerAdapter;

ViewPager mViewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(com.zenyt.R.layout.fragment_bmw, container, false);

mCollectionPagerAdapter = new CollectionPagerAdapter(
getFragmentManager());

// Set up action bar.
final ActionBar actionBar = getActivity().getActionBar();

// Specify that the Home button should show an "Up" caret, indicating
// that touching the
// button will take the user one step up in the application's hierarchy.
actionBar.setDisplayHomeAsUpEnabled(true);

// Set up the ViewPager, attaching the adapter.
mViewPager = (ViewPager) rootView.findViewById(R.id.bmw_pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
return rootView;
}

public class CollectionPagerAdapter extends FragmentStatePagerAdapter {

final int NUM_ITEMS = 2; // number of tabs

public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i) {
Fragment fragment = new TabFragment();
Bundle args = new Bundle();
args.putInt(TabFragment.ARG_OBJECT, i);
fragment.setArguments(args);
return fragment;
}

@Override
public int getCount() {
return NUM_ITEMS;
}

@Override
public CharSequence getPageTitle(int position) {

String tabLabel = null;
switch (position) {
case 0:
tabLabel = getString(R.string.label1);
break;
case 1:
tabLabel = getString(R.string.label2);
break;
}

return tabLabel;
}
}
public static class TabFragment extends Fragment {

public static final String ARG_OBJECT = "object";
private ListView myListView;
private String[] strListView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

Bundle args = getArguments();
int position = args.getInt(ARG_OBJECT);

int tabLayout = 0;
switch (position) {
case 0:
tabLayout = R.layout.models;
break;
case 1:
tabLayout = R.layout.pictures;
break;
}

View rootView = inflater.inflate(tabLayout, container, false);

return rootView;
}
}
}

TabFragment中,我想要 fragment 而不是布局(模型图片)。我怎样才能做到这一点?

模型.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/black"
>

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView2" />
</RelativeLayout>

模型.java

public class Models extends Fragment {

private ListView myListView;
private String[] strListView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(com.zenyt.R.layout.models, container, false);

myListView = (ListView) rootView.findViewById(com.zenyt.R.id.listView2);
strListView = getResources().getStringArray(com.zenyt.R.array.bmw_list_data);
ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, strListView);
myListView.setAdapter(objAdapter);

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



}
});
return rootView;
}
}

最佳答案

看来是我自己解决了。我所要做的就是稍微更改一下 PagerAdapter,如下所示:

 public class CollectionPagerAdapter extends FragmentStatePagerAdapter {

public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return new Bmw1();
case 1:
return new Bmw2();
default:
break;
}
return null;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}

@Override
public CharSequence getPageTitle(int position) {

String tabLabel = null;
switch (position) {
case 0:
tabLabel = getString(R.string.label1);
break;
case 1:
tabLabel = getString(R.string.label2);
break;
}
return tabLabel;
}
}

关于java - Android SwipeViews + TitleStrip + ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27766345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com