web-dev-qa-db-ja.com

パフォーマンスを維持しながら、ネストされたRecyclerViewをレイアウトするにはどうすればよいですか?

Google Playミュージックの「Listen Now」レイアウトに似たものを実現しようとしています。 Webで見つけたすべての例は、単一の単純なRecyclerViewです。もっと複雑なことを達成しようとしています。何かのようなもの

enter image description here

レイアウト全体(ツールバーを除く)を単一のRecyclerViewにして、さらに2つのRecyclerViewを含めることはできますか?何かのようなもの

最終的に、私が達成したいのは、以下のようなレイアウトで、パフォーマンスを維持することです。

<RecyclerView> //vertical
    <RecyclerView/> //vertical
    <RecyclerView/> //horizontal
    <LinearLayout/> //horizontal
</RecyclerView>
32
ZakTaccardi

Recyclerviewタグ内でrecyclerviewを取得することはできません。むしろ、最初のアダプターのbindViewHolder呼び出しで、次のようにrecyclerviewアダプターを再度呼び出します。

InnerRecyclerviewAdapter adapter=new InnerRecyclerviewAdapter(context,urlistArray);
holder.recyclerView.setAdapter(adapter);
holder.recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);

wrap_contentlatestrecyclerviewでも動作します

詳細については、このリンクを確認してください https://guides.codepath.com/Android/Heterogenous-Layouts-inside-RecyclerView

29
Sadashiv

私はあなたが垂直RecyclerView内に水平RecyclerViewを持っているケースを解決しようとしました、これは私のコードです。

SingleFragmentActivity

package com.example.uitestingmaterialdesign;

import Android.os.Bundle;
import Android.support.v4.app.Fragment;
import Android.support.v4.app.FragmentManager;
import Android.support.v7.app.AppCompatActivity;


public abstract class SingleFragmentActivity extends AppCompatActivity {

protected abstract Fragment createFragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_fragment_activity);

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.simple_fragment_container);

    if (fragment == null) {
        fragment = createFragment();
        fm.beginTransaction()
                .add(R.id.simple_fragment_container, fragment)
                .commit();
    }
}

}

MainActivity

package com.example.uitestingmaterialdesign;

import Android.support.v4.app.Fragment;

public class MainActivity extends SingleFragmentActivity {

@Override
protected Fragment createFragment() {
    return new PrimaryRecyclerViewFragment();
}

}

PrimaryRecyclerView

package com.example.uitestingmaterialdesign;

import Android.os.Bundle;
import Android.support.annotation.Nullable;
import Android.support.v4.app.Fragment;
import Android.support.v7.widget.LinearLayoutManager;
import Android.support.v7.widget.RecyclerView;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.TextView;


public class PrimaryRecyclerViewFragment extends Fragment {

private RecyclerView mPrimaryRecyclerView;
private String[] mMoviesGenre, mActionMovies;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mMoviesGenre = new String[]{
            "Action", "Adventure", "Comedy", "Crime", "Fantasy",
            "Historical", "Horror", "Magical", "Mystery", "Paranoid"
    };

    mActionMovies = new String[] {"Mission: Impossible – Rogue Nation", 
            "Mad Max: Fury Road", "Star Wars: The Force Awakens",
            "Avengers: Age of Ultron", "Ant- Man","Terminator Genisys",        "Furious 7",              "Blackhat", "The Man from U.N.C.L.E",
            "Jurassic World"
    };

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.primary_recycler_view, container, false);

    // Creating the primary recycler view adapter
    PrimaryAdapter adapter = new PrimaryAdapter(mMoviesGenre);

    LinearLayoutManager layoutManager = new LinearLayoutManager(
            getActivity(),
            LinearLayoutManager.VERTICAL,
            false
    );

    mPrimaryRecyclerView = (RecyclerView) view.findViewById(R.id.primary_recycler_view);
    mPrimaryRecyclerView.setLayoutManager(layoutManager);
    mPrimaryRecyclerView.setAdapter(adapter);

    return view;
}

private class PrimaryViewHolder extends RecyclerView.ViewHolder {
    private TextView mPrimaryMovieGenre;
    private RecyclerView mSecondaryRecyclerView;

