我在 a 中有两个RecylerViews
在 a 中LinearLayout
具有垂直方向ScrollView
。两者RecyclerViews
都想在TextViews
. 这种安排是这样的,在开始时,TextViews
只有第一个RecyclerView
出现,而在滚动时,第二个RecyclerView
出现。
我的期望是,由于TextViews
第二个中的任何一个都不RecyclerView
可见,它们将不会被创建,并且一旦它们出现,onBindViewHolder
将被调用以重用现有的ViewHolders
. 然而,所有的个体ViewHolders
都是被创造(onCreateView
被称为)的。难道我做错了什么?因为如果这是预期的,那么设置一个通用的 ViewPool 是没有意义的,是吗?
活动代码
public class MainActivity extends AppCompatActivity {
private List<String> names = Arrays.asList(
"Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6"
);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
RecyclerView recycler1 = (RecyclerView) findViewById(R.id.recycler1);
RecyclerView recycler2 = (RecyclerView) findViewById(R.id.recycler2);
recycler1.setLayoutManager(new LinearLayoutManager(this));
recycler2.setLayoutManager(new LinearLayoutManager(this));
recycler1.setRecycledViewPool(viewPool);
recycler2.setRecycledViewPool(viewPool);
recycler1.setNestedScrollingEnabled(false);
recycler2.setNestedScrollingEnabled(false);
recycler1.setAdapter(new SampleAdapter(new ArrayList<>(names), 1));
recycler2.setAdapter(new SampleAdapter(new ArrayList<>(names), 2));
}
private class SampleAdapter extends RecyclerView.Adapter {
private final List<String> mSampleList;
private final int mRecyclerId;
SampleAdapter(List<String> sampleList, int recyclerId) {
mSampleList = sampleList;
mRecyclerId = recyclerId;
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i("TESTING", "creating a new view for parent Id: " + mRecyclerId);
TextView textView = (TextView) getLayoutInflater().inflate(R.layout.text_view_name, null);
return new SampleViewHolder(textView);
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Log.i("TESTING", "binding existing view for parent Id: " + mRecyclerId + " for position: " +
position);
((SampleViewHolder)holder).bind(mSampleList.get(position));
}
public int getItemCount() {
return mSampleList.size();
}
}
private class SampleViewHolder extends RecyclerView.ViewHolder {
private TextView mText;
public SampleViewHolder(TextView itemView) {
super(itemView);
mText = itemView;
}
public void bind(String s) {
mText.setText(s);
}
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/recycler_containers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"/>
</LinearLayout>
</ScrollView>
文本视图
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/name_text_view"
android:layout_width="match_parent"
android:layout_height="400dp"
android:paddingTop="40dp"
android:paddingRight="40dp"
android:paddingBottom="40dp"
android:paddingLeft="40dp"
android:textColor="@color/white"
android:gravity="center">
</TextView>