java - 以编程方式添加的视图寻呼机不显示
问题描述
我以编程方式添加了用于图像滑动的 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。
解决方案
就我而言,我通过在 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);
推荐阅读
- java - CIT(自定义项目纹理)Optifine 1.16.4 上的问题
- aes - PHP/Java 中的 AES GCM
- java - 使用 Java 流仅基于属性对连续元素进行分组
- excel - 为什么在注册 Adobe Acrobat 时 explorer.exe 会使用 Internet Explorer 打开 pdf 文件?
- autodesk-forge - 在 Forge 查看器中查找 2 个对象之间的障碍物
- php - Poedit 和 WordpPress:HTML 标签翻译中的有害结果
- flutter - WebView颤动:初始化url时无法将'cookie'设置为标题
- python - 在一天中的某些时间禁用 discord.py 命令
- javascript - 如何使用异步函数等待来自 onClick 的用户输入?
- python - 无法单击并按住硒中的元素