Dobra, idę wziąć ukłucie na to. Osiągnąłem co (myślę) próbujesz zrobić. Moja aplikacja ma 3 listy listView, a każda lista zawiera inną treść pobraną ze źródła online i zapełnia ją przy użyciu niestandardowych adapterów i i ListViews
. Niestandardowy adapter jest następnie przypisywany do fragment
na PagerAdapter
. Skopiowałem dużo kodu z zasobu Google i spróbuję opisać, co zrobiłem.
pierwsze, dodałem ViewPager
do mojego układ dla mojego główną działalność
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>
Potem stworzyliśmy osobną ListView
układ mógłby używać do moich własnych adapterów:
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" />
Po tym zestawie wkopałem się w moją działalność. Reszta odbywa się MainActivity.java:
Najpierw ułożyć kilka zmiennych:
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...
}
}
Teraz przejdźmy do onCreate()
i rozpocząć tworzenie!
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);
}
}
}
}
ja nie zamierzam pisać generateListTwo(), ale mam nadzieję, że rozumiesz pojęcie z generateListOne(). Zwróć szczególną uwagę na to, co dzieje się wonPostExecute()
. Teraz musimy napisać kod FragmentPagerAdapter
i nasz ListSection Fragment
. Musimy również uwzględnić naszą niestandardową listę Adapter. Wszystkie te rzeczy następujące:
/*
* 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);
}
}
nie zapomnij ostatni }
aby zamknąć klasę MainActivity.java
. Mam nadzieję, że to pomoże komuś, wiem, że zajęło mi to na zawsze zrozumienie. Efekt, jaki zapewnia ten kod, jest podobny do tego w aplikacji Android Place.
Edycja: Zapomniałem wspomnieć o załadowaniu listy. Gdy lista zyskuje ostrość, ładuje również poprzednią i następną listę. Umożliwia to przejście do niego i już jest gotowe do pracy.Na przykład:
Przejdziesz do listy 2, a lista 1 i lista 3 zostaną załadowane. Następnie przejdź do listy 3 (i płynnie przechodzi, ponieważ jest już załadowany), a lista 4 i lista 2 są ładowane. Gwarantuje to, że po przejściu do nowej listy jest już załadowany lub jest w trakcie generowania.
A więc chcesz mieć możliwość przesuwania się między wieloma widokami listy, widząc je jako przejściowe? – burmat
@burmat tak poprawne ... Mam scenariusz taki jak ... element wybrany z listy1 ... więc lista2 zaktualizowana, teraz pozycja wybrana z listy2, ..na liście3 zaktualizowana ... i tak dalej .. i chcę wyświetlić dwa lista na raz, poprzednia wybrana lista artykułów i druga lista – Bhaumik
Co powiesz na "powtórzenie" ListView na następnej stronie ViewPage? W twoim przykładzie masz 3 strony widoku. ViewPage1: list1, list2. ViewPage2: list2, list3 ... W ten sposób interakcja z interfejsem będzie dokładnie taka, jak chcesz. Po prostu nie wiem, czy zarządzanie odsłonami stanie się denerwujące. –