gpt4 book ai didi

android - 带有 CardView 的 RecyclerView 在滚动时滞后

转载 作者:行者123 更新时间:2023-11-30 00:34:30 25 4
gpt4 key购买 nike

我在项目中使用 CardView 的 fragment 中使用 recyclweview 但是当我滚动时滞后太多,我已经看到很多解决方案但我找不到解决方案,如果有人可以帮助我那么这是我的代码,谢谢

fragment :

   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_venues_clubs, container, false);
ButterKnife.bind(this, view);

thisActivity = getActivity();
TAG = "VenuesClubsFragment";

user_id = Utils.getUserId(thisActivity);
latitude = SharedPreferenceHelper.getLocationData(thisActivity, SharedPreferenceHelper.LATITUDE);
longitude = SharedPreferenceHelper.getLocationData(thisActivity, SharedPreferenceHelper.LONGITUDE);

linearLayoutManager = new LinearLayoutManager(thisActivity) {
@Override
protected int getExtraLayoutSpace(RecyclerView.State state) {
return 300;
}
};

linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvClubList.setLayoutManager(linearLayoutManager);

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
venuesClubsBarsResponse = null;
refreshData(false, false);
}
});

btnRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
venuesClubsBarsResponse = null;
refreshData(false, false);
}
});

return view;
}

设置适配器方法:

     private void setUpRecylerViewData() {
LogUtils.i(TAG, "size " + venuesClubsBarsResponse.getData().size());

venuesAdapter = new VenuesAdapter(thisActivity, new ArrayList<>(venuesClubsBarsResponse.getData())) {
@Override
protected int getLastVisiblePosition() {
return linearLayoutManager.findLastVisibleItemPosition();
}

@Override
protected void refreshItems(boolean newOnce, boolean oldOnce) {
super.refreshItems(newOnce, oldOnce);
if (!isSearchDataAvailable) {
refreshData(newOnce, oldOnce);
}
}

@Override
protected void onItemClick(int position, VenuesClubsBarsData venuesClubsBarsData) {
super.onItemClick(position, venuesClubsBarsData);
Intent intent = new Intent(thisActivity, VenueProfileActivity.class);
intent.putExtra(EXTRA_PARAM_ID, venuesClubsBarsData.getId());
intent.putExtra(EXTRA_PARAM_NAME, venuesClubsBarsData.getVenueName());
startActivity(intent);
Utils.openActivityAnimation(thisActivity);
}
};

venuesAdapter.addAll(venuesClubsBarsResponse.getData());
rvClubList.setAdapter(venuesAdapter);
rvClubList.addOnScrollListener(venuesAdapter.onScrollListener);
}

适配器

    public abstract class VenuesAdapter extends RecyclerArrayAdapter<VenuesClubsBarsData, RecyclerView.ViewHolder> {

public ArrayList<VenuesClubsBarsData> venuesList;
private Context context;
private int lastAnimatedPosition = -1;
public RecyclerView.OnScrollListener onScrollListener;
private boolean isFromFavourite = false;

public VenuesAdapter(Context context, ArrayList<VenuesClubsBarsData> venuesClubsBarsDataArrayList) {
init(context,venuesClubsBarsDataArrayList);
}

public VenuesAdapter(Context context, ArrayList<VenuesClubsBarsData> venuesClubsBarsDataArrayList, boolean isFromFavourite) {
init(context,venuesClubsBarsDataArrayList);
this.isFromFavourite = isFromFavourite;
}


public void init(Context context, ArrayList<VenuesClubsBarsData> venuesClubsBarsDataArrayList) {
this.venuesList = venuesClubsBarsDataArrayList;
this.context = context;

onScrollListener = new RecyclerView.OnScrollListener() {
int previousLastVisibleItem = -1;

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if (dy > 0) {
int totalItem = venuesList.size();
int lastVisiblePosition = getLastVisiblePosition();
if (totalItem > 1 && previousLastVisibleItem != lastVisiblePosition) {
boolean refresh = (lastVisiblePosition >= (totalItem - 2));
if (refresh) {
// LogUtils.e("scroll down", "===============Scroll Down==============");
// LogUtils.e("last visible position", "==============" + getLastVisiblePosition());

refreshItems(false, true);
}
previousLastVisibleItem = lastVisiblePosition;
// LogUtils.e("previous last visible position", "==============" + previousLastVisibleItem);
}

}
}
};
}

protected abstract int getLastVisiblePosition();

protected void refreshItems(boolean newOnce, boolean oldOnce) {
}

protected void onItemClick(int position, VenuesClubsBarsData venuesClubsBarsData) {
}

public void addVenues(VenuesClubsBarsData venuesClubsBarsData) {

int lastPosition = venuesList.size() - 1;
venuesList.add(venuesList.size(), venuesClubsBarsData);
this.add(lastPosition, venuesClubsBarsData);
notifyItemInserted(lastPosition);
}

public void updateAll(List<VenuesClubsBarsData> clubsBarsDataList) {
if (venuesList != null) {
venuesList.clear();
}
this.clear();
venuesList = new ArrayList<>();
this.addAll(clubsBarsDataList);
venuesList.addAll(clubsBarsDataList);
notifyDataSetChanged();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_venues_clubs_bars_list, parent, false);
return new CellFeedViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
CellFeedViewHolder holder = (CellFeedViewHolder) viewHolder;
VenuesClubsBarsData venuesClubsBarsData = venuesList.get(position);
holder.bindData(position, venuesClubsBarsData, isFromFavourite);
}

@Override
public int getItemCount() {
return venuesList.size();
}

