0

嘿,所以我正在尝试创建一个从主屏幕滑动到的设置菜单。我使用 Pager Adapter 创建了一个三页视图,用户可以从中心屏幕滑动到该视图。

IE:

        POST SCREEN   ------SWIPE------- HOME ------SWIPE---------- SETTINGS SCREEN

我现在想用一个漂亮的列表视图填充设置屏幕,以便能够创建一个菜单供用户选择。我遇到的问题是将它与我​​实现的 pageAdapter 集成。我认为的问题是我要让页面滑动工作我只有一个具有多个视图的活动(在这种情况下是 3 个)。但是对于 listView 适配器,我需要一个单独的活动。

在这种情况下,我可能完全误会了,但一些澄清会很棒。

这是我到目前为止所拥有的:MainActivity.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;


/** Called when the activity is first created. */
@SuppressLint("NewApi")
public class MainActivity extends Activity {

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // PAGE ADAPTER CONTENT
    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.threepagemainview);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);

}


/**
 * 
 * @author Andy
 * 
 * This method is a data adapter to determine and load the appropriate content 
 * for each page the user swipes to
 * 
 * We have the main view set to case 1 -> main menu
 * and left view to post, right view to settings
 * 
 */
private class MyPagerAdapter extends PagerAdapter {

        /**
         * This will get the count for the amount of pages to return
         */
        public int getCount() {
                return 3;
        }

        /**
         * This method will inflate the appropriate layout resource file
         * depending on the users swipe position.
         */
        public Object instantiateItem(View collection, int position) {

                LayoutInflater inflater = (LayoutInflater) collection.getContext()
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                int resId = 0;
                switch (position) {
                case 0:
                        resId = R.layout.view_post;
                        break;
                case 1:
                        resId = R.layout.view_middle;
                        break;
                case 2:
                        resId = R.layout.view_settings;
                        break;
                }
                View view = inflater.inflate(resId, null);

                ((ViewPager) collection).addView(view, 0);

                return view;
        }

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

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == ((View) arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

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

        @Override
        public void startUpdate(View arg0) {
        }
}
}

对于 XML 文件,我有一个带有视图文本的普通文件。目前这完全是赤裸裸的骨头。

任何帮助将不胜感激。

谢谢

一个

4

1 回答 1

0

onCreate()

ViewPager myPager = (ViewPager) findViewById(R.id.threepagemainview);
LayoutInflater lf = getLayoutInflater().from(this);
postView = lf.inflate(R.layout.view_post, null);
homeView = lf.inflate(R.layout.view_middle, null);
settingsView = lf.inflate(R.layout.view_settings, null);
mListView = (ListView) settingsView.findViewById(R.id.list);
viewList = new ArrayList<View>();
viewList.add(postView);
viewList.add(homeView);
viewList.add(settingsView);
MyPagerAdapter adapter = new MyPagerAdapter(viewList);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);

你可以像这样设置你的 PagerAdapter

private class MyPagerAdapter extends PagerAdapter {
    private List<View> mListViews;

    public MyPagerAdapter(List<View> mListViews) {
        this.mListViews = mListViews;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(mListViews.get(position), 0);
        return mListViews.get(position);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mListViews.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0==arg1;
    }
}
于 2013-05-27T03:17:15.340 回答