如何使用View Pager实现List View和其他片段?

时间:2014-01-27 11:26:01

标签: android listview android-fragments android-viewpager

我正在尝试使用ViewViewr与ListView和其他片段。列表视图将用作菜单,其他片段只显示文本。

我已经设法使用TextView制作ViewPager,但我无法弄清楚如何实现List视图片段。我试图在网上找到例子,但我无法找到我需要的东西。

这就是我现在所拥有的 https://gist.github.com/anonymous/8646895

这是主要的课程

  public class MainActivity extends FragmentActivity {

MyPageAdapter pageAdapter;
List<Fragment>fragments;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, MyFragment.class.getName()));
    fragments.add(Fragment.instantiate(this, FragmentB.class.getName()));

    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(pageAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



 private 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();
     }
 }

}

2 个答案:

答案 0 :(得分:1)

您必须使用ListFragment。创建ListFragment并在ViewPager内显示。下面给出了一个简单的ListFragment代码

<强> SimpleListFragment.java

public class SimpleListFragment extends ListFragment  {  

String[] numbers_text = new String[] { "one", "two", "three", "four",  
     "five", "six", "seven", "eight", "nine", "ten", "eleven",  
     "twelve", "thirteen", "fourteen", "fifteen" };  

 String[] numbers_digits = new String[] { "1", "2", "3", "4", "5", "6", "7",  
        "8", "9", "10", "11", "12", "13", "14", "15" };  

  @Override  
  public void onListItemClick(ListView l, View v, int position, long id) {  
  new CustomToast(getActivity(), numbers_digits[(int) id]);     
  }  

  @Override  
  public View onCreateView(LayoutInflater inflater, ViewGroup container,  
    Bundle savedInstanceState) {  
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(  
     inflater.getContext(), android.R.layout.simple_list_item_1,  
     numbers_text);  
   setListAdapter(adapter);  
   return super.onCreateView(inflater, container, savedInstanceState);  
  }  
 } 

在主要活动的ViewPager的getItem()

if (index == 0) {
            SimpleListFragment list = new SimpleListFragment ();
            return list;
        }

了解更多详情click here

答案 1 :(得分:0)

按如下方式创建新的fragment

public class SuperAwesomeCardFragment extends Fragment {
    CustomAdapter customAdapter;
    ArrayList<String> values;
    public static SuperAwesomeCardFragment superAwesomeFragment;
    /**
     * Creating new instance of the fragment
     * 
     * @param position
     * @return fragment
     */
    public static SuperAwesomeCardFragment newInstance(int position) {
        superAwesomeFragment = new SuperAwesomeCardFragment();

        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        superAwesomeFragment.setArguments(b);
        return superAwesomeFragment;
    }



@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            position = getArguments().getInt(ARG_POSITION);
                values.add("First");
               values.add("second");        
        }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mContainer = container;
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        FrameLayout fl = new FrameLayout(getActivity());
        fl.setLayoutParams(params);

        final int margin = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
                        .getDisplayMetrics());
        ListView mListView = null;
        mList = commomList(mListView, params, margin);
        fl.addView(mList);
        return fl;
    }
/**
     * Common list for all views *
     * 
     * @param mList
     * @param params
     * @param margin
     * @return
     */   
    private ListView commomList(ListView mList, LayoutParams params, int margin) {
        mList = new ListView(getActivity());
        params.setMargins(margin, margin, margin, margin);
        mList.setLayoutParams(params);
        mList.setLayoutParams(params);
        customAdapter = new CustomAdapter(getActivity(), values);
        mList.setAdapter(customAdapter);
        return mList;
    }
}

其中CustomAdapter是您extends base adapter

的班级
public class CustomAdapter extends BaseAdapter {
    ArrayList<String> mlistName;
    private LayoutInflater layoutinflater;
    private Context mContext;

    public CustomAdapter(Context context, ArrayList<String> elements) {
        mlistdata = elements;
        layoutinflater = LayoutInflater.from(context);
        mContext = context;
    }

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

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int arg0, View convertview, ViewGroup arg2) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        if (convertview == null) {
            holder = new ViewHolder();
            convertview = layoutinflater.inflate(R.layout.list_items, null);

            holder.name = (TextView) convertview
                    .findViewById(R.id.list_item_name);

            convertview.setTag(holder);
        } else {
            holder = (ViewHolder) convertview.getTag();
        }

        holder.name.setText((mlistdata.get(arg0));
        return convertview;
    }

    static class ViewHolder {
        TextView  name;
    }

}

并在您的MyPageAdapter班级中使用

更改您的getItem代码
@Override
        public Fragment getItem(int position) {

            return SuperAwesomeCardFragment.newInstance(position);
        }