我正在开发一个应用程序,我必须使用GridView根据客户要求显示一些图像。
在那个GridView中,我一次显示2行和2列,这样一次就可以看到4个图像。现在用户想要做一些更改,并希望我创建一个 EditText ,在其中可以输入数字,根据该数字,GridView应该更改其列和行。
是否有可能,因为我已经浏览了许多在线示例,我得到的是在运行时更改GridView但使用固定的预定义行数和列数。但我没有找到任何例子,比如在运行时从EditText更改GridView。
和帮助的类型将是可观的。
答案 0 :(得分:1)
根据ueser输入的行数和列数,您可以计算要显示的项目数,将多少项目传递给适配器以及设置列号您可以尝试gridView.setNumColumns(numColumns);
。行数将相应调整。
答案 1 :(得分:1)
正如我在评论中提到的,您可以为gridview使用自定义baseadapter,并在适配器类的构造函数中将图像数量作为参数。下面是您可以用来实现此目的的代码段。
自定义BaseAdapter
public class Adapter extends BaseAdapter {
private final Context context;
private final LayoutInflater inflater;
private final int numbers;
public Adapter(Context context, int numberOfImages) {
this.context = context;
this.numbers = numberOfImages;
this.inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
//You need to return the numberOfImages here for
//the adapter to determine how many view you need to create.
return numbers;
}
private class viewHolder {
ImageView image;
TextView image_desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item, null);
holder = new viewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.image);
holder.image_desc = (TextView) convertView
.findViewById(R.id.img_desc);
convertView.setTag(holder);
}
holder = (viewHolder) convertView.getTag();
if (position % 2 == 0) {
holder.image.setImageResource(R.drawable.ic_action_add_person);
holder.image_desc.setText("change");
}
return convertView;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
您的主要活动:在此活动中,您需要让用户输入他们想要查看的图片数量。当用户没有输入值但我已经捕获了可能的错误并使用它来指定默认值时,我没有添加任何类型的验证或错误消息。您可以根据您的要求在此处应用其他类型的逻辑。
public class MainActivity extends Activity implements OnClickListener {
EditText text;
Button button;
int number;
private static final String NUMBER_OF_IMAGES = "numbers";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
number = Integer.parseInt(text.getText().toString());
// if user enters 0 or negative number then the default value is set
// to 2
if (number < 1) {
number = 2;
}
} catch (Exception e) {
e.printStackTrace();
// if user doesn't enter anything then code will catch
// classCastException and assign 2 as default value here
number = 2;
}
// start this intent by putting this number in the bundle
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(NUMBER_OF_IMAGES, number);
i.putExtras(bundle);
startActivity(i);
}
}
第二个活动:这是您定义和初始化gridview和适配器的活动。确保从先前的活动中检索捆绑包并检索用户正确输入的数字。
public class SecondActivity extends Activity {
GridView grid;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grids);
Bundle bundle = getIntent().getExtras();
int numbers = bundle.getInt(MainActivity.NUMBER_OF_IMAGES);
grid = (GridView) findViewById(R.id.gridview);
// depending upon your requirement you can also add logic here to set
// the number of columns based on the number entered by the user
setColumns(numbers);
adapter = new Adapter(getApplicationContext(), numbers);
grid.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
/**
* Method to set the number of columns depending upon user input for desired
* grids. other attributes for the grid view can also be set her such as
* width, height, background etc.
*
* @param number
*/
public void setColumns(int number) {
if (number > 0 && number < 10) {
grid.setNumColumns(2);
} else if (number >= 10 && number < 20) {
grid.setNumColumns(4);
} else {
grid.setNumColumns(5);
}
}
}
注意:这不是高度优化的性能代码,只是提供了如何实现所需功能的示例。