Android viewPager - 需要从 JSON 加载图像
Android viewPager - need to load images from JSON
我正在尝试创建一个 ImageView,最好是一个包含 3 个图像的 Viewpager 滑块,它应该从左向右滑动。
我是我的 singleitemview.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>
然后singleitemview.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);
}
}
我的CustomPageAdapter.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.
你能帮我更正我的代码以使其正常工作吗?
Pass the list of images you get from JSON in constructor of Adapter
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);
}
}
我正在尝试创建一个 ImageView,最好是一个包含 3 个图像的 Viewpager 滑块,它应该从左向右滑动。
我是我的 singleitemview.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>
然后singleitemview.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);
}
}
我的CustomPageAdapter.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.
你能帮我更正我的代码以使其正常工作吗?
Pass the list of images you get from JSON in constructor of Adapter
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);
}
}