我们在滚动条中创建了新闻卡的垂直列表视图, 工作正常,但是当我们打开列表中的任何卡时-例如 滚动条中的第五张卡片-始终打开第一张卡片。
因此,无论您尝试尝试打开多少张卡,它始终只会打开第一张卡。我无法接触错误,因为我是android开发的新手。
能请你帮忙吗?
以下是我们到位的适配器代码。如果您能帮助解决此错误,将不胜感激
package messenger.fb.video.chat.lite.message.Adapter;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import messenger.fb.video.chat.lite.message.Activity.News;
import messenger.fb.video.chat.lite.message.Activity.NewsBrowser;
import messenger.fb.video.chat.lite.message.Model.ArticleResponse;
import messenger.fb.video.chat.lite.message.Model.NewsUrl;
import messenger.fb.video.chat.lite.message.R;
public class VerticlePagerAdapter extends PagerAdapter {
private ImageView newsImage;
private ArticleResponse articleResponse;
private NewsUrl newsBrowser;
private TextView title, description, source,times,readFullStory;
private List<ArticleResponse> articleResponseList;
private Context context;
private LayoutInflater layoutInflater;
private int position;
private int count = 0;
public VerticlePagerAdapter(Context context, List<ArticleResponse> articleResponseList, int position) {
this.context = context;
this.articleResponseList = articleResponseList;
this.position = position;
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return articleResponseList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == ((LinearLayout)o);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View itemView = layoutInflater.inflate(R.layout.top_stories_single_news_layout, container, false);
newsImage = (ImageView)itemView.findViewById(R.id.newsImage);
title = (TextView)itemView.findViewById(R.id.newsTitle);
description = (TextView)itemView.findViewById(R.id.description);
source = (TextView)itemView.findViewById(R.id.source);
times = (TextView)itemView.findViewById(R.id.newsTime);
readFullStory = (TextView)itemView.findViewById(R.id.read_full_story);
articleResponse = articleResponseList.get(position);
newsBrowser = new NewsUrl(articleResponse.getArticleSourceResponses().getName(), articleResponse.getUrl());
Glide.with(context)
.load(articleResponse.getUrlToImage())
.into(newsImage);
title.setText(articleResponse.getTitle());
source.setText(articleResponse.getArticleSourceResponses().getName() + " | ");
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat output = new SimpleDateFormat("HH:mm:ss");
Date d = null;
Date time = new Date();
try {
d = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(articleResponse.getPublishedAt());
time = input.parse(time.toString());
} catch (ParseException e) {
e.printStackTrace();
}
long diff = time.getTime() - d.getTime();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(diff);
String publishedTime = "";
if (cal.get(Calendar.HOUR) > 0)
publishedTime = publishedTime + cal.get(Calendar.HOUR) + " hours, ";
if (cal.get(Calendar.MINUTE) > 0)
publishedTime = publishedTime + cal.get(Calendar.MINUTE) + " minutes ago";
else
publishedTime = publishedTime + " ago";
times.setText(publishedTime);
description.setText(articleResponse.getDescription());
readFullStory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(context, NewsBrowser.class);
browserIntent.putExtra("url", newsBrowser);
context.startActivity(browserIntent);
}
});
// container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}