Android - Horizo​​ntalScrollView中的多个ListView

时间:2013-07-02 15:29:42

标签: android listview scrollview horizontalscrollview

请查看商店中的Contacts + app以了解我的意思。您有三个ListView,每个都有屏幕宽度。然后,您可以使用Horizo​​ntalScrollView滚动每个。最好的方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以使用带有三个片段的viewpager作为标签,使用标签适配器,如下所示。

主持人活动:

    public class PageViewActivity extends FragmentActivity {
      MyPageAdapter pageAdapter;
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);
        List<Fragment> fragments = getFragments();
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
        ViewPager pager =
(ViewPager)findViewById(R.id.viewpager);
        pager.setAdapter(pageAdapter);
      }
    }

片段适配器

class MyPageAdapter extends FragmentPagerAdapter {
  private List<Fragment> fragments;

  public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
  }
  @Override 
  public Fragment getItem(int position) {
    return this.fragments.get(position);
  }

  @Override
  public int getCount() {
    return this.fragments.size();
  }
}

获取片段的方法(可选取决于您希望如何使用片段)

private List<Fragment> getFragments(){
  List<Fragment> fList = new ArrayList<Fragment>();

  fList.add(MyFragment.newInstance("Fragment 1"));
  fList.add(MyFragment.newInstance("Fragment 2")); 
  fList.add(MyFragment.newInstance("Fragment 3"));

  return fList;

你的片段类

public class MyFragment extends Fragment {
 public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

 public static final MyFragment newInstance(String message)
 {
   MyFragment f = new MyFragment();
   Bundle bdl = new Bundle(1);
   bdl.putString(EXTRA_MESSAGE, message);
   f.setArguments(bdl);
   return f;
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
   String message = getArguments().getString(EXTRA_MESSAGE);
   View v = inflater.inflate(R.layout.myfragment_layout, container, false);
   TextView messageTextView = (TextView)v.findViewById(R.id.textView);
   messageTextView.setText(message);

   return v;
 }
}

如果您需要更多帮助,请随时与我联系。