0

嗨,我对编程很陌生。以我有限的知识,我真的很难在我的 Android 项目中获得水平视图滑动。我下载了一个 ViewPager 教程并将代码实现到我的项目中,效果很好,但是它只显示了一些文本数字(1、2、3、4 等......)。我希望能够水平查看滑动图像(可能带有一些文本)而不仅仅是数字。我真的很沮丧,我尝试混合和匹配其他 ViewPager 教程中的代码,但它总是崩溃并抛出我无法理解的错误。我可以对此代码进行一些简单的更改,以便能够水平滑动图像和一些文本吗?谢谢。

主要活动

package com.project.viewswipingtest;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class Page5 extends FragmentActivity {
MyPageAdapter pageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pg5);

    List<Fragment> fragments = getFragments();

    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
    pager.setAdapter(pageAdapter);

}

private List<Fragment> getFragments(){
    List<Fragment> fList = new ArrayList<Fragment>();

    fList.add(MyFragment.newInstance("Fragment 1"));
    fList.add(MyFragment.newInstance("Fragment 2"));
    fList.add(MyFragment.newInstance("Fragment 3"));
    fList.add(MyFragment.newInstance("Fragment 4"));

    return fList;
}

private class MyPageAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }
    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

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

片段类

package com.project.viewswipingtest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

public static final MyFragment newInstance(String message)
{
    MyFragment f = new MyFragment();
    Bundle bdl = new Bundle(1);
    bdl.putString(EXTRA_MESSAGE, message);
    f.setArguments(bdl);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    String message = getArguments().getString(EXTRA_MESSAGE);
    View v = inflater.inflate(R.layout.myfragment_layout, container, false);
    TextView messageTextView = (TextView)v.findViewById(R.id.textView);
    messageTextView.setText(message);

    return v;
}

}

ViewPager XML

<?xml version="1.0" encoding="utf-8"?>
<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.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</RelativeLayout>

片段 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"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
4

1 回答 1

0

Fragment XML添加一个<ImageView>. 然后在 中Fragment Class.onCreateView(),你可以设置 load anyBitmap进去。

于 2013-05-13T16:37:56.540 回答