想做個應用市場,效果如圖,下面的是一級標簽,用的TabHost跳轉Activity實現。然後上面這一排是二級標簽,想用TextView + ViewPager + Fragment 實現,現在滑動有效,但是數據加載不出來。求指教,捉急一整天了。
這是“推薦”對應的Activity:
public class CommendActivity extends FragmentActivity {
private static final int INDEX_COM = 0;
private static final int INDEX_NEW = 1;
private static final int INDEX_HOT = 2;
private static final int INDEX_GUESS = 3;
private ImageView comPointer; // 指針
private TextView comCom, comNew, comHot, comGuess; // 標簽
private ViewPager comPager;
private PagerAdapter pagerAdapter;
private List<CommendFragment> fragments;
private int currentPage = 0; // 當前二級頁
private int delta = 0; // 指針單位移動量
private int offset = 0; // 指針移動偏移值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_commend);
initPointer();
initTextView();
initViewPager();
}
/*
* 配置標簽
*/
private void initTextView() {
comCom = (TextView) findViewById(R.id.comCom);
comNew = (TextView) findViewById(R.id.comNew);
comHot = (TextView) findViewById(R.id.comHot);
comGuess = (TextView) findViewById(R.id.comGuess);
comClickListener listener = new comClickListener();
comCom.setOnClickListener(listener);
comNew.setOnClickListener(listener);
comHot.setOnClickListener(listener);
comGuess.setOnClickListener(listener);
}
/*
* 配置標簽指針
*/
private void initPointer() {
comPointer = (ImageView) findViewById(R.id.comPointer);
int imageWidth = BitmapFactory.decodeResource(getResources(), R.drawable.pointer).getWidth();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
delta = dm.widthPixels / 4;
offset = (delta - imageWidth) / 2;
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
comPointer.setImageMatrix(matrix);
}
/*
* 配置ViewPager
*/
private void initViewPager() {
comPager = (ViewPager) findViewById(R.id.comPager);
fragments = new ArrayList<CommendFragment>();
CommendFragment comFragment = CommendFragment.newInstance(INDEX_COM);
CommendFragment newFragment = CommendFragment.newInstance(INDEX_NEW);
CommendFragment hotFragment = CommendFragment.newInstance(INDEX_HOT);
CommendFragment guessFragment = CommendFragment.newInstance(INDEX_GUESS);
fragments.add(comFragment);
fragments.add(newFragment);
fragments.add(hotFragment);
fragments.add(guessFragment);
comPager.setAdapter(new TabsAdapter(getSupportFragmentManager(),
fragments));
comPager.setCurrentItem(currentPage);
comPager.setOnPageChangeListener(new ComPageChangeListener());
}
private class comClickListener implements OnClickListener {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.comCom:
comPager.setCurrentItem(INDEX_COM);
break;
case R.id.comNew:
comPager.setCurrentItem(INDEX_NEW);
break;
case R.id.comHot:
comPager.setCurrentItem(INDEX_HOT);
break;
case R.id.comGuess:
comPager.setCurrentItem(INDEX_GUESS);
break;
}
}
}
private class ComPageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Toast.makeText(
CommendActivity.this,
"this is "
+ ((TabsAdapter)comPager.getAdapter()).getItem(position), Toast.LENGTH_SHORT)
.show();
Animation anim = new TranslateAnimation(delta * currentPage, delta
* position, 0, 0);
currentPage = position;
anim.setFillAfter(true);
anim.setDuration(300);
comPointer.startAnimation(anim);
}
}
}
Adapter的java代碼:
public class TabsAdapter extends FragmentPagerAdapter {
private List<CommendFragment> fragments;
public TabsAdapter(FragmentManager fm, List<CommendFragment> list) {
super(fm);
this.fragments = list;
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
}
Fragment的java代碼:
public class CommendFragment extends Fragment {
private static final int DEFAULT_TAG = -1;
private int tag;
private TextView tv;
/**
* 添加標識,返回帶標識的對象。
*
* @param tag
* 標識
* @return 帶標識的Fragment
*/
public static CommendFragment newInstance(int tag) {
CommendFragment fragment = new CommendFragment();
Bundle b = new Bundle();
b.putInt("tag", tag);
fragment.setArguments(b);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getArguments();
tag = (null == b ? DEFAULT_TAG : b.getInt("tag"));
}
/**
* 解析布局
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_com, container, false);
tv = (TextView) view.findViewById(R.id.empty);
tv.setText(tv.getText().toString() + tag);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
把適配器裡面的這個方法去掉就可以了,我也是折騰了一天