16

我尝试将 ViewPager 用作 ListView 中的一行,但得到一个奇怪的行为 - 只有第一行有效,但是当我滚动列表时它消失了。当我滚动一个空行时,突然正在查看上面的行。似乎 Android 创建了一个寻呼机并将其用于所有行。

这是我在启动应用程序时看到的:

截屏

这是我的行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:paddingBottom="2dp" >

    <TextView
        android:id="@+id/textViewFriendName"
        style="@style/TextStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/myFriendsBg"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:text="TextView" />

    <View style="@style/DividerHorizontalStyle_gray" />

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


    <View style="@style/DividerHorizontalStyle" />

</LinearLayout>

这是我的列表适配器:

public class MyFriendsAdapter extends BaseAdapter implements OnClickListener {

    private ArrayList<UiD_UserFriend> mItems;
    private Context mContext;
    private FragmentManager mFragmentManager;

    public MyFriendsAdapter(Context context, ArrayList<UiD_UserFriend> items, FragmentManager fragmentManager) {
        mContext = context;
        mItems = items;
        mFragmentManager = fragmentManager;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View currentView, ViewGroup viewGroup) {
        Holder holder = null;
        if (currentView == null) {
            currentView = View.inflate(mContext, R.layout.view_list_item_myfriends, null);
            holder = new Holder();
            holder.textViewUserName = (TextView) currentView.findViewById(R.id.textViewFriendName);
            holder.mMyFriendspager = (ViewPager) currentView.findViewById(R.id.pagerMyFriendHabits);
            currentView.setTag(holder);
        } else {
            holder = (Holder) currentView.getTag();
        }

        holder.textViewUserName.setText(mItems.get(position).getmName());
        MyFriendPagerAdapter tempMyFriendPagerAdapter = new MyFriendPagerAdapter(mFragmentManager, mItems.get(position).getFriendHabits());
        holder.mMyFriendspager.setAdapter(tempMyFriendPagerAdapter);

        return currentView;
    }

    class Holder {
        TextView textViewUserName;
        ViewPager mMyFriendspager;
    }
}

这是我的寻呼机适配器:

public class MyFriendPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<UiD_Habit> mHabits;

    public MyFriendPagerAdapter(FragmentManager fm, ArrayList<UiD_Habit> habits) {
        super(fm);
        mHabits = habits;
    }

    @Override
    public Fragment getItem(int index) {
        return new FragmentMyFriendPage(mHabits.get(index));
    }

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

4 回答 4

27

每当您需要动态添加寻呼机时,您需要为每个寻呼机设置一个 ID,使用ViewPager.setId().

于 2013-02-25T16:09:30.797 回答
3

我创建了示例应用程序

带有片段的 ViewPager 示例:https ://dl.dropboxusercontent.com/u/20178650/builds/listviewwithviewpager.zip

但是有一个很大的缺点。所有单元格都存储在内存中,应用程序可能会因内存不足而崩溃,无论您是否有很多单元格

编辑:

经验上已经找到了。如果在 ViewPager 中使用简单的视图而不是片段。我可以正确编写适配器,而不会被内存击中

带视图的示例 ViewPager:https ://dl.dropboxusercontent.com/u/20178650/builds/listviewwithviewpager_v2.zip

我希望有人能帮帮忙

于 2014-08-09T14:27:18.503 回答
0
 /// <summary>
    /// To Improve ListView Memory Optimization 
    /// </summary>
    public class ViewPageHolder : Java.Lang.Object
    {
        public TextView _firstTextView{ get; set; }
        public ViewPager _ViewPager { get; set; }
        public CirclePageIndicator _CirclePageIndicator { get; set; }
    }
Queue<int> _pagerId= new Queue<int>(Enumerable.Range(10, 100));//To set a Unique Id to View Pager to avoid ListView Recycle memory issue




public override View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
        {
            View view = convertView;
            ViewPageHolder ViewPageHolder = null;

        if (view != null)
        {
            ViewPageHolder = view.Tag as ViewPageHolder ;
        }
        if (ViewPageHolder == null)
        {
            ViewPageHolder = new ViewPageHolder ();
            view = Context.LayoutInflater.Inflate(Resource.Layout.SystemListLayout, null);
            ViewPageHolder .SystemNameTextView = view.FindViewById<TextView>(Resource.Id.SystemNameTextView);
            //View Pager
            ViewPageHolder._ViewPager = view.FindViewById<ViewPager>(Resource.Id.viewPager);
            //Circle Pager Indicator
            ViewPageHolder.CirclePageIndicator = view.FindViewById<CirclePageIndicator>(Resource.Id.viewPageIndicator);
            ViewPageHolder._ViewPager.Id = _pagerId.Dequeue();
            ViewPageHolder._ViewPager.Adapter = new ZoneRenameViewPagerFragmentAdapter(fm, Context, List.ElementAt(position).Zones.ToList(), ((BaseActivity)Context).IsTablet);
            ViewPageHolder.CirclePageIndicator.SetViewPager(ZoneRenameHolder.ZoneRenameViewPager);
            view.Tag = ZoneRenameHolder;
        }
        return view;
    }
  1. 使用支架
  2. 使用标签
  3. 生成唯一 ID
于 2015-07-11T17:09:41.143 回答
-3

我遇到了完全相同的问题。解决方案:

  1. 使用支架
  2. 使用标签
  3. 生成唯一 ID

我解决了这个问题。如果有人需要代码请ping我。

于 2015-05-05T08:07:56.377 回答