来自https://developer.android.com/preview/material/ui-widgets.html
当我们创建时,RecyclerView.Adapter我们必须指定ViewHolder将与适配器绑定.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.some_layout, parent, false);
//findViewById...
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
@Override
public int …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用新的RecyclerView,但我找不到一个RecyclerView不同类型的行/卡片视图被夸大的例子.
随着ListView我覆盖getViewTypeCount和getItemViewType,用于处理不同类型的行.
我应该像"旧"方式那样做,还是应该做点什么LayoutManager?我想知道是否有人可以指出我正确的方向.因为我只能找到一种类型的例子.
我想要一张略有不同卡片的清单.或者我应该只使用一个scrollView与cardViews它里面......度过,即使没有适配器和recyclerView?
我在RecyclerView里面用NestedScrollView.我也设置setNestedScrollingEnabled为假recyclerview
支持较低的API
ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);
现在!当用户滚动视图时,每件事似乎都没问题,但是!!! recyclerview中的视图不回收!和堆大小迅速增长!!
更新:RecyclerView布局管理器是StaggeredLayoutManager
fragment_profile.xml:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/profileSwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- RecyclerView and NestedScrollView -->
<include layout="@layout/fragment_profile_details" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
fragment_profile_details.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical" >
<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:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/nested_scrollbar_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<android.support.v7.widget.CardView
android:id="@+id/profileCardview"
android:layout_width="match_parent" …Run Code Online (Sandbox Code Playgroud) 我有一个NestedScrollView包含一个LinearLayout和一个RecyclerView(都在RelativeLayout内).
<android.support.design.widget.CoordinatorLayout
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="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scroll">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lay_account">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:paddingTop="10dp"
android:id="@+id/lay_total"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/total"
android:id="@+id/lblTotal"
android:textSize="@dimen/text_medium"
android:layout_marginLeft="20dp"
android:textColor="@color/dark_eurakostheme_color"
android:textStyle="bold"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtTotalAmount"
android:textSize="@dimen/text_extra_extra_large"
android:gravity="center_horizontal"
android:textColor="@color/eurakostheme_color"/>
<ImageView
android:layout_width="@dimen/icon_extra_extra_large"
android:layout_height="match_parent"
android:id="@+id/imageView3"
android:src="@drawable/coin_eurakos"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="@+id/divisor"
android:background="@color/dividerColor"
android:layout_alignBottom="@+id/lay_total"/>
<android.support.v7.widget.RecyclerView
android:layout_below="@id/lay_total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rclBasketAmounts"
android:background="@android:color/white"
android:padding="10dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
将数据加载到RecyclerView后,我需要以编程方式更改它的高度,如下所示:
RelativeLayout.LayoutParams lay_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
50 + ((int) (baskets.length * getResources().getDimension(R.dimen.list_height_small))));
lay_params.addRule(RelativeLayout.BELOW, …Run Code Online (Sandbox Code Playgroud) 我有一个包含370个项目的交错网格,带有图像.
我想确保快速回收物品以注意内存,但是创建ViewHolder然后绑定到适配器中的每个项目,并且不关注子项是否可见
我尝试了以下内容
StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
rv.setLayoutManager(lm);
rv.setItemViewCacheSize(20); //Has no effect
RecyclerView.RecycledViewPool pool = new RecyclerView.RecycledViewPool();
pool.setMaxRecycledViews(0, 20);
rv.setRecycledViewPool(pool); //also has no effect
Run Code Online (Sandbox Code Playgroud)
日志显示onCreateViewHolder和onBindViewHolder每次调用185次.然后在恢复对onCreateViewHolder的调用之前调用onViewRecycled 185次,直到我们达到完整的370.
这对我来说可能是一个理解问题,但我认为RecyclerView应该只绑定那些可见的视图,或者仅仅支持20个视图,或者20个池中的20个,但是很多适合屏幕.如何使用StaggeredGridLayoutManager实现此目的?
如果我听滚动更改并使用findFirstCompletelyVisibleItemPositions和findLastCompletelyVisibleItemPositions,这仍然跨越适配器中的每一项,而不仅仅是适合屏幕的6项
我的适配器代码
class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
static final int NUM_COLS = 3;
private final LayoutInflater mInflater;
private final List<GridItem> mEntries;
private int mLastExpanded; //stores where the last expanded item was
private OnCardClickListener mOnItemClick;
MyAdapter(Context context) {
super();
mEntries = new ArrayList<>();
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
void setOnTileClickListener(@Nullable OnCardClickListener listener) …Run Code Online (Sandbox Code Playgroud) android layout-manager staggeredgridlayout android-recyclerview