两个列表视图一次与半滑viewpager android

时间:2013-01-16 01:10:41

标签: android listview slider android-viewpager

是否可以将viewpager的一半滑动到屏幕上?

我的最终目标是一次显示两个列表视图,在第一页幻灯片后,左侧列表将是上一个列表。 所以如下所示..

list1,list2

list2中,项目list3

项目list3,list4

任何解决方案?

感谢enter image description here

1 个答案:

答案 0 :(得分:1)

好的,我准备好了。我完成了你想要做的事情(我想)。我的应用程序有3个ListView,每个列表包含从在线源获取的不同内容,并使用自定义适配器和ViewPager填充ListViews。然后,自定义适配器将分配给fragment上的PagerAdapter。我从Google资源中复制了很多代码,并试图概述我的所作所为。

首先,我在我的MainActivity布局中添加了ViewPager

<强> activity_main.xml中:

<android.support.v4.view.ViewPager 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!-- add a PagerTitleStrip -->
    <android.support.v4.view.PagerTitleStrip 
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"/>
</android.support.v4.view.ViewPager>

然后,我创建了一个单独的ListView布局,我可以用于自定义适配器:

<强> listview.xml

<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#E6E6E6"
    android:background="#E6E6E6"
    tools:context=".MainActivity" />

在我完成这些工作后,我挖掘了我的活动。其余的发生在 MainActivity.java

首先,列出一些变量:

public class MainActivity extends FragmentActivity implements OnNavigationListener {

    // your pager adapter
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    // your custom adapters (look this up on your own if you do not understand)
    ArrayList<ListEntry> listOneArrayList = null;
    ArrayList<ListEntry> listTwoArrayList = null;   
    CustomAdapterListOne customAdapterListOne = null;
    CustomAdapterListTwo customAdapterListTwo = null;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // more on that in the next block...
    }
}

现在,让我们进入onCreate()并开始创作!

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // set up your pager adapter
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // if you want to set a default view:
    mViewPager.setCurrentItem(0);   

    // now, run some AsyncTasks to load up our lists
    // I use AsyncTasks because I fetch my data from a server
    new generateListOne().execute();
    new generateListTwo().execute();

}


/*
 * Get the entries and create a list adapter
 */
private class generateListOne extends AsyncTask<String, Void, Object> {     
    @Override
    protected Object doInBackground(String... args) {
        listOneArrayList = new ArrayList<ListEntry>();
        // this is where I would do all of my networking stuff
        // and populate my arraylist
        return null;
    }

    @Override
    protected void onPostExecute(Object result) {
        // you have to create a new xml layout for 'listview_row' to use here v
        customAdapterListOne = new CustomAdapterListOne(self, R.layout.listview_row, listOneArrayList);
        /** Very important! This is where you specify where the list goes: **/
        // * Note: Fragment pages start at 0!
        ListSectionFragment fragment = (ListSectionFragment) getSupportFragmentManager().findFragmentByTag(
            "android:switcher:"+R.id.viewpager+":0"); // <- this is where you specify where the list goes       
        if (fragment != null) {                 // <- Could be null if not instantiated yet
            if(fragment.getView() != null) {
                customAdapterListOne.notifyDataSetChanged();
                fragment.updateListOneDisplay(customAdapterListOne);
            }
        }           
    }       
}

我不打算写出generateListTwo(),但希望你能理解generateListOne()的概念。 密切关注 onPostExecute()中发生的事情。现在,我们必须写出FragmentPagerAdapterListSection Fragment。此外,我们必须包括我们的自定义列表适配器。所有这些内容如下:

/*
 * Your Custom Adapter Class
 */    
private class CustomAdapterListOne extends ArrayAdapter<ListEntry> {
    /*
     * Read up on the rest of this for custom adapter if you 
     * are unfamilar. There are plenty of resources.. 
     * 
     * I am not going to type it all out.
     */
}

/*
 * SectionsPagerAdapter class for FragmentPagerAdapter title
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new ListSectionFragment();
        Bundle args = new Bundle();
        args.putInt(ListSectionFragment.ARG_SECTION_NUMBER, i + 1);
        fragment.setArguments(args);
        return fragment;            
    }

    @Override
    public int getCount() {
        // make sure this is correct
        int yourNumberOfLists = 5;
        return yourNumberOfLists;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return "First List";
            case 1: return "Second List";
            //case 2: etc..

        }
        return null;
    }

    public boolean onInterceptTouchEvent(MotionEvent event) {
        return false;
    }
}

/*
 * ListSectionFragment class for ListFragment(s)
 */
public static class ListSectionFragment extends ListFragment {

    public static final String ARG_SECTION_NUMBER = "section_number";
    public static int CURRENT_SECTION = 0;

    static ListSectionFragment newInstance(int num) {
        ListSectionFragment fragment = new ListSectionFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;        
    }

    public void updateListOneDisplay(ArrayAdapter<ListEntry> listOneAdapter) {
        setListAdapter(listOneAdapter);
    }

    public void updateListTwoDisplay(ArrayAdapter<ListEntry> listTwoAdapter) {
        setListAdapter(listTwoAdapter);
    }

    // etc..

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle args = getArguments();
        CURRENT_SECTION = args.getInt(ARG_SECTION_NUMBER);
        // note, we are using your listview here v
        View view = inflater.inflate(R.layout.listview, container, false);
        return view;
    }

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

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // and just for kicks:
        Log.i(TAG, "Item clicked: " + position);
    }

}

不要忘记关闭}课程的上一个MainActivity.java。希望这有助于某人,我知道我花了很长时间才弄明白。此代码提供的效果类似于Android Place应用程序的效果。

编辑:我忘了在列表加载时提及。当列表获得焦点时,它还会加载上一个和下一个列表。这样就可以过渡到它并让它已经准备好了。例如:

您转到列表2,列表1和列表3已加载。然后转到列表3(由于它已经加载,它会平滑过渡),并加载列表4和列表2。这可以确保在转换到新列表时,它已经加载或正在生成。