多个recyclerviews?

时间:2017-12-06 14:21:25

标签: android android-recyclerview nestedrecyclerview

我想通过recyclerview将我的主页设为两行 与书籍数据和显示独立的事件我如何实现它我想在我的代码中添加事件我该怎么办?

MoviesAdapter.java

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {

private List<Movie> moviesList;
public Context context;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title;
    public ImageView thumbnail;


    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        thumbnail = (ImageView) view.findViewById(R.id.book);

    }
}//End of MyViewHolder class

public MoviesAdapter(List<Movie> moviesList) {
    this.moviesList = moviesList;
}

//display different items in the data set
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.movie_list_row, parent, false);

    return new MyViewHolder(itemView);
}

//display data at specified location
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    holder.title.setText(moviesList.get(position).getTitle());

    holder.thumbnail.setImageResource(moviesList.get(position).getImage());
}

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

我想让我的网页像这样

enter image description here

2 个答案:

答案 0 :(得分:2)

使用带有3个RecyclerViews的LinearLayout并为每个都创建一个适配器应该没问题。嵌套的RecyclerViews只有在你有很多类别时才有意义。

答案 1 :(得分:0)

我使用三个recyclerview完成它,它们的适配器类是MainActivity.java和activity.xml的代码

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private List<Book> bookList = new ArrayList<>();
    private List<Author> authorList = new ArrayList<>();
    private List<Event> eventList = new ArrayList<>();

    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;
    private RecyclerView recyclerView3;

    private BookAdapter bAdapter;
    private AuthorAdapter aAdapter;
    private EventAdapter eAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        recyclerView1 =  findViewById(R.id.recycler_book);
        bAdapter = new BookAdapter(this,bookList);
        recyclerView1.setHasFixedSize(true);
         RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView1.setLayoutManager(mLayoutManager);
        recyclerView1.setAdapter(bAdapter);
        prepareBookData();

      //second recycler
        recyclerView2 =  findViewById(R.id.recycler_author);
        aAdapter = new AuthorAdapter(this,authorList);
        recyclerView2.setHasFixedSize(true);
        RecyclerView.LayoutManager aLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView2.setLayoutManager(aLayoutManager);
        recyclerView2.setAdapter(aAdapter);
        prepareAuthorData();

        //third recycler
        recyclerView3 = (RecyclerView) findViewById(R.id.recycler_event);
        eAdapter = new EventAdapter(this,eventList);
        recyclerView3.setHasFixedSize(true);
        RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
        //   RecyclerView.LayoutManager eLayoutManager = new GridLayoutManager(this,1,GridLayoutManager.HORIZONTAL,false);
        recyclerView3.setLayoutManager(eLayoutManager);
        recyclerView3.setAdapter(eAdapter);
        prepareEventData();
    }

    private void prepareBookData(){

        int[] drawableArray = {R.drawable.youcanwin, R.drawable.halfgirl};
        String[] nameArray = {"You Can Win", "Half Girlfriend"};
        Book a=new Book(nameArray[0],drawableArray[0]);
        bookList.add(a);

        Book b=new Book(nameArray[1],drawableArray[1]);
        bookList.add(b);

        Book c=new Book(nameArray[0],drawableArray[0]);
        bookList.add(c);

        bAdapter.notifyDataSetChanged();
    }

    private void prepareAuthorData(){

        int[] drawableArray = {R.drawable.youcanwin, R.drawable.halfgirl};
        String[] nameArray = {"You Can Win", "Half Girlfriend"};

        Author a=new Author(nameArray[0],drawableArray[0]);
        authorList.add(a);

        Author b=new Author(nameArray[1],drawableArray[1]);
        authorList.add(b);

        Author c=new Author(nameArray[0],drawableArray[0]);
        authorList.add(c);

        Author d=new Author(nameArray[1],drawableArray[1]);
        authorList.add(d);

        bAdapter.notifyDataSetChanged();
    }

    private void prepareEventData(){

        String[] desArray = {"new","old"};
        String[] nameArray = {"You Can Win", "Half Girlfriend"};

        Event a=new Event(nameArray[0],desArray[0]);
        eventList.add(a);

        Event b=new Event(nameArray[0],desArray[0]);
        eventList.add(b);

        Event c=new Event(nameArray[0],desArray[0]);
        eventList.add(c);

        bAdapter.notifyDataSetChanged();
    }
 }

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="com.example.hardik.threerecycler.MainActivity"
android:background="#e6edec">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        app:cardCornerRadius="4dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Books"
            android:textSize="20sp"
            android:textStyle="bold" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_book"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginTop="20dp" />
    </android.support.v7.widget.CardView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Authors"
        android:textStyle="bold"
        android:textSize="20sp"
        android:layout_marginTop="10dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_author"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@+id/recycler_book"
        android:layout_marginTop="5dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Events"
        android:textStyle="bold"
        android:textSize="20sp"
        android:layout_marginTop="10dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_event"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@+id/recycler_author"
        android:layout_marginTop="5dp"/>

</LinearLayout>