以编程方式添加的View Pager不会显示

时间:2018-06-17 04:37:45

标签: java android xml android-viewpager

我添加了viewpager用于以编程方式滑动图像,但我的viewpager不显示。我添加了viewpager来膨胀xml文件。

这是我的代码。

添加了viewpager

View view = LayoutInflater.from(getContext()).inflate(R.layout.custom_view_pager,null);
                ViewPager pager = view.findViewById(R.id.viewPager);
                pager.setId(View.generateViewId());

                FullBannerViewPager adapter = new FullBannerViewPager(getContext(),fullBannerModels.get(fullBannerCount));
                pager.setAdapter(adapter);
                llMainLayout.addView(view);

custom_view_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="@dimen/_100sdp"
    android:background="@color/dokaan_red">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ASDFASDFAS FSD FASDF"
        android:textColor="@color/white"
        />

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"
        />

</LinearLayout>

这是我的适配器FullBannerViewPager.java

package com.hazesoft.dokan.dashboard.view.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hazesoft.dokan.R;
import com.hazesoft.dokan.dashboard.view.Model.DashboardFullBannerModel;
import com.hazesoft.dokan.singleproductdetail.GalleryActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class FullBannerViewPager extends PagerAdapter {

    Context context;
    List<DashboardFullBannerModel> fullBannerModels;

    public FullBannerViewPager(Context context, List<DashboardFullBannerModel> fullBannerModels) {
        this.context = context;
        this.fullBannerModels = fullBannerModels;
        Log.d("fullbannermodel", "FullBannerViewPager: "+new Gson().toJson(fullBannerModels));
    }

    @Override
    public int getCount() {
        return fullBannerModels.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return  view == ((LinearLayout) object);
    }

    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        view.removeView((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = mLayoutInflater.inflate(R.layout.full_banner_view_pager, collection, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.full_banner);
        Picasso.with(context).load(fullBannerModels.get(position).getImage()).into(imageView);

        collection.addView(itemView);

        return itemView;

    }
}

full_banner_view_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/ll_main_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="@dimen/_195sdp">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/full_banner"
        android:layout_marginTop="@dimen/_7sdp"
        android:layout_marginBottom="@dimen/_7sdp"
        android:src="@drawable/aa"
        />

</LinearLayout>

这里的膨胀视图还可以,但是没有显示viewpager。

1 个答案:

答案 0 :(得分:0)

在我的情况下,我通过在viewpager中创建一个视图并在设置viewpager之后解决,并删除视图。 这里的代码

<强> custom_view_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="@dimen/_195sdp">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="@dimen/_195sdp"
        android:id="@+id/viewPager"
        />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/aa"
        />

</FrameLayout>

Java代码

 View view = LayoutInflater.from(getContext()).inflate(R.layout.custom_view_pager,null);
                    ViewPager pager = view.findViewById(R.id.viewPager);
                    ImageView imageView = view.findViewById(R.id.imageView);
                    FullBannerViewPager adapter = new FullBannerViewPager(getContext(),fullBannerModels.get(fullBannerCount));
                    pager.setAdapter(adapter);
                    llMainLayout.addView(view);

                    imageView.setVisibility(View.GONE);