我创建了一个类似于 android market 的测试示例 viewpager 选项卡。但是所有选项卡都显示相同的 listView 项目。我需要以这样一种方式实现,即每个选项卡在 listView 中都有自己的一组项目。
这是我的主要活动。
package com.example.viewpagesample;
import java.util.ArrayList;
import java.util.List;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.viewpagerindicator.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
public class MainActivity extends SherlockFragmentActivity {
private ViewPager mPager;
private TitlePageIndicator mIndicator;
private MainPagerAdapter mAdapter;
private List<Fragment> mFragments;
private static final String FRAGMENT1 = Fragment1.class.getName();
private static final String FRAGMENT2 = Fragment1.class.getName();
private static final String FRAGMENT3 = Fragment1.class.getName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add fragments
mFragments = new ArrayList<Fragment>();
mFragments.add(Fragment.instantiate(this, FRAGMENT1));
mFragments.add(Fragment.instantiate(this, FRAGMENT2));
mFragments.add(Fragment.instantiate(this, FRAGMENT3));
// adapter
mAdapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments);
// pager
mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setAdapter(mAdapter);
// indicator
mIndicator = (TitlePageIndicator) findViewById(R.id.title_indicator);
mIndicator.setViewPager(mPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}
片段1类:
package com.example.viewpagesample;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment1 extends SherlockFragment {
private ListView mList;
@Override
public View onCreateView(LayoutInflater inf, ViewGroup grp, Bundle icicle) {
View v = inf.inflate(R.layout.f_fragment1, grp, false);
mList = (ListView) v.findViewById(R.id.listView1);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, Cheese.STRINGS);
mList.setAdapter(adapter);
}
}
Cheese.STRINGS 只是一个字符串数组。
片段2类:
package com.example.viewpagesample;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment2 extends SherlockFragment {
private ListView mList;
@Override
public View onCreateView(LayoutInflater inf, ViewGroup grp, Bundle icicle) {
View v = inf.inflate(R.layout.f_fragment2, grp, false);
mList = (ListView) v.findViewById(R.id.listView2);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, Cheese.fragment2);
mList.setAdapter(adapter);
}
}
在 fragment2 中,我使用 Cheese.fragment2 作为 listView 中的项目
但是 tab2 正在显示与 tab11 相同的 listView 项目?
如何在 tab2 中加载不同的项目?