android - 如何在 Android 中为每个 ViewPager 显示一个 JSON 对象?

标签 android android-studio android-viewpager

我正在开发一个短新闻应用程序。它从 URL 解析 JSON。我想在每个 View 寻呼机上显示一个新闻。在下一个 ViewPager 上滑动时,我想像列表一样显示下一个新闻。我怎样才能用 ViewPager 做到这一点?

最佳答案

将 JSON 数据设置为 PagerAdapter,就像我们设置为 Base 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);

关于android - 如何在 Android 中为每个 ViewPager 显示一个 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52232616/

相关文章:

java - 如何在android中实现java中的Kotlin类?

android - PreferenceFragment 中的 RunTimeException

android - 华硕ME172v usb在MAC中调试

Android textview仅在视觉上旋转

android - 当我移动到相邻页面之外时,带有 TabLayout 的 ViewPager 不显示来自 recyclerview 的列表

android - 如何将Activity转为Fragment,使用ViewPager实现滑动?

javascript - 使用 XDK apprate 不起作用

android - Android Studio/ADB 未找到 Pixel C

android - 不必要; SDK_INT 总是 >= 21,而我的 minSdkVersion 是 16

android - 当 fragment 恢复/重新创建时,ViewPager getScrollX 返回 0