Viewpager,光标和片段(Viewpager, Cursor and Fragment)

编程入门 行业动态 更新时间:2024-10-23 16:22:17
Viewpager,光标和片段(Viewpager, Cursor and Fragment)

我是viewpager的新手,想知道是否有人能指出我使用带有片段和数据库的viewpager的项目的教程或源代码。 我已经看过PagerAdapters的例子,但我只是没有得到它们如何一起工作(Cursor,Fragment和PagerAdapter)

提前致谢。

I am new to the viewpager and was wondering if anyone could point me to a tutorial or source code of a project that uses a viewpager with fragments and a database. I've seen examples of PagerAdapters but I'm just not getting how they all work together (Cursor, Fragment and PagerAdapter)

Thanks in advance.

最满意答案

我在与您的问题相关的其他帖子中发布了一些答案。

以下是您可能会发现有用的一些链接。

第一个链接:在每个页面幻灯片中获取自定义视图。 (已接受的答案)

Android:如何在ViewPager中创建不同的视图?

第二:如何在ViewPager中正确使用片段。 (已接受的答案)

如何在ViewPager中正确使用片段?

如果这两个链接没有帮助,试试这个:

public class AllActivities extends FragmentActivity implements ActionBar.TabListener { public ViewPager viewPager; private AllPagesAdapter mAdapter; private ActionBar actionBar; private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initializing all stuff viewPager = (ViewPager)findViewById(R.id.pager); actionBar = getActionBar(); mAdapter = new AllPagesAdapter(getSupportFragmentManager()); viewPager.setAdapter(mAdapter); actionBar.setHomeButtonEnabled(true); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //Add the tabs here for(String tab_name:tabs){ actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this)); } viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ @Override public void onPageSelected(int position){ //on Page change, that particular page should be selected actionBar.setSelectedNavigationItem(position); } @Override public void onPageScrolled(int arg0,float arg1,int arg2){ } @Override public void onPageScrollStateChanged(int position){ } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) { viewPager.setCurrentItem(tab.getPosition()); } } //This is one of the fragment and assume that Expandablelistview is your list view.. In both the cases, i am setting the adapter in the onActivityCreated() method. public class Android extends android.support.v4.app.Fragment { ExpandableListAdapter listAdapter; // private ExpandableListView expListView; List<String> listDataHeader; HashMap<String, List<String>> listDataChild; ListView lv1; private static final String QUESTION = "question"; private static final String ANSWERS = "answer"; // ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View mikeview = inflater.inflate(R.layout.androidlayout, container, false); return mikeview; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ExpandableListView expListView = null; try{ expListView = (ExpandableListView) getActivity().findViewById(R.id.androidExpandableList); } catch (Exception e){ e.printStackTrace(); } new Thread(){ @Override public void run(){ } }.start(); try { setParent(); prepareChild(); } catch (Exception e) { e.printStackTrace(); } ExpandableListAdapter listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild); expListView.setAdapter(listAdapter); // Listview on child click listener expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { /*Toast.makeText( getActivity(), listDataHeader.get(groupPosition) + " : " + listDataChild.get( listDataHeader.get(groupPosition)).get( childPosition), Toast.LENGTH_SHORT) .show();*/ return true; } }); // Listview Group expanded listener expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { /* Toast.makeText(getActivity(), listDataHeader.get(groupPosition) + " Expanded", Toast.LENGTH_SHORT).show();*/ } }); // Listview Group collasped listener expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { /*Toast.makeText(getActivity(), listDataHeader.get(groupPosition) + " Collapsed", Toast.LENGTH_SHORT).show();*/ } }); } public void setParent(){ listDataHeader = new ArrayList<String>(); try{ JSONObject json = new JSONObject(loadJSONFromAsset()); JSONArray array = json.getJSONArray("androidquestion"); for(int my =0;my<array.length();my++){ JSONObject c = array.getJSONObject(my); String topics = c.getString(QUESTION); listDataHeader.add(topics); } } catch (JSONException e) { e.printStackTrace(); } } public void prepareChild() throws JSONException { listDataChild = new HashMap<String, List<String>>(); try{ JSONObject json = new JSONObject(loadJSONFromAsset()); JSONArray array = json.getJSONArray("androidquestion"); for(int mz = 0;mz<array.length();mz++){ ArrayList<String> child = new ArrayList<String>(); JSONObject d = array.getJSONObject(mz); String ans = d.getString(ANSWERS); child = new ArrayList<String>(); child.add(ans); int position = mz ; listDataChild.put(listDataHeader.get( position),child); } } catch(JSONException e) { e.printStackTrace(); } } public String loadJSONFromAsset() { String json = null; try { InputStream is = getActivity().getAssets().open("android.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } } //This is the adapter. public class AllPagesAdapter extends FragmentStatePagerAdapter { public AllPagesAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int index) { switch (index) { case 0: return new Android(); case 1: return new CoreJava(); case 2: return new J2EE(); case 3: return new Database(); case 4: return new WebServices(); } return null; } @Override public int getCount() { return 5; } } //This is the adapter for my expandable list view.. in your case, your can just use the listadapter or any other adapter you want to use. This is just an optional part. public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List<String> _listDataHeader; // header titles // child data in format of header title, child title private HashMap<String, List<String>> _listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; } @Override public Object getChild(int groupPosition, int childPosititon) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .get(childPosititon); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.newlistitems,null); } TextView txtListChild = (TextView) convertView .findViewById(R.id.newlistviewitems); txtListChild.setText(childText); return convertView; } @Override public int getChildrenCount(int groupPosition) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .size(); } @Override public Object getGroup(int groupPosition) { return this._listDataHeader.get(groupPosition); } @Override public int getGroupCount() { return this._listDataHeader.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater myInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = myInflater.inflate(R.layout.newlistgroup, null); } TextView lblListHeader = (TextView) convertView .findViewById(R.id.newlistviewgroup); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }

Lemme知道你是否需要更多的例子.. :)

