Вот полный код со всеми изменениями. Я проверил ваш код, и он работает нормально.
MainActivity Java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity{
RecyclerView topRecyclerView, bottomRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_main);
// Fetching View From XMl
topRecyclerView = findViewById(R.id.topRecyclerView);
bottomRecyclerView = findViewById(R.id.bottomRecyclerView);
// Data For Top Recycler Views
List<ItemModel> topRecycleData = new ArrayList<ItemModel>();
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 1"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 2"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 3"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 4"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 5"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 6"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 7"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 8"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 9"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 10"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 11"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 12"));
// Data For Bottom Recycler Views
List<ItemModel> bottomRecycleData = new ArrayList<ItemModel>();
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 1"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 2"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 3"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 4"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 5"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 6"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 7"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 8"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 9"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 10"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 11"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 12"));
// Setting Layouts To Recycler Views
topRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
bottomRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// Creating Adapters (No need to send onNote)
RecyclerAdapter topAdapter = new RecyclerAdapter
(this,R.layout.item_layout_vertical,topRecycleData);
RecyclerAdapter bottomAdapter = new RecyclerAdapter
(this,R.layout.item_layout_horizontal,bottomRecycleData);
// Setting Adapters To Layouts
topRecyclerView.setAdapter(topAdapter);
bottomRecyclerView.setAdapter(bottomAdapter);
}
}
MainActivity XML Использование Scoll View и Linear Layout с весамина внутренних линейных макетах, чтобы масштабировать их одинаково по высоте экрана. В вашем коде верхний макет скрывался нижним макетом. Представление прокрутки должно содержать одного дочернего элемента, поэтому в качестве дочернего элемента представления прокрутки добавляется дополнительный линейный макет.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:scrollbars="none"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_margin="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4CE9D9"
android:text="Header No 1"
android:textSize="18sp"
android:textColor="#000"
android:padding="5dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/topRecyclerView"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomRecyclerViewLayout"
android:layout_margin="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4CE9D9"
android:text="Header No 2"
android:textSize="18sp"
android:textColor="#000"
android:padding="5dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/bottomRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
item_layout_vertical xml Добавьте идентификатор в верхний макет, это будет использоваться для настройки прослушивателя onclick позже.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/verticalContainer"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/imgTitle"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:text="Hello Dear Dibas"/>
</LinearLayout>
item_layout_horizontally xml Добавьте идентификатор в верхний макет, это будет использоваться для настройки прослушивателя onclick позже.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalContainer"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/imgTitle"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:text="Hello Dear Dibas"/>
</LinearLayout>
RecyclerAdapter Java
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>{
Context context;
int LayoutId;
List<ItemModel> data;
public RecyclerAdapter(Context context, int layoutId, List<ItemModel> data) {
this.context = context;
LayoutId = layoutId;
this.data = data;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View myView = inflater.inflate(LayoutId,null);
return new RecyclerViewHolder(myView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, final int
position) {
final ItemModel singleItem = data.get(position);
holder.imgTitle.setText(singleItem.getImgTitle());
holder.img.setImageDrawable(context.getResources()
.getDrawable(singleItem.getImgId()));
//checking which layout is in layoutId and adding onclick listener
if (LayoutId == R.layout.item_layout_horizontal)
{
holder.horizontalLayout.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
//check which item is clicked here and proceed
if(position==0)
{
//do something
}
else if(position==1)
{
//do something
}
else if(position==2)
{
//do something
}
//and so on to the list end
}
});
}
else {
holder.verticalLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//check which item is clicked here and proceed
if(position==0)
{
//do something
}
else if(position==1)
{
//do something
}
else if(position==2)
{
//do something
}
//and so on to the list end
}
});
}
}
@Override
public int getItemCount() {
return data.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView imgTitle;
ImageView img;
LinearLayout horizontalLayout,verticalLayout;
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
imgTitle = itemView.findViewById(R.id.imgTitle);
img = itemView.findViewById(R.id.img);
//fetching the container views that are to be attached to the
//recycler view and for adding onclick listeners
//(because of multiple layouts for the same adapter)
if(LayoutId==R.layout.item_layout_horizontal)
horizontalLayout=itemView.findViewById(R.id.horizontalContainer);
else
verticalLayout=itemView.findViewById(R.id.verticalContainer);
}
}
}
Надеюсь, что этопомогает. Работает нормально. Добавьте тост в условия прослушивания щелчка, чтобы проверить, работают ли они для конкретной позиции. И важно отметить, что индекс позиции начинается с 0 (первый элемент) и так далее.