将寻呼机视为列表视图行项目

Gau*_*ora 9 android listview android-viewpager

我有一个包含20行的列表视图,我想在列表视图中设置一个视图寻呼机作为每一行.由于列表视图的行中的项目可能是一个或多个,我想使用视图寻呼机显示列表视图行项目.

为此,我使用以下代码:

自定义布局,将显示在列表视图行中(作为寻呼机项)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inner_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6.0dip"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:text="Example Value"
        android:textAppearance="?android:textAppearanceMedium" />
Run Code Online (Sandbox Code Playgroud)

主列表视图具有View Pager的自定义行项目

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


         <android.support.v4.view.ViewPager
        android:id="@+id/mypager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

MyPagerAdapter类

public class MyPagerAdapter extends PagerAdapter {

    private Context context;

    public MyPagerAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Object instantiateItem(View container, int position) {
        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = inflater.inflate(R.layout.inner_layout_file, null);
        ((ViewPager) container).addView(view, position);
        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == ((View) obj);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView((View) object);
    }
}
Run Code Online (Sandbox Code Playgroud)

列表视图适配器获取视图方法

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);

    MyPagerAdapter adapter = new MyPagerAdapter(context);
    ViewPager myPager = (ViewPager) convertView.findViewById(R.id.mypager);
    myPager.setAdapter(adapter);

    myPager.setCurrentItem(0);

    return convertView;
}
Run Code Online (Sandbox Code Playgroud)

我没有收到任何错误,运行后呈现的视图始终为空.

小智 5

我遇到了同样的问题,并通过在getView()方法中动态地向viewpager提供id来解决它.

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);

    MyPagerAdapter adapter = new MyPagerAdapter(context);
    ViewPager myPager = (ViewPager) convertView.findViewById(R.id.mypager);

    **myPager.setId(position + getCount());**

    myPager.setAdapter(adapter);

    myPager.setCurrentItem(0);

    return convertView;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮到你.


egf*_*nor 0

将 instantiateItem() 中的行更改为:

((ViewPager) container).addView(view, position);
Run Code Online (Sandbox Code Playgroud)

到:

((ViewPager) container).addView(view, 0);
Run Code Online (Sandbox Code Playgroud)