我将使用按钮创建gridview作为gridview的最后一项,我想将其用作添加按钮。
例如: 当您单击按钮(项目1)时,添加到gridview和(添加按钮)转到第二项
第1项按钮
Item1 Item2 Item3 Item4 按钮
像顶级风格的东西
答案 0 :(得分:0)
一种可能性是,您可以根据自己的要求设计多个网格项布局。然后在Adapter类的“getView”方法中,您可以检查哪个xml作为视图膨胀。
希望这会帮助你挖掘它。答案 1 :(得分:0)
在这里你得到了我为实现这一点所做的一切。 MainActivity类:
public class MainActivity extends AppCompatActivity {
GridView gridView;
GridAdapter adapter;
List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add("Test " + i);
}
gridView = (GridView) findViewById(R.id.gridView);
adapter = new GridAdapter(this, list);
gridView.setAdapter(adapter);
}
}
main.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"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.stackoverflow.MainActivity">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</GridView>
</RelativeLayout>
现在为gridView
创建一个适配器public class GridAdapter extends BaseAdapter {
List<String> list;
Context context;
public GridAdapter(Context context, List<String> list) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.griditem, parent, false);
}
TextView textView = (TextView) view.findViewById(R.id.textView);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout);
textView.setText(list.get(position));
if (list.size() - 1 == position) {
linearLayout.addView(addButton());
}
return view;
}
private Button addButton() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button button = new Button(context);
button.setLayoutParams(params);
button.setText("Testing");
return button;
}
}
gridItem.xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Testing"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
</LinearLayout>
最主要的想法是获得最后一项并最后添加一个按钮。您可以优化此代码,也可以使用recyclerView。现在,当您向list添加项目时,请调用notifyDataSetChanged(); 希望它会对你有所帮助。