2014-10-03 3 views
-2

ViewPager에서 ListView를 만들고 있지만 ViewPager는 ListView없이 표시됩니다. ViewPager에서 ListView를 표시하려면 어떻게해야합니까? 이 코드는 어디에 수정해야합니까?viewpager에서 Lististview를 어떻게 표시합니까?

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> 
01,235 16,

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" 
    /> 

답변

0

는 목록보기의 볼을 처리하는 ListFragment를 만듭니다. 이제 FragmentAdapter 클래스를 설정하여 ViewPager에 목록 뷰를로드하십시오.

관련 문제