android - ListView 行项目中的 ViewPager

标签 android android-listview android-viewpager

我已经阅读了很多关于这个问题的相关问题。然而,他们都没有得到答复。我正在尝试将 ViewPager 添加到 ListView 中的每一行,其中寻呼机中有两个布局。我相信这是可行的,因为 SwipeListView .

现在我已经实现了。但是,显示空白页,中间有一行。没有行出现。

Enter image description here

这是我的代码:

MainActivity + ArrayAdapter

public class MainActivity extends Activity {

    ListView list;
    String[] heros;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        heros = getResources().getStringArray(R.array.heros);

        list = (ListView) findViewById(R.id.listView1);
        list.setAdapter(new CustomAdapter(this, heros));
    }

    public class CustomAdapter extends ArrayAdapter<String>
    {
        String[] heros;
        Context context;
        public CustomAdapter(Context context, String[] heros) {
            super(context, R.layout.pager_item_list, R.id.listView1, heros);
            // TODO Auto-generated constructor stub
            this.heros = heros;
            this.context = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.pager_item_list, parent, false);

            Log.d("how many times", "I really don't know how it should be");

            ViewPager pager = (ViewPager) row.findViewById(R.id.pager);
            pager.setId(position);
            pager.setAdapter(new PagerCustomAdapter());

            pager.setCurrentItem(0);

            return row;
        }
    }
}

这是我的pageAdapter

public class PagerCustomAdapter extends PagerAdapter{
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public Object instantiateItem(View container, int position) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int id = 0;

        Log.d("I am in pagerAdater", "I am here, dammit!");

        switch(position)
        {
            case 0:
                id = R.layout.fragment_top_item;
                break;
            case 1:
                id = R.layout.fragment_bottom_item;
                break;
        }
        View view = inflater.inflate(id, null);
        ((ViewPager)container).addView(view, 0);
        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

}

我已经尝试了 pagerAdapterFragmentPagerAdapter 并且都得到了相同的结果

我在 getView()instantiateItem() 中添加了两条日志消息,它们都按预期在 logcat 中读取。 LogCat 中既没有警告也没有错误.

我错过了什么?

更新

activity_main 的 XML

<RelativeLayout
    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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</ListView>

</RelativeLayout>

pager_item_list 的 XML

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

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

最佳答案

我不确定你想要达到什么目的。你想在事件发生时更改 View 吗?喜欢当你点击它?或者您想要与 SwipeListView 相同的结果。

如果你想改变 View ,你可以尝试使用 ViewFlipper anc 在事件发生时改变布局。但是我不确定这是不是你想要的结果

关于android - ListView 行项目中的 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20774098/

相关文章:

android - 从 ListView 中获取点击的项目

android - 在 listView 之前调用 onTouchEvent()

android - 用于连续滚动的圆形 ViewPager

Android.app Fragments 与 android.support.v4.app 使用 ViewPager?

java - VariableDeclaratorId 和错误位置

android - 如何解决此错误 'com.google.ads.mediation.admob.AdMobAdapter cannot be cast to com.google.android.gms.ads.mediation.m '?

android - 每行中的 TextView 和 Button 以及 onListItemClick()

android - 如何将 ImageView 与 ListView 中的一些文本结合起来

android - 验证由 ViewPager 托管的多个 fragment 上的字段

Android环境变量和父子 Activity