Android viewPager语言 - 需要从 JSON 加载图像



>我正在尝试创建一个 ImageView,最好说一个带有 3 张图像的 Viewpager 滑块,它应该从左向右滑动。

我是我的单项视图.xml我有这个(我放在那里 ViewPager(:

<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="100dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/sometext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sometext"
/>
</RelativeLayout>

然后单项视图.java

public class SingleitemView extends AppCompatActivity {
CustomPagerAdapter mCustomPagerAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
//... here I get some data from URL as json
//and then calling this:
createMarkersFromJson(json.toString());
}
private void createMarkersFromJson(String json) throws JSONException {
JSONObject jsonObj = new JSONObject(json);
JSONArray actors = jsonObj.getJSONArray("result");
for (int i = 0; i < actors.length(); i++) {
final JSONObject c = actors.getJSONObject(i);
String typ = c.getString("typ");
final String place2 = c.getString("place");
final String img1 = c.getString("img1");
final String img2 = c.getString("img2");
final String img3 = c.getString("img3");
//Picasso.with(this).load(img1).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imgimg1);
mCustomPagerAdapter = new CustomPagerAdapter(this);
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
}
}

我的自定义页面适配器.java

class CustomPagerAdapter extends PagerAdapter {
int[] mResources = {
R.drawable.bottom2,
R.drawable.bottom,
R.drawable.hrady
};
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.img1);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}

}

最后pager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@mipmap/ic_launcher"
android:foregroundGravity="top|center"
android:scaleType="fitCenter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/img1" />
</LinearLayout>

问题是,图像是在 mResources 内部的 CustomPagerAdapter 中定义的,但我需要使用来自 json 的图像,当我从 json 获取数据时.java我只在 singleitemview 中知道。

你能帮我纠正我的代码让它工作吗?

在适配器的构造函数中传递从 JSON 获得的图像列表

class CustomPagerAdapter extends PagerAdapter {   
List<String> mResources = new ArrayList<>();
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context , List<String> mResources) {
mContext = context;
this.mResources=mResources;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mResources.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.img1);
Picasso.with(this).load(mResources.get(position)).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}

}

最新更新