滚动视图内的两列RecyclerView

时间:2015-10-26 09:48:51

标签: android scrollview android-recyclerview

我希望我的2列RecyclerView,即使用GridLayoutManager,在scrollview中。我发现了一种方法可以将单列recyclelerview从这个link放入scrollview中,但是我无法弄清楚如何将网格RecyclerView放在scrollview中。

1 个答案:

答案 0 :(得分:6)

我找到了解决方案。适合我的工作。

我的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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_grid_recycler_view"
    tools:context="com.example.recyclerview_horizontal.GridRecyclerView">

    <ScrollView
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_height="match_parent">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Gridview inside scrollview"
                android:layout_marginBottom="50dp"/>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Gridview inside scrollview"
                />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

<强> MainActivity

package com.example.gridrecyclerviewscroll;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements RecyclerAdapter.DynamicHeight {

    RecyclerView recyclerView;
    RecyclerAdapter adapter;
    ArrayList<Integer> list;
    ArrayList<Integer> size;
    TextView txt;
    long sumHeight=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        RecyclerView.LayoutManager lm = new GridLayoutManager(this,2);

       // txt = (TextView) findViewById(R.id.txt1);
        recyclerView.setLayoutManager(lm);

        size = new ArrayList<>();
        list = new ArrayList<>();
        list.add(R.drawable.index1);
        list.add(R.drawable.index2);
        list.add(R.drawable.index3);
        list.add(R.drawable.index4);
        list.add(R.drawable.index5);
        list.add(R.drawable.index6);
        list.add(R.drawable.index8);
        list.add(R.drawable.index9);
        list.add(R.drawable.index10);

        adapter = new RecyclerAdapter(list,this);
        recyclerView.setAdapter(adapter);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    @Override
    public void HeightChange(int position, int height) {
        size.add(height);
        sumHeight = calSum();

        float density = this.getResources().getDisplayMetrics().density;
        float viewHeight = sumHeight * density;
        recyclerView.getLayoutParams().height = (int) sumHeight;
    }

    private long calSum() {
        long tot=0;
        for(int i=0;i<size.size();i++){
            tot = tot + size.get(i);
        }
        return tot;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

<强> RecyclerViewAdapter

package com.example.gridrecyclerviewscroll;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by nteam on 23/10/15.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private ArrayList<Integer> list;
    Context context;
    private DynamicHeight dynamicHeight;
    public RecyclerAdapter(ArrayList<Integer> list, DynamicHeight dynamicHeight){
        this.list = list;
        this.dynamicHeight = dynamicHeight;
    }
    public RecyclerAdapter(ArrayList<Integer> list){
        this.list = list;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_card,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.img.setImageResource(list.get(position));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        public ViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img_thumbnail);
        }
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        holder.itemView.post(new Runnable() {
            @Override
            public void run() {
                int cellwidth = holder.itemView.getWidth();
                int cellheight = holder.itemView.getHeight();

                if(dynamicHeight!=null) dynamicHeight.HeightChange(position, cellheight);
            }
        });
    }

    public interface DynamicHeight {
        void HeightChange(int position, int height);
    }
}

所有积分都转到@Ashkan