【发布时间】:2016-09-05 04:36:18
【问题描述】:
我需要创建一个包含 3 列的 RecyclerView,并带有一个按钮,当单击该按钮时,一个自定义视图会添加到 RecyclerView 中。当我单击视图时,它会被删除。例如:我添加了 5 个视图,如果我点击数字 3,数字 3 被破坏,数字 4 e 5 后退一步。我在 GridLayout 中创建了它,但我希望它位于 RecyclerView 中,我知道我需要一个适配器、一个 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,但没有成功。
【问题讨论】:
-
向我们展示您迄今为止所做的尝试。
-
关于我对 RecyclerView 的尝试,我在博客中尝试了指南,但没有成功。 vogella.com/tutorials/AndroidRecyclerView/article.html
标签: java android android-recyclerview