这是我取得的成就?3个不同的部分,每个部分有10个不同的项目.
这是我正在关注的教程 链接,下面是截图:
试图显示different Views每个部分.喜欢:
对于第1节(layout_1.xml)
对于第2节(layout_2.xml)
对于第3节(layout_3.xml)
但示出了布局视图的layout_1.xml在每节 ...(第1,2,3)
我可以知道我的代码在哪里犯了错误,我错过了什么?
public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {
private ArrayList<SingleItemModel> itemsList;
private Context mContext;
public SectionListDataAdapter(Context context, ArrayList<SingleItemModel> itemsList) {
this.itemsList = itemsList;
this.mContext = context;
}
@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
switch (i) {
case 0:
View …Run Code Online (Sandbox Code Playgroud) 我正在开发一个搜索联系人功能,在该屏幕中,NestedScrolView内部有一个RecyclerView(fillViewport = true).屏幕设计:(这个设计是由客户所接受,我不能改变它)
将当前设备的所有联系人加载到ArrayList后,将从此阵列中过滤搜索结果.
有几种情况会使应用程序非常滞后:
1.当用户输入没有结果的输入时,用户清除搜索,我必须再次显示所有结果.该NestedScrollView有渲染UI进行的所有项目RecyclerView(例如:300个项目).
2.当结果数量有很多变化时(例如,1到300个项目).该NestedScrollView有渲染UI进行了大量的项目RecyclerView
我知道这个设计打破了RecyclerView的回收技术,但我无法改变它.
我尝试了什么:
recyclerView.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)
在AndroidManifest中:
android:windowSoftInputMode="adjustNothing"
Run Code Online (Sandbox Code Playgroud)
适配器:
public class RecyclerContactAdapter extends RecyclerView.Adapter<RecyclerContactAdapter.ViewHolder> {
private List<MobileContact> contacts;
private Context context;
public RecyclerContactAdapter() {
contacts = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.context = parent.getContext();
View view = LayoutInflater.from(context)
.inflate(R.layout.item_recycler_contact, parent, false);
return new ViewHolder(view); …Run Code Online (Sandbox Code Playgroud) performance android android-recyclerview android-nestedscrollview
经过大量搜索之后,我知道使用常规适配器是可行的,但是我不知道如何使用分页库来实现。我不需要代码只是一个提示。
例

如果我有50种观点怎么办?我的适配器中应该有50个静态内部类吗?根据这个答案,是的.
我的第一个想法是将每个视图内部类移动到一个单独的公共类中,但它们必须是静态的.所以将每个封装到一个公共类中,使内部类静态?有没有更好的选择?
编辑:代码示例:那么这将是一个很好的解决方案?这不也会扼杀性能吗?
public class MainViewHolder extends DragSortAdapter.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
View container;
TextView title;
//called in onCreateViewHolder in adapter
public MainViewHolder(DragSortAdapter adapter, View itemView) {
super(adapter, itemView);
container = itemView.findViewById(R.id.card_root);
title = container.findViewById(R.id.text);
}
//called by onBindViewHolder in adapter
public void setData(Data data) {
title.setText(data.title);
}
}
Run Code Online (Sandbox Code Playgroud)
edit2:sample,用于返回viewholder的新实例
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new MainViewHolder(...);
case 2: return new MainViewHolderOther(...);
...
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用嵌套的RecyclerView.在垂直RecyclerView内的手段我有多个水平回收视图
我将适配器附加到父RecyclerView的onBindViewHolder方法内的水平recylerviews,如下所示.
@Override
public void onBindViewHolder(final MainViewHolder holder, final int position) {
switch (holder.getItemViewType()) {
case TYPE_PRODUCT:
((ListHolderProduct) holder).itemTitle.setText(browseCategoryHomePageItems.get(position).displayName.toUpperCase());
((ListHolderProduct) holder).recyclerView.setAdapter(new CarouselProductsRecyclerAdapter(context
, browseCategoryHomePageItems.get(position).products
, R.layout.activity_categoryhome_products_grid_item
, nestedRecyclerItemClickedListener
, position));
break;
case TYPE_DEAL:
((ListHolderDeal) holder).itemTitle.setText(browseCategoryHomePageItems.get(position).displayName.toUpperCase());
((ListHolderDeal) holder).recyclerView.setAdapter(new CarouselDealsRecyclerAdapter(context
, browseCategoryHomePageItems.get(position).dealItems
, R.layout.activity_categoryhome_deals_grid_item
, nestedRecyclerItemClickedListener
, position));
break;
//few more types like this
}
}
Run Code Online (Sandbox Code Playgroud)
现在每当我滚动页面时,由于我将适配器连接到OnBindViewHolder上的水平RecyclerView,所以它有点滞后
并且可以有N个TYPE_PRODUCT或任何类型的水平列表.意味着可以存在多个相同类型的水平列表.
知道如何优化这个东西并提高滚动速度.
它是滞后的,因为之前每次为列表调用setAdapter.
更新我正在扩展LinearLayoutManager,并在我设置extraLayout空间已修复我的问题但我不知道这是正确的方式我是设置额外的空间如下.
layoutManager.setExtraLayoutSpace(2 * this.getResources().getDisplayMetrics().heightPixels);
Run Code Online (Sandbox Code Playgroud)
而follwoing是自定义布局管理器类
public class PreCachingLayoutManager extends LinearLayoutManager {
private static final int DEFAULT_EXTRA_LAYOUT_SPACE = 600;
private int extraLayoutSpace = …Run Code Online (Sandbox Code Playgroud) performance android adapter android-viewholder android-recyclerview
是否可以在我的适配器中创建多视图类型..就像为我的标题添加视图然后在标题下面是一个列表.
我的适配器的代码段:
public class StoreAdapter extends RecyclerView.Adapter<StoreAdapter.BindingHolder> {
List<Store> mStoreList;
public class BindingHolder extends RecyclerView.ViewHolder {
private ViewDataBinding binding;
public BindingHolder(View v) {
super(v);
binding = DataBindingUtil.bind(v);
}
public ViewDataBinding getBinding() {
return binding;
}
}
public StoreAdapter(List<Store> storeList) {
this.mStoreList = storeList;
}
@Override
public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_store, parent, false);
BindingHolder holder = new BindingHolder(v);
return holder;
}
@Override
public void onBindViewHolder(BindingHolder holder, int position) {
final Store store = mStoreList.get(position);
holder.getBinding().setVariable(BR.store, …Run Code Online (Sandbox Code Playgroud) android android-adapter android-studio android-recyclerview android-databinding
好吧,我之前尝试过的一些方法:
1.使用CollapsingToolbarLayout
我把我CardView的insid CollapsingToolbarLayout并把他们都在AppBarLAyout.
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed" >
<CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<!-- My Views Goes there -->
</CardView
<android.support.v7.widget.Toolbar
android:id="@+id/flexible.example.toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@null"
app:layout_collapseMode="pin"
style="@style/ToolBarWithNavigationBack"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RecyclerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
></RecyclerView>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
PS:我删除了不相关的代码,不提我
这种方式正常但是!!! 当CardView高度高于屏幕高度时,它的内容由AppBarLayout用户显示并且不向用户显示
2.使用NestedScrollView
我放入CardView和RecyclerView内部NestedScrollView.但问题是当用户到达时回收到RecyclerView,然后滚动回到顶部,fling会变得迟钝和错误并停止一些用户必须滚动越来越多的地方!
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/nested_scrollbar_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.CardView
android:id="@+id/flexible.example.cardview"
android:layout_width="match_parent" …Run Code Online (Sandbox Code Playgroud) android android-recyclerview android-collapsingtoolbarlayout android-appbarlayout nestedscrollview
我在我的应用程序中使用了 RecyclerView,但是当我滚动我的 RecyclerView 时,我的项目将消失!我在很多 RecyclerView 中使用了这段代码,但这次我不知道发生了什么。有我的活动代码:
public class ActivityStartup extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
RecyclerView menuListRecycler;
RecyclerView schedualRecycler;
private List<ReminderModel> tempSchedualArray = new ArrayList<>();
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
LinearLayoutManager mSchedualLayoutManager = new LinearLayoutManager(ActivityStartup.this);
private FloatingActionButton mAddReminderButton;
RecyclerView.Adapter mSchedualAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toolbar = (Toolbar) findViewById(R.id.toolBar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigationView); …Run Code Online (Sandbox Code Playgroud) public abstract class BaseAdapters extends RecyclerView.Adapter<BaseAdapters.MyViewHolder> implements View.OnClickListener {
protected Context parentContext;
public int layout_id;
protected List<?> dataList = new ArrayList<>();
public class MyViewHolder extends RecyclerView.ViewHolder {
MyViewHolder(View view) {
super(view);
declareViews(view,this);
}
}
@Override
public void onClick(View view) {
onClickViews(view);
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
bindView(holder, i);
}
public void notifyList(List<?> filterdNames) {
this.dataList = filterdNames; …Run Code Online (Sandbox Code Playgroud) android ×10
adapter ×4
performance ×2
android-collapsingtoolbarlayout ×1
cardview ×1
java ×1
kotlin ×1
layout ×1
xml ×1