I am working on a short news app. It's parsed JSON from a URL. I want to show one news per view pager.on swipe on next ViewPager
, I want to show next news like the list. How can I can do that with ViewPager
?
Asked
Active
Viewed 1,484 times
1
-
You can load fragments in ViewPager and each fragment can fetch it's required information. Here are some links to get started. http://www.tothenew.com/blog/updating-viewpager-with-new-data-dynamically/ https://stackoverflow.com/questions/35824429/populate-each-fragment-in-viewpager-with-different-json-object-without-loading-a – nikhil_salunke Sep 08 '18 at 06:24
2 Answers
2
Set JSON data to PagerAdapter as we set to Base Adapter or other adapters & create custom layout for adapter.
view_pager_layout.xml
<RelativeLayout
android:id="@+id/loutPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/loutCirIndicator"
android:background="@android:color/transparent"
android:padding="5dp"/>
<LinearLayout
android:id="@+id/loutCirIndicator"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:orientation="horizontal">
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
app:ci_drawable="@drawable/bg_selected_indicator"
app:ci_drawable_unselected="@drawable/bg_unselected_indicator" />
</LinearLayout>
</RelativeLayout>
row_pager_item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:clickable="false"
android:scaleType="centerInside" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="abc" />
</LinearLayout>
ViewPager Adapter
public class SamplePagerAdapter extends PagerAdapter {
Activity activity;
List<Response_Model> Arr_ResultList;
ImageView imageView;
TextView textView;
public SamplePagerAdapter(final Activity activity,final List<Response_Model> Arr_ResultList) {
this.activity = activity;
this.Arr_ResultList = Arr_ResultList;
}
@Override
public int getCount() {
return Arr_ResultList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final int pos = position;
View view = LayoutInflater.from(container.getContext()).inflate(R.layout.row_pager_item, container, false);
imageView = (ImageView) view.findViewById(R.id.imageView);
textView = (TextView) view.findViewById(R.id.textView);
Glide.with(activity).load(Arr_ResultList.get(pos).getImage())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
textView.setText(Arr_ResultList.get(pos).getName());
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
Set Pager Adapter to ViewPager
SamplePagerAdapter pagerAdapter = new SamplePagerAdapter(MainActivity.this, Arr_Result_List);
viewPager.setAdapter(pagerAdapter);
indicator.setViewPager(viewPager);

Ritu Tandel
- 89
- 1
0
You can use PagerAdapter
for ViewPager
and create your custom xml layout and
set your view on cell by position. Pass your list into pager adapter in adapter constructor.
You can refer this link it will be help you.

Mohamed Mohaideen AH
- 2,527
- 1
- 16
- 24

Vijendra patidar
- 1,444
- 1
- 14
- 24