我正在创建一个论坛应用程序。它具有类似于Facebook主页的主页。它包含带有视频和图像的帖子。当我滚动页面时,它挂了很多东西。我想知道如何使它像Facebook主页一样流畅。请帮忙。
适配器代码:
公共类PostListAdapter扩展了RecyclerView.Adapter {
public CardView cardView;
Context context;
List<StoriesDatum> storiesDatumList;
StoriesDatum storiesDatum;
FragmentHome fragmentHome;
boolean isPressed;
public static ImageView ivFav;
SimpleExoPlayer exoPlayer;
String videoURL = "http://blueappsoftware.in/layout_design_android_blog.mp4";
int qty;
View itemView;
public PostListAdapter(Context context, List<StoriesDatum> storiesDatumList, FragmentHome fragmentHome) {
this.context = context;
this.storiesDatumList = storiesDatumList;
this.fragmentHome = fragmentHome;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_post, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
MyViewHolder myViewHolder = new MyViewHolder(itemView);
myViewHolder.layout = (RelativeLayout) itemView.findViewById(R.id.layout);
myViewHolder.txtNamePosted = (TextView) itemView.findViewById(R.id.txtNamePosted);
myViewHolder.txtPostTitle = (TextView) itemView.findViewById(R.id.txtPostTitle);
myViewHolder.ivAdd = (ImageView) itemView.findViewById(R.id.ivAdd);
myViewHolder.ivLike = (ImageView) itemView.findViewById(R.id.ivLike);
myViewHolder.ivFav = (ImageView) itemView.findViewById(R.id.ivFav);
myViewHolder.relativeLike = (RelativeLayout) itemView.findViewById(R.id.relativeLike);
myViewHolder.tvPost = (TextView) itemView.findViewById(R.id.tvPost);
myViewHolder.txtLike = (TextView) itemView.findViewById(R.id.txtLike);
myViewHolder.txtCommunityName = (TextView) itemView.findViewById(R.id.txtCommunityName);
myViewHolder.ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
myViewHolder.txtComment = (TextView) itemView.findViewById(R.id.txtComment);
myViewHolder.txtTime = (TextView) itemView.findViewById(R.id.txtTime);
myViewHolder.txtCommentCount = (TextView) itemView.findViewById(R.id.txtCommentCount);
myViewHolder.txtLikeCount = (TextView) itemView.findViewById(R.id.txtLikeCount);
myViewHolder.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
myViewHolder.ivPost = (ImageView) itemView.findViewById(R.id.ivPost);
myViewHolder.exoPlayerView = (SimpleExoPlayerView) itemView.findViewById(R.id.exo_player_view);
storiesDatum = storiesDatumList.get(position);
holder.txtCommunityName.setText(storiesDatum.getComTitle());
holder.txtPostTitle.setText(storiesDatum.getTitle());
// TextView tvPost = new TextView(R.id.tvPost);
String story_body = String.valueOf(storiesDatum.getStoryBody());
String story_type = String.valueOf(storiesDatum.getStoryType());
String story_url = String.valueOf(storiesDatum.getStoryUrl());
if (story_type.equals("1")) {
holder.tvPost.setText(story_body);
holder.tvPost.setVisibility(View.VISIBLE);
holder.ivPost.setVisibility(View.GONE);
holder.exoPlayerView.setVisibility(View.GONE);
fragmentHome.makeTextViewResizable(holder.tvPost, 3, "....View More", true);
} else if (story_type.equals("2")) {
holder.tvPost.setVisibility(View.GONE);
holder.ivPost.setVisibility(View.VISIBLE);
holder.exoPlayerView.setVisibility(View.GONE);
if (story_url.startsWith("https://") || story_url.startsWith("http://")) {
Glide.with(context).load(storiesDatum.getStoryUpload())
.placeholder(R.mipmap.top_communities_circle)
.error(R.drawable.ic_launcher_background)
.into(holder.ivPost);
} else {
Glide.with(context).load(Constants.image_url + storiesDatum.getStoryUpload())
.placeholder(R.mipmap.top_communities_circle)
.error(R.drawable.ic_launcher_background)
.into(holder.ivPost);
}
Log.e("tag", "image_url" + Constants.image_url + storiesDatum.getStoryUpload());
} else {
holder.tvPost.setVisibility(View.GONE);
holder.ivPost.setVisibility(View.GONE);
holder.exoPlayerView.setVisibility(View.VISIBLE);
try {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
Uri videoURI;
if (story_url != null) {
videoURI = Uri.parse("http://66.42.57.85/tellit/public/uploads//157658624483.mp4");
} else {
videoURI = Uri.parse("http://66.42.57.85/tellit/public/uploads//157658624483.mp4");
}
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null);
holder.exoPlayerView.setPlayer(exoPlayer);
exoPlayer.prepare(mediaSource);
//exoPlayer.setPlayWhenReady(true);
} catch (Exception e) {
Log.e("MainAcvtivity", " exoplayer error " + e.toString());
}
}
String comm_photo = String.valueOf(storiesDatum.getComImage());
if (comm_photo.equals(null) || comm_photo.equals("null") || comm_photo.equals("")) {
holder.ivImage.setImageResource(R.mipmap.top_communities_circle);
} else {
Glide.with(context).load(comm_photo)
.placeholder(R.mipmap.top_communities_circle)
.error(R.drawable.ic_launcher_background)
.into(holder.ivImage);
}
holder.txtNamePosted.setText("Posted by " + storiesDatum.getUsername());
String created_date = storiesDatum.getCreatedAt();
//call date
String corecDate = fragmentHome.createDate(created_date);
holder.txtTime.setText(corecDate);
holder.txtCommentCount.setText(storiesDatum.getCommentsCount() + " Comments");
holder.txtLikeCount.setText(storiesDatum.getStoryemojiCount() + " Reactions");
holder.txtCommunityName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
storiesDatum = storiesDatumList.get(position);
fragmentHome.sendToCommunityPage(storiesDatum.getUserId(), storiesDatum.getComTitle(), storiesDatum.getCommunityId(), storiesDatum.getCommunity().getDescription(), storiesDatum.getComImage());
}
});
holder.txtNamePosted.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
storiesDatum = storiesDatumList.get(position);
fragmentHome.sendToUserProfile(storiesDatum.getUserId(), storiesDatum.getUsername());
}
});
holder.ivAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
storiesDatum = storiesDatumList.get(position);
fragmentHome.addCommunity(storiesDatum.getCommunityId());
}
});
holder.relativeLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupwindow_obj = popupDisplay(holder);
int currentapiVersion = Build.VERSION.SDK_INT;
if (currentapiVersion >= Build.VERSION_CODES.O) {
Log.e("tag", "checked123");
popupwindow_obj.showAsDropDown(holder.txtPostTitle, -10, -15);
} else {
Log.e("tag", "checked1233456");
popupwindow_obj.showAsDropDown(holder.txtPostTitle, 0, 10);
}
}
});
Log.e("tag", "jpined_favourite" + storiesDatum.getJoined() + storiesDatum.getFav());
if (storiesDatum.getJoined() == 0) {
holder.ivAdd.setVisibility(View.VISIBLE);
} else {
holder.ivAdd.setVisibility(View.GONE);
}
if (storiesDatum.getFav() == 1) {
holder.ivFav.setImageResource(R.mipmap.mark_as_favorite_filled);
} else {
holder.ivFav.setImageResource(R.mipmap.mark_favorite);
}
holder.txtComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragmentHome.openCommentbox();
}
});
holder.ivFav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
storiesDatum = storiesDatumList.get(position);
if (storiesDatum.getFav() == 0) {
fragmentHome.addFavt(storiesDatum.getCommunityId().toString(), "markfav");
} else {
fragmentHome.addFavt(storiesDatum.getCommunityId().toString(), "unmarkfav");
}
}
});
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragmentHome.sendToSinglePost();
}
});
}
// call this method when required to show popup
public PopupWindow popupDisplay(final PostListAdapter.MyViewHolder holder) {
final PopupWindow popupWindow = new PopupWindow(context);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_emoji, null);
ImageView imgAdvice = (ImageView) view.findViewById(R.id.imgAdvice);
ImageView imgLove = (ImageView) view.findViewById(R.id.imgLove);
ImageView imgBrokenHeart = (ImageView) view.findViewById(R.id.imgBrokenHeart);
ImageView imgEpic = (ImageView) view.findViewById(R.id.imgEpic);
ImageView imgFunny = (ImageView) view.findViewById(R.id.imgFunny);
ImageView imgHealMe = (ImageView) view.findViewById(R.id.imgHealMe);
ImageView imgHearU = (ImageView) view.findViewById(R.id.imgHearU);
ImageView imgMeToo = (ImageView) view.findViewById(R.id.imgMeToo);
ImageView imgNoEgo = (ImageView) view.findViewById(R.id.imgNoEgo);
ImageView imgRedFlag = (ImageView) view.findViewById(R.id.imgRedFlag);
ImageView imgSolidarity = (ImageView) view.findViewById(R.id.imgSolidarity);
ImageView imgTruth = (ImageView) view.findViewById(R.id.imgTruth);
ImageView imgYellowFlag = (ImageView) view.findViewById(R.id.imgYellowFlag);
ImageView imgTellMore = (ImageView) view.findViewById(R.id.imgTellMore);
imgAdvice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.advice);
holder.txtLike.setText("Advice");
popupWindow.dismiss();
}
});
imgLove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.love);
holder.txtLike.setText("Love");
popupWindow.dismiss();
}
});
imgBrokenHeart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.broken_heart);
holder.txtLike.setText("Broken Heart");
popupWindow.dismiss();
}
});
imgEpic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.epic);
holder.txtLike.setText("Epic");
popupWindow.dismiss();
}
});
imgFunny.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.funny);
holder.txtLike.setText("Funny");
popupWindow.dismiss();
}
});
imgAdvice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.advice);
holder.txtLike.setText("Advice");
popupWindow.dismiss();
}
});
imgHealMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.heal_me);
holder.txtLike.setText("Heal Me");
popupWindow.dismiss();
}
});
imgHearU.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.hear_you);
holder.txtLike.setText("I Hear You");
popupWindow.dismiss();
}
});
imgMeToo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.me_too);
holder.txtLike.setText("Me Too");
popupWindow.dismiss();
}
});
imgNoEgo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.no_ego);
holder.txtLike.setText("No Ego");
popupWindow.dismiss();
}
});
imgRedFlag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.red_flag);
holder.txtLike.setText("Red Flag");
popupWindow.dismiss();
}
});
imgSolidarity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.solidarity);
holder.txtLike.setText("Solidarity");
popupWindow.dismiss();
}
});
imgTruth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.truth);
holder.txtLike.setText("Truth");
popupWindow.dismiss();
}
});
imgYellowFlag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.yellow_flag);
holder.txtLike.setText("Yellow Flag");
popupWindow.dismiss();
}
});
imgTellMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ivLike.setImageResource(R.mipmap.tellmore);
holder.txtLike.setText("Tell More");
popupWindow.dismiss();
}
});
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
return popupWindow;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return storiesDatumList.size();
}
private static class ViewHolder {
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtNamePosted, txtCommunityName, txtPostTitle, tvPost, txtLike, txtComment, txtTime, txtCommentCount, txtLikeCount;
Button btnPay;
RelativeLayout layout, relativeLike, relativeLayout;
LinearLayout linear;
ImageView ivAdd, ivImage, ivLike, ivFav, ivPost;
SimpleExoPlayerView exoPlayerView;
;
public MyViewHolder(View itemView) {
super(itemView);
}
}
}
通过片段调用适配器:
JSONArray jsonArray = jsonObject.getJSONArray("data");
if (jsonArray.length() > 0) {
list.setVisibility(View.VISIBLE);
txtNoData.setVisibility(View.GONE);
List<StoriesDatum> datumArrayList = new ArrayList<>();
datumArrayList = Arrays.asList(gson.fromJson(jsonArray.toString(), StoriesDatum[].class));
final PostListAdapter postListAdapter = new PostListAdapter(getActivity(), datumArrayList, FragmentHome.this);
RecyclerView.LayoutManager LayoutManager1 = new LinearLayoutManager(getActivity());
list.setItemAnimator(new DefaultItemAnimator());
list.setLayoutManager(LayoutManager1);
// set elements to adapter
list.setAdapter(postListAdapter);
}
我不确定如何使它平滑滚动。请帮忙。我的页面仅包含15个项目。
答案 0 :(得分:1)
您的适配器确实非常繁重,尤其是onBindViewHolder
方法。
一些提示:
FragmentHome
并改用interface),ViewHolder
的新实例中,ExoPlayer
中,您都应将播放视频委托给新屏幕,ViewHolder
逻辑。答案 1 :(得分:0)
添加nestedScrollingEnabled false进行回收视图 ->> android:nestedScrollingEnabled =“ false”
示例
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_home_"
android:layout_width="match_parent"
android:nestedScrollingEnabled="false"
android:layout_height="match_parent" />