如何在RecyclerView中显示十行

时间:2017-10-08 04:52:51

标签: android android-layout android-recyclerview

我希望始终在RecyclerView中显示十行。 RecyclerView没有可配置的设置。一种方法是计算每行的大小。我希望桌子占据屏幕的一半,并且总是在该区域内显示0..10个项目。如果超过10个项目,用户将会scrooll看到这些项目。现在,RecyclerView的默认行为似乎是随着它的增长继续推动所有事情。

2 个答案:

答案 0 :(得分:1)

@Override
public int getItemCount() {
    if(list.size()>10){
     return 10;
 }else{
 return list.size();
}
}

在您的Recycler视图适配器类中,如果数组或列表的大小大于10,则将get item count返回到10

答案 1 :(得分:0)

你的猜测是正确的,它正在动态计算行高,这是一个例子:

您希望显示的数据的简单等级:

public class CustomItem {

    private int counter;

    public CustomItem(int counter) {
        this.counter = counter;
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
}

在列表中显示CustomItem的简单布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/darker_gray">

    <TextView
        android:id="@+id/txvTest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Test"/>

</RelativeLayout>

此类的RecyclerAdapter,您将看到我们以onBindViewHolder方法以编程方式设置行高:

public class CustomItemRecyclerAdapter extends RecyclerView.Adapter<CustomItemRecyclerAdapter.SimpleItemViewHolder> {

    private static int NB_OF_ITEM_TO_DISPLAY = 10;
    private List<CustomItem> customItems;
    private Context context;
    private int rowHeight;

    public class SimpleItemViewHolder extends RecyclerView.ViewHolder {

        TextView txvTest;
        RelativeLayout container;

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

            txvTest = (TextView) itemView.findViewById(R.id.txvTest);
            container = (RelativeLayout) itemView;
        }
    }

    public CustomItemRecyclerAdapter(Context context, List<CustomItem> customItems) {
        this.context = context;
        this.customItems = customItems;
    }

    public void setItemHeight(int parentHeightInPx){
        // Height of a row is just parent height divided by number of row to display
        this.rowHeight = parentHeightInPx / CustomItemRecyclerAdapter.NB_OF_ITEM_TO_DISPLAY;
        // Notify adapter because items will need to be redraw to use newly set height
        this.notifyDataSetChanged();
    }

    @Override
    public SimpleItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.custom_item, parent, false);
        return new SimpleItemViewHolder(itemView);

    }

    @Override
    public void onBindViewHolder(SimpleItemViewHolder holder, int position) {
        CustomItem item = customItems.get(position);

        holder.txvTest.setText("Item " + item.getCounter());
        holder.txvTest.setBackgroundColor(item.getCounter());

        // Row size magic is here!
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, rowHeight);
        holder.container.setLayoutParams(params);
    }

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

}

活动布局,TextView用作占位符占据屏幕的一半,RecyclerView

<?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="match_parent"
              android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:text="TEST"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/a_main_rcv_items"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:layout_weight="1"
        android:scrollbars="vertical"/>

</LinearLayout>

并且Activity代码带有注释以了解其工作原理;):

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private CustomItemRecyclerAdapter adapter;

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

        // Link to layout recycler view
        recyclerView = (RecyclerView) findViewById(R.id.a_main_rcv_items);

        // Create a simple linear layout manager and set it to recyclerview
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        // Create an instance of our custom Adapter
        adapter = new CustomItemRecyclerAdapter(MainActivity.this, generateItems(20));
        // Set this adapter
        recyclerView.setAdapter(adapter);

        // Listen for recycler view layout change event to get its height and then compute and set new rows height
        recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                // Set item height using height of recyclerView
                adapter.setItemHeight(view.getHeight());
                // Need to simulate a scroll to force a redraw of recycler rows with new height... I have not find better for now...
                recyclerView.scrollBy(0,1);
            }
        });
    }

    private List<CustomItem> generateItems(int nb) {
        ArrayList<CustomItem> items = new ArrayList<>(nb);
        for (int i = 1; i < nb + 1; i++) {
            items.add(new CustomItem(i));
        }
        return items;
    }
}