gpt4 book ai didi

java - 以编程方式添加的 View 寻呼机不显示

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:57 24 4
gpt4 key购买 nike

我以编程方式添加了用于图像滑动的 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>

此处膨胀 View 正常,但 viewpager 未显示。

最佳答案

就我而言,我通过在 viewpager 中创建 View 之一并在设置 viewpager 后删除 View 来解决。代码如下

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);

关于java - 以编程方式添加的 View 寻呼机不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50893738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com