Android 图像按钮列表视图

Android ImageButton ListView

怎么能做出这样的东西?

?

对于我的应用程序,我想要一些图像按钮列表,如果我按下一个按钮,我想做一些事情。我真的搜索了所有 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>