Android: How to scroll a parentView when child containing recyclerView is scrolled

时间:2017-04-06 17:11:52

标签: java android xml android-fragments android-recyclerview

I have created an app that contains a viewPager inside the mainActivity, this viewPager contains 5 fragments, of which 4 are recyclerViews and one is a normal linearLayout containing some text...

Here is a screenshot of the app, not all tabs in the tablayout are visible:

screenshot

Now, as you might have seen already, there isn't much space in the viewPager for the user to see anything, so they have to scroll too much to view things in the recylerView. I want to modify my app so that when the user tries to scroll inside the recyclerView, the visible part of the mainActivity is scrolled down till the recyclerView occupies the entire page and then the recyclerView begins to scroll normally.

Can someone please help me implement this type of scroll feature into my app. You can just check out this app for a reference to what I'm saying. Just open up any movie or tvSeries and then try scrolling, the mainActivity gets scrolled first and then the rest of the layout.... Can someone please help. I've already tried the solutions on stackOverflow and many of them don't work, I also tried to google for a solution, but didn't get anything useful....

Here is the code for my adapter:

public class cardViewAdapterCreditsCast extends RecyclerView.Adapter<cardViewAdapterCreditsCast.ViewHolder> {

private Context context;
private List<creditsModel> creditsModels;

public cardViewAdapterCreditsCast(Context context, List<creditsModel> creditsModels) {
    this.context = context;
    this.creditsModels = creditsModels;
}

@Override
public cardViewAdapterCreditsCast.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_cast_row, parent, false);

    return new cardViewAdapterCreditsCast.ViewHolder(view);
}

@Override
public void onBindViewHolder(cardViewAdapterCreditsCast.ViewHolder holder, int position) {
    final creditsModel creditsModel = creditsModels.get(position);

    int tempNumber = holder.celebImage.getWidth();
    holder.celebImage.setMinimumHeight(tempNumber);
    holder.celebImage.setMaxHeight(tempNumber + 1);

    String imagePath = "https://image.tmdb.org/t/p/w500" + creditsModel.getProfilePath();

    holder.starringAs.setText(creditsModel.getCharacter());
    holder.celebName.setText(creditsModel.getActorName());

    if (creditsModel.getProfilePath() != null) {
        Picasso.with(context).load(imagePath).transform(new CircleTransform()).into(holder.celebImage);
    } else
        holder.celebImage.setBackgroundResource(R.drawable.not_found);
}

@Override
public int getItemCount() {
    return creditsModels.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView celebImage;
    public TextView celebName;
    public TextView starringAs;

    private LinearLayout linearLayout;

    public ViewHolder(View itemView) {
        super(itemView);

        linearLayout = (LinearLayout) itemView.findViewById(R.id.castRowMainLinearLayout);

        celebImage = (ImageView) itemView.findViewById(R.id.castRowImage);
        celebName = (TextView) itemView.findViewById(R.id.castRowName);
        starringAs = (TextView) itemView.findViewById(R.id.castRowAppearance);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果滚动视图或回收视图不止一个,则使用嵌套的滚动视图。

答案 1 :(得分:0)