Listview在Viewpager的片段类中

时间:2014-08-13 18:16:18

标签: android listview android-viewpager

我有一个ViewPager设置,包含3个片段类和一个pageadapter类。

MainActivity类

package com.example.swipeview;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {
ViewPager viewpager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewpager = (ViewPager) findViewById(R.id.viewpagers);
    com.example.swipeview.PagerAdapter padapter = new          com.example.swipeview.PagerAdapter(getSupportFragmentManager());

    viewpager.setAdapter(padapter);

}

}

PageAdapter类

 package com.example.swipeview;

 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentPagerAdapter;

 public class PagerAdapter extends FragmentPagerAdapter {

 public PagerAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
 }

   @Override
   public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    switch(arg0){
    case 0:
    return new FragmentOne();

    case 1:
    return new FragmentTwo();

    case 2:
    return new FragmentThree();

    default:
        break;
    }
    return null;
    }

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


    }
    }

和我的第一个FragmentClass作为例子

 package com.example.swipeview;

import com.example.swipeview.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment {


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_one_layout, container, false);

}



}//end of class

我已尝试过我常用的方法在Activity类中创建一个listview,但这并不适用于片段类。我想知道是否有人可以告诉我如何将listview编码成片段类。

1 个答案:

答案 0 :(得分:0)

使用自定义适配器(使用List作为源)的示例:

public class ItemsFragment extends Fragment {

    /* Must be populated later. */
    private final List<Item> items = new ArrayList<>();
    private ItemAdapter adapter;
    private ListView listView = null;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_items_list, container, false);
        adapter = new ItemAdapter(this.getActivity(), items);
        listView = (ListView) layout.findViewById(R.id.listview_items);
        return layout;
    }




    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Item currentItem = adapter.getItem(position);
                // (...)
            }
        });
    }

    /* (...) */

}