像 Paytm 一样的水平 RecyclerView

Horizontal RecyclerView Like Paytm

我必须实现自定义水平 RecyclerView,在顶部有一个 header(标题),在 RecyclerView 的末尾右侧有一个部分(查看全部)。

我创建了一个带有 header 和页脚的 RecyclerView,但我想要一个右侧部分(查看全部),我希望在其中触发一些事件。

在Paytm App中实现

我希望得到如下结果

这会给你灵感

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><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="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp">

<android.support.v7.widget.RecyclerView
    android:id="@+id/verticalScrollRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.v7.widget.RecyclerView></RelativeLayout>

vertical_scroll_single_entry.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:weightSum="1"
android:gravity="center_vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

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

<Button
    android:id="@+id/selectAllButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="See All >>"
    android:textAllCaps="false"/></LinearLayout>

自定义适配器class 垂直滚动条

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {


private Context context;
private ArrayList arrayList;
private LayoutInflater layoutInflater;

public CustomAdapter(Context context, ArrayList arrayList) {
    this.context = context;
    this.layoutInflater = LayoutInflater.from(context);
    this.arrayList = arrayList;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.vertical_scroll_single_entry, parent, false);
    return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    //initialise values to views inside holder at runtime
    holder.recyclerView.setAdapter(new CustomAdapterTwo(context, arrayList));
    holder.recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
    holder.recyclerView.setHasFixedSize(true);
}

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

class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    RecyclerView recyclerView;
    Button selectAllButton;

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

        recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);

        selectAllButton = (Button) itemView.findViewById(R.id.selectAllButton);
        selectAllButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Toast.makeText(context, "Select All At : " + String.valueOf(getLayoutPosition()), Toast.LENGTH_SHORT).show();
    }
}}

水平适配器单项文件recycler_view_single_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="match_parent" android:gravity="center_horizontal">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Price"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Remove this button \n and put image view"
    android:textAllCaps="false"/></LinearLayout>

水平回收器视图适配器class

public class CustomAdapterTwo extends RecyclerView.Adapter<CustomAdapterTwo.CustomViewHolder> {
private Context context;
private ArrayList arrayList;
private LayoutInflater layoutInflater;

public CustomAdapterTwo(Context context, ArrayList arrayList) {
    this.context = context;
    this.arrayList = arrayList;
    this.layoutInflater = LayoutInflater.from(context);
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.recycler_view_single_item, parent, false);

    return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {



}

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

class CustomViewHolder extends RecyclerView.ViewHolder {

    public CustomViewHolder(View itemView) {
        super(itemView);
    }
}}

你的主要activityclass

public class MainActivity extends AppCompatActivity {
private RecyclerView verticalScrollRecyclerView;

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

    initialiseView();

}

private void initialiseView() {

    verticalScrollRecyclerView = (RecyclerView) findViewById(R.id.verticalScrollRecyclerView);

    ArrayList<String> stringArrayList = new ArrayList<>();

    stringArrayList.add("One");
    stringArrayList.add("Two");
    stringArrayList.add("Three");
    stringArrayList.add("Four");
    stringArrayList.add("Five");
    stringArrayList.add("Six");
    stringArrayList.add("Seven");
    stringArrayList.add("Eight");
    stringArrayList.add("Nine");
    stringArrayList.add("Ten");



    //setting adapter and layout manager to recyclerView
    verticalScrollRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    verticalScrollRecyclerView.setAdapter(new CustomAdapter(this, stringArrayList));
    verticalScrollRecyclerView.setHasFixedSize(true);

}}

看起来像