我有2个recycler view如下图所示,问题是当我滚动第二个recylcer视图时,第一个recyclerview仍然被修复。当我在第二个recyclerview中向下滚动时,我希望第一个消失。如果我在上面滚动,就会出现RecyclerView 1。怎么做?
主要活动代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// initToolbars();
recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
recylerViewLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(recylerViewLayoutManager);
recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, subjects,images);
recyclerView.setAdapter(recyclerViewAdapter);
MainGridRecView = (RecyclerView) findViewById(R.id.maingridrc);
MainGridRecViewLayoutManager = new GridLayoutManager(context,2);
MainGridRecView.setLayoutManager(MainGridRecViewLayoutManager);
MainGridRecViewAdapter = new MainGridRecyclerViewAdapter(MainActivity.this, subjects,images);
MainGridRecView.setAdapter(MainGridRecViewAdapter);
活动XML:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/liner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_above="@+id/recyclerview1"
android:gravity="center"
android:background="#10bcc9"
android:textColor="@android:color/white"
android:text="New Games." />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_width="wrap_content"
android:layout_height="140dp"
android:scrollbars="horizontal" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="@+id/recyclerview1"
android:gravity="center"
android:background="#10bcc9"
android:textColor="@android:color/white"
android:text="Used Games." />
<android.support.v7.widget.RecyclerView
android:id="@+id/maingridrc"
android:layout_width="wrap_content"
android:layout_height="330dp"
android:layout_marginTop="5dp"
android:scrollbars="vertical" />
<!-- <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />-->
</LinearLayout>
适配器
public class RecyclerViewAdapter extends RecyclerView.Adapter<Viewholder>{
String[] SubjectValues;
String[] imageValues;
Context mContext;
View view1;
ViewHolder viewHolder1;
// TextView textView;
public RecyclerViewAdapter(Context context,String[] SubjectValues1, String[] images){
SubjectValues = SubjectValues1;
imageValues= images;
mContext = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View v){
super(v);
}
}
@Override
public Viewholder onCreateViewHolder(ViewGroup parent, int viewType){
view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items,parent,false);
// viewHolder1 = new Viewholder(view1);
Viewholder vh = new Viewholder(view1, new Viewholder.IMyViewHolderClicks() {
public void onPotato(View caller) { Log.d("VEGETABLES", "Poh-tah-tos");
Intent intent = new Intent(mContext,SingleObjectActivity.class);
mContext.startActivity(intent);
};
public void onTomato(ImageView callerImage) {
Log.d("VEGETABLES", "To-m8-tohs");
// v1 CategoryList mDataset = new CategoryList(getOrder(), getId(), item.getUrl(), item.getUserName(), item.getLikes());
Intent intent = new Intent(mContext,SingleObjectActivity.class);
mContext.startActivity(intent);
}
});
return vh;
}
@Override
public void onBindViewHolder(Viewholder holder, int position){
holder.txtViewTitle.setText(SubjectValues[position]);
holder.imgViewIcon.setImageResource(R.drawable.ghost_recon);
}
@Override
public int getItemCount(){
return SubjectValues.length;
}
}
回收程序XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imagecateg"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/txtview"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/imagecateg"
android:paddingLeft="35dp"
android:textSize="15dp"
android:textStyle="bold"
android:background="#f4e04c"
android:textColor="@color/colorPrimary"
/>
</RelativeLayout>
编辑
最佳答案
看看下面的例子,
mainActivity.Class类
public class MainActivity extends AppCompatActivity {
List<String> horizontalArrayList = new ArrayList<>();
List<String> verticalArrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verticalArrayList.add("fake entry");
for(int i=0;i<20;i++){
horizontalArrayList.add("user "+i);
}
for(int i=0;i<40;i++){
verticalArrayList.add("user "+i);
}
RecyclerView verticalRecyclerView = (RecyclerView) findViewById(R.id.verticalRecyclerView);
verticalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
VerticalAdapter mAdapter = new VerticalAdapter(this, horizontalArrayList,verticalArrayList);
verticalRecyclerView.setAdapter(mAdapter);
}
}
活动\主.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.recyclerviewdemo.android.activities.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/verticalRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
水平适配器.class
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder> {
private List<String> mArrayList = new ArrayList<>();
private Context mContext;
public HorizontalAdapter(Context mContext, List<String> mArrayList) {
this.mContext = mContext;
this.mArrayList = mArrayList;
}
@Override
public HorizontalAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_horizontal, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(HorizontalAdapter.MyViewHolder holder, int position) {
holder.textView.setText(mArrayList.get(position));
}
@Override
public int getItemCount() {
return mArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(View v) {
super(v);
textView = (TextView) v.findViewById(R.id.textView);
}
}
}
项目_horizontal.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/colorAccent"
android:padding="5dp" />
</LinearLayout>
垂直适配器类
public class VerticalAdapter extends RecyclerView.Adapter<VerticalAdapter.MyViewHolder> {
private int ITEM_TEXT_VIEW = 0, ITEM_HEADER_VIEW = 1;
private List<String> mVerticalArrayList = new ArrayList<>();
private List<String> mHorizontalArrayList = new ArrayList<>();
private Context mContext;
public VerticalAdapter(Context mContext, List<String> mHorizontalArrayList,List<String> mVerticalArrayList) {
this.mContext = mContext;
this.mVerticalArrayList = mVerticalArrayList;
this.mHorizontalArrayList = mHorizontalArrayList;
}
@Override
public VerticalAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_vertical, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(VerticalAdapter.MyViewHolder holder, int position) {
if (getItemViewType(position) == ITEM_HEADER_VIEW) {
holder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext,
LinearLayoutManager.HORIZONTAL, false));
HorizontalAdapter mAdapter = new HorizontalAdapter(mContext, mHorizontalArrayList);
holder.recyclerView.setAdapter(mAdapter);
holder.textView.setVisibility(View.GONE);
} else {
holder.textView.setText(mVerticalArrayList.get(position));
holder.recyclerView.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return mVerticalArrayList.size();
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return ITEM_HEADER_VIEW;
} else {
return ITEM_TEXT_VIEW;
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
RecyclerView recyclerView;
public MyViewHolder(View v) {
super(v);
textView = (TextView) v.findViewById(R.id.textView);
recyclerView = (RecyclerView) v.findViewById(R.id.horizontalRecyclerView);
}
}
}
项目_vertical.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/horizontalRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>