Android:表现为GridLayout的RecyclerView [英] Android: RecyclerView behaving as a GridLayout

查看:78
本文介绍了Android:表现为GridLayout的RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个具有3列的RecyclerView,并带有一个按钮,单击该按钮时,会将自定义视图添加到RecyclerView中.当我在视图中单击时,它将被删除.例如:我添加了5个视图,如果我单击3号,则3号将被销毁,而4 e 5号将退后一步.我已经在GridLayout中创建了它,但是我希望它位于RecyclerView中,我知道我需要一个适配器,一个Viewholder和一个LayoutManager.那怎么办呢?

I need to create a RecyclerView with 3 columns, with a button that when clicked, a custom view is added into the RecyclerView. And when I click in a view, it gets deleted. for example: I added 5 views, if I click on number 3, number 3 gets destroyed, and number 4 e 5 take one step back. I've created this in a GridLayout, but I want it to be in a RecyclerView, I'm aware that I need an Adapter, a Viewholder and a LayoutManager. So how is this done?

这是使用GridLayout的外观:

This is how it looked with a GridLayout:

public class MainActivity extends AppCompatActivity {
    GridLayout gridLayout;
    static int i;
    static int n = 1000;
    private Button theButton;

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

        gridLayout = (GridLayout)findViewById(R.id.gamehistory);
        Button b = (Button)findViewById(R.id.Button01);

        b.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {

                theButton = new Button(MainActivity.this);
                TextView theText = new TextView(MainActivity.this);
                theText.setGravity(Gravity.CENTER_HORIZONTAL);
                final LinearLayout theLayout = new LinearLayout(MainActivity.this);

                theLayout.setOrientation(LinearLayout.VERTICAL);
                theLayout.setBackgroundColor(Color.parseColor("#8BAAC3"));
                theLayout.setId(++i);
                theButton.setId(++n);
                theButton.setText(theButton.getId() + "");
                theText.setText(theLayout.getId() + "");
                theLayout.addView(theButton);
                theLayout.addView(theText);
                gridLayout.addView(theLayout);
                GridLayout.LayoutParams lp = (GridLayout.LayoutParams) theLayout.getLayoutParams();
                lp.setMargins(10, 10, 10, 10);

                theButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        gridLayout.removeView(theLayout);
                    }
                });
            }
        });
    }
}

关于我对RecyclerView的尝试,我已经尝试了博客中的指南但是没有用.

Regarding my attempts with a RecyclerView I've tried a guide in a blog but it didn't work.

推荐答案

通常,您应该发布使用RecyclerView尝试的代码(而不是GridView的代码).话虽如此,这里有一些例子可以帮助您朝正确的方向前进.

Typically you should post what code you attempted with the RecyclerView (not the code with the GridView). That being said here are some examples to get you going in the right direction.

示例权限

public class MainActivity extends AppCompatActivity {

    private static final int NUMBER_COLUMNS = 3;

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

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(this, NUMBER_COLUMNS));
        recyclerView.addItemDecoration(new SampleItemDecoration());
        final MyAdapter adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);

        // Items can be added to adapter from any part of code
        adapter.addItem("Static Item A");
        adapter.addItem("Static Item B");


        findViewById(R.id.button_add_item).setOnClickListener(new View.OnClickListener() {
            int i;

            @Override
            public void onClick(View v) {
                adapter.addItem("Dynamic Item " + i++);
            }
        });
    }
}

示例适配器

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<String> items = new ArrayList<>();

    public void addItem(String name) {
        items.add(name);
        notifyItemInserted(items.size() - 1);
    }

    public void removeItem(int position) {
        items.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.grid_item_button, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setButtonName(items.get(position));
    }

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

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private Button button;

        public ViewHolder(View itemView) {
            super(itemView);
            button = (Button) itemView.findViewById(R.id.grid_button);
            button.setOnClickListener(this);
        }


        public void setButtonName(String buttonName) {
            button.setText(buttonName);
        }

        @Override
        public void onClick(View v) {
            removeItem(getAdapterPosition());
        }
    }
}

样品装饰器

class SampleItemDecoration extends RecyclerView.ItemDecoration {

    Paint paint = new Paint();

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        for (int i = 0; i < parent.getChildCount(); i++) {
            View view = parent.getChildAt(i);
            paint.setColor(Color.parseColor("#8BAAC3"));
            if (parent.getChildLayoutPosition(view) == RecyclerView.NO_POSITION) {
                continue;
            }

            // Compute bounds of cell in layout
            Rect bounds = new Rect(
                    layoutManager.getDecoratedLeft(view),
                    layoutManager.getDecoratedTop(view),
                    layoutManager.getDecoratedRight(view),
                    layoutManager.getDecoratedBottom(view)
            );

            // Add space between cell backgrounds
            bounds.inset(2, 2);

            c.drawRect(bounds, paint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.set(10, 10, 10, 10); // Specify spacing between items in grid
    }
}

这篇关于Android:表现为GridLayout的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