public class CellFeedViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.iv_venue_featured_pic)
ImageView ivVenueFeaturedPic;
@Bind(R.id.iv_overlay_image)
ImageView ivOverlayImage;
@Bind(R.id.tv_rating)
CustomFontTextView tvRating;
@Bind(R.id.tv_venue_name)
CustomFontTextView tvVenueName;
@Bind(R.id.tv_followers)
CustomFontTextView tvFollowers;
@Bind(R.id.tv_city_country)
CustomFontTextView tvCityCountry;
@Bind(R.id.linear_view)
LinearLayout linearView;
@Bind(R.id.iv_verified)
ImageView ivVerified;
@Bind(R.id.iv_icon)
ImageView ivIcon;
@Bind(R.id.tv_distance)
CustomFontTextView tvDistance;
@Bind(R.id.cv_club_bar)
CardView cvClubBar;
int position;
VenuesClubsBarsData venuesClubsBarsData;

public CellFeedViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);

cvClubBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClick(position, venuesClubsBarsData);
}
});
}

public void bindData(int position, VenuesClubsBarsData venuesClubsBarsData, boolean isFromFavourite) {
this.position = position;
this.venuesClubsBarsData = venuesClubsBarsData;

tvVenueName.setText(venuesClubsBarsData.getVenueName());
tvRating.setText(venuesClubsBarsData.getRating());
tvFollowers.setText(venuesClubsBarsData.getFollowers() + " followers");
tvCityCountry.setText(venuesClubsBarsData.getCity() + ", " + venuesClubsBarsData.getCountry());
tvDistance.setText(venuesClubsBarsData.getDistance() + " mi");
ivVerified.setVisibility(View.GONE);
// if (venuesClubsBarsData.isVerified()) {
// ivVerified.setVisibility(View.VISIBLE);
// } else {
// ivVerified.setVisibility(View.GONE);
// }

if (isFromFavourite){
ivIcon.setVisibility(View.VISIBLE);
if (venuesClubsBarsData.getIsThisClub()){
ivIcon.setImageResource(R.mipmap.club_icon);
}else if (venuesClubsBarsData.getIsThisBar()){
ivIcon.setImageResource(R.mipmap.bar_icon);
}
}else {
ivIcon.setVisibility(View.GONE);
}

if (StringUtils.isValid(venuesClubsBarsData.getVenueFeaturedPic())) {
try {
String imgUrl = venuesClubsBarsData.getVenueFeaturedPic();
Picasso.with(context).load(imgUrl).placeholder(R.mipmap.logo).into(ivVenueFeaturedPic);
} catch (Exception e) {
e.printStackTrace();
}
} else {
ivVenueFeaturedPic.setImageResource(R.mipmap.logo);
}
}
}
}

View .xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv_club_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardBackgroundColor="@color/blue"
android:layout_margin="4dp"
android:clickable="true">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/Venues_club_imageview_height"
android:padding="2dp"
android:orientation="vertical">


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back_ground">

<ImageView
android:id="@+id/iv_venue_featured_pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
tools:src="@mipmap/logo" />

<ImageView
android:id="@+id/iv_overlay_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/overlay_screen"
android:scaleType="fitCenter" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:orientation="horizontal">

<whatsfourtwonite.quriousclick.com.whats42nite.widget.CustomFontTextView
android:id="@+id/tv_rating"
style="@style/text_view_showing_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:textSize="17sp"
android:background="@color/toolbar_color"
android:drawablePadding="6dp"
android:drawableRight="@mipmap/star"
android:gravity="left|center_vertical"
android:paddingLeft="6dp"
android:paddingRight="6dp"
tools:text="5.0" />

</LinearLayout>

<LinearLayout
android:id="@+id/linear_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp"
android:paddingLeft="12dp">

<whatsfourtwonite.quriousclick.com.whats42nite.widget.CustomFontTextView
android:id="@+id/tv_venue_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:shadowColor="@color/black"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="5"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/colorPrimary"
android:textSize="22sp"
android:textStyle="bold"
tools:text="Venue Name" />


<whatsfourtwonite.quriousclick.com.whats42nite.widget.CustomFontTextView
android:id="@+id/tv_followers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="-5dp"
android:visibility="gone"
android:textColor="@color/colorPrimary"
android:textSize="15sp"
tools:text="2 followers" />

<whatsfourtwonite.quriousclick.com.whats42nite.widget.CustomFontTextView
android:id="@+id/tv_city_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white"
android:textSize="16sp"
tools:text="City, Country" />

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:orientation="vertical">

<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/iv_verified"
android:visibility="visible"
android:layout_gravity="bottom|right"
android:src="@mipmap/progress_sm_w03" />

<whatsfourtwonite.quriousclick.com.whats42nite.widget.CustomFontTextView
android:id="@+id/tv_distance"
style="@style/text_view_showing_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:background="@color/toolbar_color"
android:textSize="17sp"
android:drawableLeft="@mipmap/ic_near_me_white_18dp"
android:drawablePadding="3dp"
android:gravity="left|center_vertical"
tools:text="11.25 mi" />
</LinearLayout>

<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_gravity="left|top"
android:src="@mipmap/bar_icon"
android:visibility="gone" />

</FrameLayout>

</LinearLayout>

</android.support.v7.widget.CardView>

最佳答案

这不是 CardView 的问题。你的图片分辨率是多少在 RecyclerView 的布局中使用 in for ImageView。

  1. 如果您使用的是 drawable/中的高质量图像,请将这些图像移动到 drawable-hdpi 或 drawable-xhdpi。

2.如果图片来自远程服务器,尝试获取合适分辨率的图片。

关于android - 带有 CardView 的 RecyclerView 在滚动时滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43655137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com