如何实施section recyclerview

时间:2017-01-27 05:39:32

标签: android

我是Android的新手,我正在创建一个应用程序,我希望它有一个类似udemy或像UI这样的Playstore但我不知道如何实现多个部分,我已经看过各种网页,但我可以似乎有人能帮助我吗?请指出正确的方向或简单的教程,谢谢你的帮助

这样的事情: https://lh3.googleusercontent.com/5WRlb_NPDauO0zluiwbL6JXF8c1G6uVxJwd2I_6hXY87x7Mc6gcbN7KMFtCe75kM0z4=h310 它有基于您的搜索,销售课程,我们推荐等的部分

这就是我所拥有的:

活动main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hasg_.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

/>
</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="200dp"
    android:layout_margin="12dp"
    android:layout_height="300dp">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

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

adapter.java

package com.example.hasg_.myapplication;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by hasg_ on 23/01/2017.
 */
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
    private ArrayList<String> mDataset;

    public MainAdapter(ArrayList<String> mDataset) {
        this.mDataset = mDataset;
    }

    @Override
    public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row, parent, false);
        // set the view's size, margins, paddings and layout parameters

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {

    holder.mTitle.setText(mDataset.get(position));
    }

    @Override
    public int getItemCount() {

        return mDataset.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTitle;
        public ViewHolder(View itemView) {
            super(itemView);

            mTitle= (TextView) itemView.findViewById(R.id.title);
        }
    }
}

MainActivity.java

package com.example.hasg_.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private  RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private RecyclerView.Adapter mAdapter;
    private ArrayList<String> mDataset;

    @Override
    protected void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDataset=new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            mDataset.add("New Title #"+i);


        }
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
            mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter= new MainAdapter(mDataset);
        mRecyclerView.setAdapter(mAdapter);


        }
    }

4 个答案:

答案 0 :(得分:2)

在onCreateViewHolder()内部膨胀不同的XML文件,如下所示:

@Override
public int getItemViewType(int position) {
    return mItems.get(position); //data source 
}

然后覆盖getItemViewType():

\p{L}

答案 1 :(得分:1)

在GitHub上给出的一个很好的例子检查这个Android documentation

答案 2 :(得分:0)

您需要在布局中为要表示的每个数据源创建单独的RecyclerView。如果他们的数据不同(并且可能会),您需要为每个数据创建唯一的适配器和项目布局文件。请确保为其提供不同的ID,并在MainActivity

中正确配置它们

然后,您可以按照activity main.xml中更清晰的部分将它们展开。

答案 3 :(得分:0)

要了解以及如何从头开始构建,请参阅以下链接

http://android-pratap.blogspot.in/2015/12/horizontal-recyclerview-in-vertical.html