在Android中混合listview和pageview以制作可滑动的设置菜单

时间:2013-05-27 02:18:19

标签: android listview adapter swipe

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

即:

        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文件,我简单地有一个带有视图文本的普通文件。目前这完全是骨干。

非常感谢任何帮助。

由于

A

1 个答案:

答案 0 :(得分: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;
    }
}