我在我的应用程序中使用view pager来显示json数据中的图像。 json数据来自rest api。我已在控制器类中反序列化数据。我正在使用Picasso imgae loader从json加载图像。现在问题是在运行应用程序后,viewpager上没有显示任何图像。我没有找到它显示空白的原因。
My Controller class
public class NewsController {
private static final String TAG = NewsController.class.getSimpleName();
private UserCallbackListener mListener;
private NewsRestApiManager mApiManager;
private AppImage appImages;
public NewsController(UserCallbackListener listener) {
mListener = listener;
mApiManager = new NewsRestApiManager();
}
public void startFetching(){
mApiManager.getNewsApi().getNews(new Callback<String>() {
@Override
public void success(String s, Response response) {
Log.d(TAG, "JSON :: " + s);
try {
JSONArray array = new JSONArray(s);
for(int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
NewsModel news = new NewsModel();
news.setTitle( jsonObject.optString( "title") );
news.setBody( jsonObject.optString( "body" ) );
news.setUpdatedAt( jsonObject.getString( "updated_at" ) );
ArrayList<AppImage> list = new ArrayList();
JSONArray imageArray =jsonObject.getJSONArray("appImages");
if (imageArray.length() > 1) {
for(int j=0; j<imageArray.length();j++){
appImages = new AppImage();
try {
appImages.setSrc( new JSONArray( s ).getJSONObject( i ).getJSONArray( "appImages" ).getJSONObject( j ).optString( "src" ) );
}catch (JSONException e){
e.printStackTrace();
}
list.add(appImages);
}
}
news.setAppImages( list );
TeaserImageSmall coverImage=new TeaserImageSmall();
coverImage.setSrc( new JSONArray( s ).getJSONObject( i ).getJSONObject( "teaserImageSmall" ).optString( "src" ));
news.setTeaserImageSmall(coverImage);
mListener.onFetchProgressNews(news);
}
} catch (JSONException e) {
mListener.onFetchFailed();
}
mListener.onFetchComplete();
}
@Override
public void failure(RetrofitError error) {
Log.d(TAG, "Error :: " + error.getMessage());
mListener.onFetchComplete();
}
});
}
public interface UserCallbackListener{
void onFetchStart();
void onFetchProgressNews(NewsModel news);
void onFetchProgressNews(List<NewsModel> userList);
void onFetchComplete();
void onFetchFailed();
}
}
ViewPager适配器
private List<NewsModel> imageList;
private LayoutInflater layoutInflater;
private Context context;
public ViewPagerAdapter(Context context, List<NewsModel> imageList ) {
this.context = context;
this.imageList=imageList;
}
@Override
public int getCount() {
return imageList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
layoutInflater=(LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View itemView = layoutInflater.inflate(R.layout.sliding_img_layout, view, false);
final NewsModel imageFromNews=imageList.get( position );
ImageView myImage = itemView.findViewById(R.id.sliding_image);
Picasso.with(itemView.getContext()).load(imageFromNews.getTeaserImageSmall().getSrc()).into( myImage );
view.addView(itemView, 0);
myImage.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(context,DetailNews.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("src",imageFromNews.getTeaserImageSmall().getSrc());
i.putExtra("title",imageFromNews.getTitle());
i.putExtra("body",imageFromNews.getBody());
context.startActivity(i);
}
} );
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
我的主要片段是
public class OptionMenuFragment extends Fragment implements View.OnClickListener{
private static ViewPager viewPager;
private static int currentPage = 0;
private List<NewsModel> imageList=new ArrayList<>( );
private ViewPagerAdapter adapter;
private NewsController mController;
public OptionMenuFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.option_main_fragment, container, false);
viewPager = view.findViewById(R.id.pager);
adapter=new ViewPagerAdapter( this.getActivity(),imageList );
viewPager.setAdapter(adapter);
CircleIndicator indicator = (CircleIndicator) view.findViewById(R.id.indicator);
indicator.setViewPager(viewPager);
mController = new NewsController(this);
mController.startFetching();
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == imageList.size()) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
System.out.println(viewPager.getCurrentItem());
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
},2000,5000);
}
@Override
public void onFetchStart() {
}
@Override
public void onFetchProgressNews(NewsModel news) {
System.out.println(news.getTeaserImageSmall());
imageList.add(news);
adapter.notifyDataSetChanged();
}
@Override
public void onFetchProgressNews(List<NewsModel> userList) {
}
@Override
public void onFetchComplete() {
}
@Override
public void onFetchFailed() {
}
我对浏览寻呼机的看法是
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.1">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>