在我的Android应用程序中,我使用Recyclerview来滚动项目。滚动页面时滞后。我已经为这个问题找到了更多的解决方案,但它仍然没有用。
这是我的代码:
listAdapter = new FeedRecycleAdapter_Sample(this, feedItems);
listView.setLayoutManager(new LinearLayoutManager(this));
listView.setAdapter(listAdapter);
我在上面的代码中调用适配器类。
提前致谢。
更新
以下是我用来从MainActivity类调用的适配器类。
FeedRecycleAdapter_Sample.java
public class FeedRecycleAdapter_Sample extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public FeedRecycleAdapter_Sample(Activity activity, List<FeedItem> feedItems) {
this.activity = activity;
this.feedItems = feedItems;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
final ViewHolderImage mViewHolder = new ViewHolderImage(mView);
return mViewHolder;
}
@Override
public void onBindViewHolder(ViewHolder v, final int position) {
final ViewHolderImage viewHolderImage = (ViewHolderImage) v;
final FeedItem item = feedItems.get(position);
getImageIDValue = item.getImage_id();
getVideoIDValue = item.getVideo_id();
getAudioIDValue = item.getAudio_id();
getDocIDValue = item.getDoc_id();
if (!(item.getImge()).equals("") || !(item.getVideo()).equals("") || !(item.getAudio()).equals("") || !(item.getFile()).equals("")) {
if (!getImageIDValue.equals("0") || !getVideoIDValue.equals("0") || !getAudioIDValue.equals("0") || !getDocIDValue.equals("0")) {
if (!getImageIDValue.equals("0")) {
getValue_data = "0";
}
if (!getVideoIDValue.equals("0")) {
getValue_data = "1";
}
if (!getAudioIDValue.equals("0")) {
getValue_data = "2";
}
if (!getDocIDValue.equals("0")) {
getValue_data = "3";
}
switch (getValue_data) {
case "0":
viewHolderImage.feedImageView.setImageUrl(item.getImge(), imageLoader);
viewHolderImage.feedImageView.setVisibility(View.VISIBLE);
viewHolderImage.feedImageView
.setResponseObserver(new FeedImageView.ResponseObserver() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
}
});
break;
case "1":
viewHolderImage.feedImageView.setImageUrl(ConfigUrl.Vdo_PNG, imageLoader);
viewHolderImage.feedImageView.setVisibility(View.VISIBLE);
viewHolderImage.feedImageView
.setResponseObserver(new FeedImageView.ResponseObserver() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
}
});
break;
case "2":
viewHolderImage.feedImageView.setImageUrl(ConfigUrl.Audio_PNG, imageLoader);
viewHolderImage.feedImageView.setVisibility(View.VISIBLE);
viewHolderImage.feedImageView
.setResponseObserver(new FeedImageView.ResponseObserver() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
}
});
break;
case "3":
viewHolderImage.feedImageView.setImageUrl(ConfigUrl.Doc_PNG, imageLoader);
viewHolderImage.feedImageView.setVisibility(View.VISIBLE);
viewHolderImage.feedImageView
.setResponseObserver(new FeedImageView.ResponseObserver() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
}
});
break;
}
}
} else {
viewHolderImage.feedImageView.setVisibility(View.GONE);
}
viewHolderImage.comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(activity, ActComment.class);
i.putExtra("postId", item.getId());
i.putExtra("uid", item.getUser_id());
GET_POST_M = item.getId();
activity.startActivity(i);
}
});
viewHolderImage.name.setText(item.getName());
viewHolderImage.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String cd=item.getName();
String c =cd.substring(cd.lastIndexOf(" "));
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.fromParts("tel", c.trim(), null));
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
activity.startActivity(callIntent);
}
});
if(!item.getMemID().equals("")){
viewHolderImage.d_number.setText(item.getMemID());
}
else{
viewHolderImage.d_number.setVisibility(View.GONE);
}
PrettyTime prettyTime = new PrettyTime();
Long d=Long.parseLong(item.getTimeStamp())* 1000;
viewHolderImage.timestamp.setText(prettyTime.format(new Date(d)));
// Chcek for empty status message
if (!TextUtils.isEmpty(item.getStatus())) {
viewHolderImage.statusMsg.setText(item.getStatus());
viewHolderImage.statusMsg.setVisibility(View.VISIBLE);
String checkText= viewHolderImage.statusMsg.getText().toString().trim();
boolean isContain = containsURL(checkText);
boolean isEmail=containsEmail(checkText);
boolean isPhone=containsPhone(checkText);
if(isContain==true){
viewHolderImage.statusMsg.setAutoLinkMask(Linkify.ALL);
viewHolderImage.statusMsg.setText(item.getStatus());
viewHolderImage.statusMsg.setMovementMethod(LinkMovementMethod.getInstance());
}
if(isEmail==true){
viewHolderImage.statusMsg.setAutoLinkMask(Linkify.ALL);
viewHolderImage.statusMsg.setText(item.getStatus());
viewHolderImage.statusMsg.setMovementMethod(LinkMovementMethod.getInstance());
}
if(isPhone==true){
viewHolderImage.statusMsg.setAutoLinkMask(Linkify.ALL);
viewHolderImage.statusMsg.setText(item.getStatus());
viewHolderImage.statusMsg.setMovementMethod(LinkMovementMethod.getInstance());
}
} else {
// status is empty, remove from view
viewHolderImage.statusMsg.setVisibility(View.GONE);
}
if(!(item.getAudioURL()).equals("")){
viewHolderImage.docNameUrl1.setText(item.getAudioURL());
viewHolderImage.docNameUrl1.setVisibility(View.VISIBLE);
}
else{
viewHolderImage.docNameUrl1.setVisibility(View.GONE);
}
if(!(item.getFileURL()).equals("")){
viewHolderImage.docNameUrl.setText(item.getFileURL());
viewHolderImage.docNameUrl.setVisibility(View.VISIBLE);
}
else{
viewHolderImage.docNameUrl.setVisibility(View.GONE);
}
// user profile pic
// viewHolderImage.profilePic.setImageUrl(item.getProfilePic(), imageLoader);
if(item.getProfilePic()!=null) {
if(!item.getProfilePic().equals("default.png")) {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
URL url = null;
Bitmap image = null;
try {
url = new URL(item.getProfilePic());
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
}
final Bitmap bm = Bitmap.createScaledBitmap(image, image.getWidth() / 2, image.getHeight() / 2, true);
viewHolderImage.profilePic.setImageBitmap(bm);
viewHolderImage.profilePic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new PhotoFullPopupWindow(activity,bm);
}
});
}
else{
viewHolderImage.profilePic.setImageResource(R.drawable.avatar);
}
}
else{
viewHolderImage.profilePic.setImageResource(R.drawable.avatar);
}
//click listener
viewHolderImage.docNameUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(activity, Single_file.class);
i.putExtra("single_file", item.getFile());
activity.startActivity(i);
activity.finish();
}
});
viewHolderImage.docNameUrl1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(activity, StreamingMp3Player.class);
i.putExtra("single_audio", item.getAudio());
activity.startActivity(i);
activity.finish();
}
});
//like post
viewHolderImage.like_liked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolderImage.like_liked1.getDrawable()== activity.getResources().getDrawable(R.drawable.liked)) {
viewHolderImage.like_liked1.setImageResource(R.drawable.likes);
}else{
viewHolderImage.like_liked1.setImageResource(R.drawable.liked);
like_post(MainActivity_Sample.GET_UID,item.getId());
}
}
});
//comment_count
if(!(item.getComment_count()).equals("0")){
if((item.getComment_count()).equals("1")){
viewHolderImage.ccount.setVisibility(View.VISIBLE);
viewHolderImage.ccount.setText(item.getComment_count()+" comment");
}
else {
viewHolderImage.ccount.setVisibility(View.VISIBLE);
viewHolderImage.ccount.setText(item.getComment_count() + " comments");
}
}
else {
viewHolderImage.ccount.setVisibility(View.INVISIBLE);
}
//like & comment count layout
if(viewHolderImage.getlike.getVisibility()==View.INVISIBLE && viewHolderImage.ccount.getVisibility()==View.INVISIBLE){
viewHolderImage.like_comment_count.setVisibility(View.GONE);
}
//comment click event
if(viewHolderImage.ccount.getVisibility()==View.VISIBLE){
viewHolderImage.ccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(activity, ActComment.class);
//Toast.makeText(activity, "success!"+item.getId(), Toast.LENGTH_LONG).show();
i.putExtra("postId", item.getId());
i.putExtra("uid", item.getUser_id());
GET_POST_M=item.getId();
activity.startActivity(i);
}
});
}
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return feedItems.size();
}
public class ViewHolderImage extends ViewHolder {
TextView name,timestamp,statusMsg,url, docNameUrl,docNameUrl1,ccount, getlike, d_number;
CircleImageView profilePic;
ImageView delete,like_liked1;
FeedImageView feedImageView,feedImageViewV,feedImageViewA,feedImageViewD;
LinearLayout comment,like_comment_count,like_liked;
public ViewHolderImage( View convertView) {
super(convertView);
name = (TextView) convertView.findViewById(R.id.name);
d_number = (TextView) convertView.findViewById(R.id.d_number);
delete=(ImageView)convertView.findViewById(R.id.delete);
getlike = (TextView) convertView.findViewById(R.id.getlike);
like_liked1=(ImageView) convertView.findViewById(R.id.like_liked1);
like_liked=(LinearLayout)convertView.findViewById(R.id.like_liked);
docNameUrl=(TextView)convertView.findViewById(R.id.txtDocUrl);
docNameUrl1=(TextView)convertView.findViewById(R.id.txtDocUrl1);
timestamp = (TextView) convertView
.findViewById(R.id.timestamp);
statusMsg = (TextView) convertView
.findViewById(R.id.txtStatusMsg);
url = (TextView) convertView.findViewById(R.id.txtUrl);
ccount = (TextView) convertView.findViewById(R.id.ccount);
profilePic = (CircleImageView) convertView
.findViewById(R.id.profilePic);
feedImageView = (FeedImageView) convertView
.findViewById(R.id.feedImage1);
textview=(TextView)convertView.findViewById(R.id.cmd);
like_comment_count=(LinearLayout)convertView.findViewById(R.id.like_comment_count);
comment=(LinearLayout)convertView.findViewById(R.id.comment);
}
}
}