0

我尝试用两个片段创建 viewpager。在一个片段中,我尝试使用动态内容创建列表视图。

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <com.example.pkg.views.CustomViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.view.PagerTabStrip
                android:id="@+id/pager_title"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:textAppearance="@style/PagerTabStripText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top" />
    </com.example.pkg.views.CustomViewPager>
</RelativeLayout>

在主要活动类中,我使用自己的 CustomFragmentAdapter 和覆盖的 getItem(int index) 方法。

@Override
public Fragment getItem(int i) {
switch (i) {
    case 0:
        return new List();

类列表扩展片段:

public class List extends Fragment {
    private PointManager pointManager;
    private ListView list ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.list, container, false);
        list = (ListView) rootView.findViewById(R.id.point_list);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        pointManager = PointManager.getInstance(getActivity());

        ListViewAdapter adapter = new ListViewAdapter(getActivity(), R.id.point_list, pointManager.getPoints());
        list.setAdapter(adapter);
        list.invalidate();
    }

    @Override
    public void onResume() {
        super.onResume();
        ListViewAdapter adapter = new ListViewAdapter(getActivity(), R.id.point_list, pointManager.getPoints());
        list.setAdapter(adapter);
        list.invalidate();
    }
}

列表.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <ListView
            android:id="@+id/point_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/white"
            android:divider="#ffdead"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector" />
</RelativeLayout>

列表视图适配器:

public class ListViewAdapter extends ArrayAdapter<Point> {
    private final Activity activity;
    private List<Point> points;

    public ListViewAdapter(Activity context, int textViewResourceId, List<Point> objects) {
        super(context, textViewResourceId, objects);
        this.activity = context;
        this.points = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parentView) {
        ViewHolder viewHolder;

        View rowView = convertView;

        if(rowView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.custom_list, parentView, false);

            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) rowView.findViewById(R.id.notification_title);
            viewHolder.description = (TextView) rowView.findViewById(R.id.notification_desc);
            viewHolder.icon = (ImageView) rowView.findViewById(R.id.notification_icon);
            rowView.setTag(viewHolder);
        }
        else
            viewHolder = (ViewHolder) rowView.getTag();

        viewHolder.title.setText(points.get(position).getTitle());
        viewHolder.description.setText(points.get(position).getDescription());

        return rowView;
    }

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

    private class ViewHolder {
        TextView title;
        TextView description;
        ImageView icon;
    }
}

一切都好。但!滑动在 ViewPager 中不起作用。我认为这是因为我在 onCreateView 中返回 rootView。如果我返回 super.onCreateView(...),滑动就可以了!但未显示列表。

怎么了?

谢谢。

4

1 回答 1

0

这是因为我在 CustomViewPager 中处理 onInterceptTouchEvent 中第二个片段的操作。

if(getCurrentItem() == CURRENT_SECOND) {} 

我添加了

if(getCurrentItem() == CURRENT_FIRST) {}

和所有的作品。感谢@Luksprog。

于 2013-07-01T10:06:17.127 回答