gpt4 book ai didi

android - Recyclerview 内的 Recyclerview 滚动不流畅

转载 作者:太空狗 更新时间:2023-10-29 14:47:16 25 4
gpt4 key购买 nike

我有一个垂直的 Recyclerview(parent) 里面有三行。每一行都是 RecyclerView(child1)、RecyclerView(child2)、RecyclerView(child3),每行都有不同类型的布局管理器,比如 child1 有 gridLayout 管理器,child2 和 3 有 Vertical LinearLayout 管理器。

所以当我在行中设置它们时,每一行都有不同类型的适配器。但是当我滚动 recyclerview 时出现问题。滚动不流畅,第二行可见需要 700 毫秒。

我的 row_list_item 布局是:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/ll_main_block"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="10dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="12dp"
android:paddingEnd="10dp"
android:gravity="center_vertical"
android:weightSum="1">

<TextView
android:id="@+id/tv_heading"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:gravity="center_vertical"
android:text=""
android:textColor="@color/dark_grey"
android:textSize="@dimen/midium_text_size" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:id="@+id/linearLayout_viewall"
android:visibility="gone"
android:gravity="center_vertical|end"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_more"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawablePadding="5dp"
android:gravity="center_vertical|end"
android:text="@string/view_all"
android:textSize="@dimen/small_text_size" />

<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:contentDescription="@null"
android:src="@drawable/next" />
</LinearLayout>
</LinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_category_outlet"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>

我将一些主要的适配器代码与 multiType 放在一起:

@Override
public int getItemCount() {
int size = 4;
if (isErrorView) {
size = 1;
}
return size;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;

if (viewType == TYPE_NEARBY
|| viewType == TYPE_PEOPLE || viewType == TYPE_DEFAULT || viewType == TYPE_SERVICES) {

view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_discover_adapter, parent, false);
RecyclerView.ViewHolder timelineViewHolder = new vHolderEverything(view);
return timelineViewHolder;

} else if (viewType == TYPE_ERROR) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.inflator_error_listview, parent, false);
vHolderError timelineViewHolder = new vHolderError(view);
return timelineViewHolder;
}
return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
String headingText = "";
boolean isMainBlockInvisible = false;
vHolderEverything mVHolder;
switch (getItemViewType(position)) {
case TYPE_ERROR:
break;
case 1:
mVHolder = (vHolderEverything) holder;
try {
GridLayoutManager lLayout = new GridLayoutManager(curr_activity, 2);
mVHolder.rv_category_outlet.setNestedScrollingEnabled(false);
mVHolder.rv_category_outlet.setHasFixedSize(true);

int spanCount = 4; // 3 columns
int spacing = 4; // 50px
boolean includeEdge = false;

mVHolder.rv_category_outlet.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
mVHolder.rv_category_outlet.setLayoutManager(lLayout);

if (dataForDiscovery != null) {
try {

if (position == TYPE_SERVICES) {
headingText = curr_activity.getResources().getString(R.string.popular_Service);

mVHolder.adapterObject_services.clear();
mVHolder.adapterObject_services.addAll(dataForDiscovery.getData().getPopularServices());
if (mVHolder.adapterObject_services != null && mVHolder.adapterObject_services.size() > 0) {
mVHolder.rv_category_outlet.setAdapter(mVHolder.discoveryPopularListAdapter);
} else {
isMainBlockInvisible = true;
}
} else {
isMainBlockInvisible = true;

}

} catch (Exception e) {
isMainBlockInvisible = true;
}
} else {
isMainBlockInvisible = true;
}

} catch (Exception e) {
isMainBlockInvisible = true;
}

if (isMainBlockInvisible) {
mVHolder.ll_main_block.setVisibility(View.GONE);
} else {
((vHolderEverything) holder).linearLayout_viewall.setVisibility(View.GONE);
mVHolder.tv_heading.setText(headingText);
mVHolder.tv_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity_Search.search_initiate_view_pager.setCurrentItem(position);
}
});
}
break;
case 2:

