是否可以滑動viewpager一半的屏幕?兩個列表視圖與一半滑動viewpager android
我的最終目標是一次顯示兩個列表視圖,第一頁幻燈片之後,左側列表將成爲之前的列表。 所以像如下..
列表1,列表2
列表2,項目list3
項目list3,list4
任何解決方案?
感謝
是否可以滑動viewpager一半的屏幕?兩個列表視圖與一半滑動viewpager android
我的最終目標是一次顯示兩個列表視圖,第一頁幻燈片之後,左側列表將成爲之前的列表。 所以像如下..
列表1,列表2
列表2,項目list3
項目list3,list4
任何解決方案?
感謝
好了,我要帶刺在此。我完成了你想要做的(我想)。我的應用程序有3個ListView,每個列表包含從在線源獲取的不同內容,並使用自定義適配器和ListViews
填充ViewPager
。然後,自定義適配器被分配到PagerAdapter
上的fragment
。我從谷歌資源中複製了很多我的代碼,並試圖概述我所做的。
首先,我添加了一個ViewPager
到我的佈局我的MainActivity
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()
中發生了什麼。現在,我們必須寫出FragmentPagerAdapter
和我們的ListSection 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已加載。這確保了當您轉換到新列表時,它已經加載或正在生成過程中。
因此,您希望能夠在多個ListView之間滑動,在發生轉換時看到它們兩個? – burmat
@burmat是的正確..我有情景像...從list1中選擇的項目...所以list2更新,現在從list2中選擇的項目,..so list3更新...等等..我想顯示兩個一次列出,先前選擇的項目列表和第二個列表 – Bhaumik
如何在下一個ViewPage中「重複」ListView?在你的例子中你會有3個viewPages。 ViewPage1:list1,list2。 ViewPage2:list2,list3 ...這種方式界面交互將正是你想要的。我只是不知道列表視圖的管理是否會變得煩人。 –