我有一个片段,它使用AsyncTask下载JSON并显示几个NEWS段。在这里,我可以上下滑动并查看所有新闻片段。但我希望水平滑动并通过滑动逐个查看新闻片段。据我所知(我是初学者)我必须使用ViewPager适配器来实现这一点。但我不知道如何。请有人帮助我。
我的片段类
public class NewsDetailFragment extends Fragment {
private View view1;
private ArrayList<BaseElement> newsdetail;
private LazyAdapter adapter;
private Activity activity;
private CommonVariable commonVariable;
private ProgressDialog dialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.newsdetail_fragment, container,
false);
activity = this.getActivity();
commonVariable = (CommonVariable) activity.getApplication();
view1 = (View) view.findViewById(R.id.list);
dialog = new ProgressDialog(NewsDetailFragment.this.getActivity());
dialog.setMessage("Loading News");
dialog.setCancelable(true);
new BackGround().execute();
return view;
}
public class BackGround extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
newsdetail = JSONServices.getNewsDescription();
return null;
}
@SuppressWarnings("unchecked")
@Override
/* check again */
protected void onPostExecute(Void result) {
commonVariable.setTheater(newsdetail);
adapter = new LazyAdapter(newsdetail, activity,Element.NEWS_DETAIL.getType());
((AdapterView<ListAdapter>) view1).setAdapter(adapter);
dialog.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog.show();
super.onPreExecute();
}
}
}
我的ViewPager适配器
public class ViewPageAdapter extends PagerAdapter {
private LinkedHashMap<String, BaseElement> item;
private int page;
private Activity activity;
private LayoutInflater inflater;
private ViewPageAdapter(LinkedHashMap<String, BaseElement> item, int page,
Activity activity) {
super();
this.item = item;
this.page = page;
this.activity = activity;
}
@Override
public int getCount() {
return item.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == (LinearLayout) object;
}
@Override
public Object instantiateItem(View container, int position) {
View view = null;
return view;
}
}
PageAdapter
public class PageAdapter extends FragmentStatePagerAdapter {
public static final String ARG_SECTION_NUMBER = "section_number";
private ArrayList<BaseElement> item;
private Activity activity;
public PageAdapter(FragmentManager fragmentManager,
ArrayList<BaseElement> item, Activity activity) {
super(fragmentManager);
this.activity = activity;
this.item = item;
}
@Override
public Fragment getItem(int position) {
// ------set fragment class for viewPager----------//
Fragment fragment = new PageSectionFragment();
Bundle bundle = new Bundle();
// ---- setPage position as agument for PageSectionFragment
// class---//
bundle.putInt(ARG_SECTION_NUMBER, position);
fragment.setArguments(bundle);
return fragment;
}
@Override
public int getCount() {
return item.size();
}
@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position) {
FilmCategory film = (FilmCategory) item.get(position);
Sihala sinhla = new Sihala(activity);
return sinhla.getSinhalaString(film.getCategory());
}
}
答案 0 :(得分:1)
将此添加到您的布局中..
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
然后将此类添加到Fragment类中。
private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
/*@Override
public android.support.v4.app.Fragment getItem(int position) {
return AlertSlideFragment.create(position);
}*/
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
tabFragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
tabFragment.setArguments(args);
/*fragment = getRegisteredFragment(0);*/
return tabFragment;
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container,
position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
最后这里是DummySectionFragment
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View rootView = null;
/*View rootView = inflater.inflate(R.layout.fragment_tabbed_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));*/
int screenNum = getArguments().getInt(ARG_SECTION_NUMBER);
//listAdapterAlert= new ListAdapterAlert(getActivity(), R.layout.alert_list);
if(screenNum == 0) // 0 for recent
{
rootView = inflater.inflate(R.layout.alert_list,
container, false);
TextView tv = (TextView) rootView.findViewById(R.id.tv);
}
else if(screenNum == 1) // 1 for by due date
{
rootView = inflater.inflate(R.layout.alert_list,
container, false);
TextView tv = (TextView) rootView.findViewById(R.id.tv);
}
else if(screenNum == 2) // 2 for by campaign
{
rootView = inflater.inflate(R.layout.campaign_alert,
container, false);
}
return rootView;
}
}
希望它会对你有所帮助。