mVHolder = (vHolderEverything) holder;
try {
LinearLayoutManager layoutManager = new org.solovyev.android.views.llm.LinearLayoutManager(curr_activity, LinearLayoutManager.VERTICAL, false);
mVHolder.rv_category_outlet.setLayoutManager(layoutManager);
mVHolder.rv_category_outlet.setHasFixedSize(true);

if (dataForDiscovery != null) {
try {
if (position == TYPE_PEOPLE) {

mVHolder.adapterObject.clear();
mVHolder.adapterObject.addAll(dataForDiscovery.getData().getPeople());
headingText = curr_activity.getResources().getString(R.string.follow_people_discover);
if (mVHolder.adapterObject != null && mVHolder.adapterObject.size() > 0) {
mVHolder.rv_category_outlet.setAdapter(mVHolder.adapter);
} else {
isMainBlockInvisible = true;
}
} else {
isMainBlockInvisible = true;

}

} catch (Exception e) {
isMainBlockInvisible = true;
}
} else {
isMainBlockInvisible = true;
}

} catch (Exception e) {
isMainBlockInvisible = true;
}

if (isMainBlockInvisible) {
mVHolder.ll_main_block.setVisibility(View.GONE);
} else {
// if (headingText.equalsIgnoreCase("Services")) {
((vHolderEverything) holder).linearLayout_viewall.setVisibility(View.GONE);
// } else {
// ((vHolderEverything) holder).linearLayout_viewall.setVisibility(View.VISIBLE);
//}
mVHolder.tv_heading.setText(headingText);
mVHolder.tv_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity_Search.search_initiate_view_pager.setCurrentItem(position);
}
});
}
break;
case TYPE_DEFAULT:
mVHolder = (vHolderEverything) holder;
mVHolder.ll_main_block.setVisibility(View.GONE);
break;
default:
mVHolder = (vHolderEverything) holder;
mVHolder.ll_main_block.setVisibility(View.GONE);

break;
}

}

public static class vHolderEverything extends RecyclerView.ViewHolder {
TextView tv_heading;
TextView tv_more;
RecyclerView rv_category_outlet;
LinearLayout ll_main_block, linearLayout_viewall;
ArrayList<ReusableServices> adapterObject_services= new ArrayList<ReusableServices>();
DiscoveryPopularListAdapter discoveryPopularListAdapter = null;
RecyclerView.Adapter adapter = null;
ArrayList<FollowPeopleSuggestionData> adapterObject= new ArrayList<FollowPeopleSuggestionData>();

public vHolderEverything(View itemView) {
super(itemView);

this.tv_heading = (TextView) itemView.findViewById(R.id.tv_heading);
this.tv_more = (TextView) itemView.findViewById(R.id.tv_more);
this.rv_category_outlet = (RecyclerView) itemView.findViewById(R.id.rv_category_outlet);
this.ll_main_block = (LinearLayout) itemView.findViewById(R.id.ll_main_block);
this.linearLayout_viewall = (LinearLayout) itemView.findViewById(R.id.linearLayout_viewall);
MazkaraApp.getInstance().applyTypefaceItalic(tv_more);
MazkaraApp.getInstance().applyTypefaceBlack(tv_heading);


try {
if (adapterObject_services != null)
discoveryPopularListAdapter = new DiscoveryPopularListAdapter(curr_activity, adapterObject_services);
} catch (Exception e) {
e.printStackTrace();
}
try {
if (adapterObject != null)
adapter = new AdapterRecycler_Search_Users(curr_activity, curr_activity, adapterObject);
} catch (Exception e) {
e.printStackTrace();
}

}
}

只有第一次才会发生。第二次它应该正常工作。

最佳答案

@Anant 我有同样的要求,下面的技巧奏效了。将父 recyclerview 放入 NestedScrollView 并在您的 onCreate 方法中设置。

mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNestedScrollingEnabled(false);

这可以防止父 recyclerview 的滚动,并且滚动变得绝对平滑。

关于android - Recyclerview 内的 Recyclerview 滚动不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38610034/

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