如何在viewpager中显示Lististview?

时间:2014-10-03 05:06:28

标签: android listview android-viewpager

我在ViewPager中创建ListView,但是ViewPager没有ListView。 我该怎么做才能在ViewPager中显示ListView? 我在哪里修复此代码?

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    ViewPager viewPager=(ViewPager)findViewById(R.id.viewpager);
    PagerAdapter pagerAdapter=new MyPagerAdapter(this);
    viewPager.setAdapter(pagerAdapter);
}

MyPagerAdapter.java

public Object instantiateItem(ViewGroup container,int position){
    int[] pages = {R.layout.page2, R.layout.page3};
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(pages[position], null);
    LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
            R.layout.list_bottom, null, false);
    container.addView(layout);

    ArrayList<CustomDataBottom> data=new ArrayList<CustomDataBottom>();
    CustomDataBottom itemBottom=new CustomDataBottom();
    Bitmap image1= BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    Bitmap image2=BitmapFactory.decodeResource(context.getResources(),R.drawable.dog);
    itemBottom.setImageData(image1);
    data.add(itemBottom);
    itemBottom.setImageData(image2);
    data.add(itemBottom);
    CustomBottomAdapter bottomAdapter=new CustomBottomAdapter(context,0,data);
    ListView bottomListView=(ListView)mLinearLayout.findViewById(R.id.listbottom);
    bottomListView.setAdapter(bottomAdapter);
    return layout;
}

CustomBottomAdapter.java

public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    convertView = inflater.inflate(R.layout.list_bottom, parent, false);
    CustomDataBottom item1=(CustomDataBottom)getItem(0);
    ImageView imageView1=(ImageView)convertView.findViewById(R.id.android);
    imageView1.setImageBitmap(item1.getImage());
    CustomDataBottom item2=(CustomDataBottom)getItem(1);
    ImageView imageView2=(ImageView)convertView.findViewById(R.id.dog);
    imageView2.setImageBitmap(item2.getImage());
   return convertView;
}

activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
android:orientation="vertical">

<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/listtop"/>
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

</LinearLayout>

list_bottom.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/listbottom"
    />

1 个答案:

答案 0 :(得分:0)

创建一个ListFragment来处理列表视图的查看。现在通过设置FragmentAdapter类将listview加载到ViewPager中。