I have posted some answers in the other posts related to your question.

Here's some of the links that you might find helpful.

First Link: To get custom views in every page slide. (Accepted answer)

Android:How to create different view in ViewPager?

Second: How to properly use fragments with ViewPager. (Accepted answer)

How to properly use fragments with ViewPager?

If those two links are not that helpful, try this:

public class AllActivities extends FragmentActivity implements ActionBar.TabListener { public ViewPager viewPager; private AllPagesAdapter mAdapter; private ActionBar actionBar; private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initializing all stuff viewPager = (ViewPager)findViewById(R.id.pager); actionBar = getActionBar(); mAdapter = new AllPagesAdapter(getSupportFragmentManager()); viewPager.setAdapter(mAdapter); actionBar.setHomeButtonEnabled(true); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //Add the tabs here for(String tab_name:tabs){ actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this)); } viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ @Override public void onPageSelected(int position){ //on Page change, that particular page should be selected actionBar.setSelectedNavigationItem(position); } @Override public void onPageScrolled(int arg0,float arg1,int arg2){ } @Override public void onPageScrollStateChanged(int position){ } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) { viewPager.setCurrentItem(tab.getPosition()); } } //This is one of the fragment and assume that Expandablelistview is your list view.. In both the cases, i am setting the adapter in the onActivityCreated() method. public class Android extends android.support.v4.app.Fragment { ExpandableListAdapter listAdapter; // private ExpandableListView expListView; List<String> listDataHeader; HashMap<String, List<String>> listDataChild; ListView lv1; private static final String QUESTION = "question"; private static final String ANSWERS = "answer"; // ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View mikeview = inflater.inflate(R.layout.androidlayout, container, false); return mikeview; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ExpandableListView expListView = null; try{ expListView = (ExpandableListView) getActivity().findViewById(R.id.androidExpandableList); } catch (Exception e){ e.printStackTrace(); } new Thread(){ @Override public void run(){ } }.start(); try { setParent(); prepareChild(); } catch (Exception e) { e.printStackTrace(); } ExpandableListAdapter listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild); expListView.setAdapter(listAdapter); // Listview on child click listener expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { /*Toast.makeText( getActivity(), listDataHeader.get(groupPosition) + " : " + listDataChild.get( listDataHeader.get(groupPosition)).get( childPosition), Toast.LENGTH_SHORT) .show();*/ return true; } }); // Listview Group expanded listener expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { /* Toast.makeText(getActivity(), listDataHeader.get(groupPosition) + " Expanded", Toast.LENGTH_SHORT).show();*/ } }); // Listview Group collasped listener expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { /*Toast.makeText(getActivity(), listDataHeader.get(groupPosition) + " Collapsed", Toast.LENGTH_SHORT).show();*/ } }); } public void setParent(){ listDataHeader = new ArrayList<String>(); try{ JSONObject json = new JSONObject(loadJSONFromAsset()); JSONArray array = json.getJSONArray("androidquestion"); for(int my =0;my<array.length();my++){ JSONObject c = array.getJSONObject(my); String topics = c.getString(QUESTION); listDataHeader.add(topics); } } catch (JSONException e) { e.printStackTrace(); } } public void prepareChild() throws JSONException { listDataChild = new HashMap<String, List<String>>(); try{ JSONObject json = new JSONObject(loadJSONFromAsset()); JSONArray array = json.getJSONArray("androidquestion"); for(int mz = 0;mz<array.length();mz++){ ArrayList<String> child = new ArrayList<String>(); JSONObject d = array.getJSONObject(mz); String ans = d.getString(ANSWERS); child = new ArrayList<String>(); child.add(ans); int position = mz ; listDataChild.put(listDataHeader.get( position),child); } } catch(JSONException e) { e.printStackTrace(); } } public String loadJSONFromAsset() { String json = null; try { InputStream is = getActivity().getAssets().open("android.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } } //This is the adapter. public class AllPagesAdapter extends FragmentStatePagerAdapter { public AllPagesAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int index) { switch (index) { case 0: return new Android(); case 1: return new CoreJava(); case 2: return new J2EE(); case 3: return new Database(); case 4: return new WebServices(); } return null; } @Override public int getCount() { return 5; } } //This is the adapter for my expandable list view.. in your case, your can just use the listadapter or any other adapter you want to use. This is just an optional part. public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List<String> _listDataHeader; // header titles // child data in format of header title, child title private HashMap<String, List<String>> _listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; } @Override public Object getChild(int groupPosition, int childPosititon) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .get(childPosititon); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.newlistitems,null); } TextView txtListChild = (TextView) convertView .findViewById(R.id.newlistviewitems); txtListChild.setText(childText); return convertView; } @Override public int getChildrenCount(int groupPosition) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .size(); } @Override public Object getGroup(int groupPosition) { return this._listDataHeader.get(groupPosition); } @Override public int getGroupCount() { return this._listDataHeader.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater myInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = myInflater.inflate(R.layout.newlistgroup, null); } TextView lblListHeader = (TextView) convertView .findViewById(R.id.newlistviewgroup); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }

Lemme know if you need some more examples on that..:)

更多推荐

本文发布于:2023-04-27 16:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:光标   片段   Viewpager   Cursor   Fragment

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!