怎样做这样的事情?
?
对于我的应用程序,我想要一些图像按钮列表,如果我按下一个按钮,我想做点什么。我已经在所有Google上进行了真正的搜索,并找到了一些示例,但是所有示例都很复杂,因此有更多详细信息
谁能建议我一个有关如何制作一张简单的图像按钮列表的教程或示例?
最佳答案
创建按钮列表的一种简单方法是创建适配器。您可以通过执行列表ButtonListAdapter buttonListAdapter = new ButtonListAdapter(context, List<"of whatever you send in">);.
然后使用列表list.setAdapter(buttonListAdapter);
来使用它
class ButtonListAdapater extends BaseAdapter
{
Context mContext;
private LayoutInflater mInflater;
List<"whatever you want to pass in such as images or textfiels or classes"> mDatas;
public ButtonListAdapater (Context context, List<"whatever you want to pass in such as images or textfiels or classes"> results)
{
mContext = context;
mDatas = results;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount()
{
return mDatas.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)
{
ViewHolder holder;
"whatever you want to pass in such as images or textfiels or classes" data = mDatas.get(position);
if (convertView == null) {
"whatever layout you made, on click effect can be in layout on in code"
convertView = mInflater.inflate(R.layout.data_list_item, null);
holder = new ViewHolder();
holder.tvButton = (TextView) convertView.findViewById(R.id.textViewButton);
holder.lbButton = (ImageButton) convertView.findViewById(R.id.Button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvButton.setText(data.getData());
"With the button here it depends on what you want to do and how, since its perfectly fine to put an onclicklistener here or in the layout"
holder.llButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, OTherActivity.class);
startActivity(intent)
}
});
return convertView;
}
static class ViewHolder
{
TextView tvButton;
ImageButton lbButton;
}
而且data_list_item布局xml可以很简单,例如
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/R.id.Button/>
</LinearLayout>