Android:RecyclerView表现为GridLayout

时间:2016-04-08 12:33:57

标签: java android android-recyclerview

我需要创建一个包含3列的RecyclerView,其中包含一个按钮,单击该按钮时,自定义视图将添加到RecyclerView中。当我点击视图时,它会被删除。例如:我添加了5个视图,如果我点击数字3,数字3被破坏,数字4 e 5退一步。我在GridLayout中创建了这个,但我希望它在RecyclerView中,我知道我需要一个Adapter,一个Viewholder和一个LayoutManager。那怎么办呢?

这是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的尝试,我尝试了guide in a blog,但它没有用。

1 个答案:

答案 0 :(得分:2)

通常,您应该使用RecyclerView发布您尝试的代码(而不是使用GridView的代码)。这里所说的是让你走向正确方向的一些例子。

样本活动

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());
        }
    }
}

SAMPLE DECORATOR

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
    }
}