ठीक है, मैं इस पर एक वार लेने के लिए जा रहा हूँ। मैंने पूरा किया (मुझे लगता है) आप करने की कोशिश कर रहे हैं। मेरे एप्लिकेशन में 3 ListViews हैं, और प्रत्येक सूची में ऑनलाइन स्रोत से प्राप्त विभिन्न सामग्री होती है और कस्टम एडाप्टर और ListViews
का उपयोग करके ViewPager
पॉप्युलेट करती है। कस्टम एडाप्टर को पर fragment
पर असाइन किया गया है। मैंने 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()
में क्या हो रहा है, इस पर बहुत करीब ध्यान दें। अब, हमें 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
वर्ग बाहर बंद हुआ। उम्मीद है कि यह किसी की मदद करता है, मुझे पता है कि यह मुझे हमेशा के लिए बाहर ले गया। यह कोड जो प्रभाव प्रदान करता है वह एंड्रॉइड प्लेस एप्लिकेशन के समान है।
संपादित करें: सूची लोड होने पर मैं उल्लेख करना भूल गया था। जब एक सूची फोकस प्राप्त करती है, तो यह पिछली और अगली सूची भी लोड करती है। इससे इसे संक्रमण करना संभव हो जाता है और यह पहले से ही वहां जाने के लिए तैयार हो सकता है।उदाहरण के लिए:
आप सूची 2 पर जाते हैं और सूची 1 और सूची 3 लोड होते हैं। फिर आप सूची 3 पर जाते हैं (और यह आसानी से संक्रमण करता है क्योंकि यह पहले से लोड हो चुका है), और सूची 4 और सूची 2 लोड हो गए हैं। यह सुनिश्चित करता है कि जब आप किसी नई सूची में संक्रमण करते हैं, तो यह पहले ही लोड हो चुका है या उत्पन्न होने की प्रक्रिया में है।
तो आप दोनों सूची दृश्यों के बीच स्लाइड करने में सक्षम होना चाहते हैं, क्योंकि उनमें से दोनों संक्रमण हो रहे हैं? – burmat
@ बबमत हां सही .. मेरे पास परिदृश्य है ... सूची 1 से चयनित आइटम ... इसलिए list2 अपडेट किया गया, अब आइटम 2 से चुना गया आइटम, ..so list3 अपडेट किया गया ... और इसी तरह .. और मैं दो प्रदर्शित करना चाहता हूं एक समय में सूचीबद्ध, पिछली चयनित आइटम सूची, और दूसरी सूची – Bhaumik
अगले व्यूपेज में ListView को "दोहराएं" के बारे में क्या? आपके उदाहरण में आपके पास 3 व्यूपेज होंगे। व्यू पेज 1: सूची 1, सूची 2। ViewPage2: list2, list3 ... इस तरह इंटरफ़ेस इंटरैक्शन बिल्कुल वही होगा जो आप चाहते हैं। मुझे नहीं पता कि सूचीदृश्य का प्रबंधन परेशान हो जाएगा या नहीं। –