    public PrimaryViewHolder(View itemView) {
        super(itemView);
        mPrimaryMovieGenre = (TextView) itemView.findViewById(R.id.primary_movie_genre);
        mSecondaryRecyclerView = (RecyclerView) itemView.findViewById(R.id.secondary_recycler_view);
    }

    // This get called in PrimaryAdapter onBindViewHolder method
    public void bindViews(String genre, int position) {
        mPrimaryMovieGenre.setText(genre);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(
                getActivity(),
                LinearLayoutManager.HORIZONTAL,
                false
        );

        mSecondaryRecyclerView.setLayoutManager(linearLayoutManager);
        mSecondaryRecyclerView.setAdapter(getSecondaryAdapter(position));
    }
}

private class PrimaryAdapter extends RecyclerView.Adapter<PrimaryViewHolder> {
    private String[] mMovieGenre;

    public PrimaryAdapter(String[] moviesGenre) {
        mMovieGenre = moviesGenre;
    }

    @Override
    public PrimaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view = inflater.inflate(R.layout.primary_recycler_view_item, parent, false);
        return new PrimaryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PrimaryViewHolder holder, int position) {
        String genre = mMovieGenre[position];
        holder.bindViews(genre, position);
    }

    @Override
    public int getItemCount() {
        return mMovieGenre.length;
    }
}

private class SecondaryViewHolder extends RecyclerView.ViewHolder {

    private TextView mTextView;

    public SecondaryViewHolder(View view) {
        super(view);
        mTextView = (TextView) itemView.findViewById(R.id.secondary_text_view);
    }

    public void bindView(String name) {
        mTextView.setText(name);
    }
}

private class SecondaryAdapter extends RecyclerView.Adapter<SecondaryViewHolder> {
    private String[] mMovies;

    public SecondaryAdapter(String[] movies) {
        mMovies = movies;
    }

    @Override
    public SecondaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view = inflater.inflate(R.layout.secondary_recycler_view_item, parent, false);
        return new SecondaryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SecondaryViewHolder holder, int position) {
        String name = mMovies[position];
        holder.bindView(name);
    }

    @Override
    public int getItemCount() {
        return mMovies.length;
    }
}

private SecondaryAdapter getSecondaryAdapter(int position) {

    SecondaryAdapter adapter;
    switch (position) {
        case 0:
            return new SecondaryAdapter(mActionMovies);
        case 1:
            return null;
        case 2:
            return null;
        case 3:
            return null;
        case 4:
            return null;
        case 5:
            return null;
        case 6:
            return null;
        case 7:
            return null;
        case 8:
            return null;
        case 9:
            return null;
        default:
            return null;
    }
}
}

Primary_recycler_view.xml

<?xml version="1.0" encoding="utf-8"?>
<Android.support.v7.widget.RecyclerView
xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@+id/primary_recycler_view"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>

primary_recycler_view_item.xml(水平リサイクラビュー付き)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:padding="8dp">

<Android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.Android.com/apk/res-auto"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="2dp">

    <LinearLayout
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:orientation="vertical"
        Android:padding="8dp"
        Android:layout_gravity="bottom">

        <TextView
            Android:id="@+id/primary_movie_genre"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:textSize="20sp"
            Android:paddingTop="16dp"
            Android:paddingBottom="16dp"/>

        <Android.support.v7.widget.RecyclerView
            Android:id="@+id/secondary_recycler_view"
            Android:layout_gravity="center_horizontal"
            Android:layout_width="360dp"
            Android:layout_height="180dp"/>
    </LinearLayout>

</Android.support.v7.widget.CardView>

</LinearLayout>

secondary_recycler_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:padding="8dp"
Android:gravity="center">

<TextView
    Android:id="@+id/secondary_text_view"
    Android:layout_width="120dp"
    Android:layout_height="160dp"
    Android:gravity="center|bottom"
    Android:background="#BDBDBD"/>
</LinearLayout>

single_fragment_activity

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@+id/simple_fragment_container"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>

ご質問がある場合、またはより良い方法を見つけた場合はお知らせください。これがお役に立てば幸いです。

8
Astonvish32