1

如何製作這樣的東西?Android ImageButton ListView

image

對於我的應用,我想要一些圖像按鈕的列表,如果我按下一個按鈕,我想做點什麼。我真的搜索所有的谷歌找到,我已經找到了一些例子,但他們都是複雜的,有更多的細節

任何人都可以建議我一個教程或例如關於如何使一個SIMPLE列表圖像按鈕?

+0

? onListItemClick的listview可以做你的博愛 – MHP

+0

我想要獨家形象的按鈕,因爲這是我的應用程序中的一個要求:(我需要使用它,只是它 –

+0

所以你需要使用listview,每個項目是一個ImageButton,你應該設置onClick對於您的自定義適配器中的每個ImageButton,在getView方法 – MHP

回答

4

那麼一個簡單的方法來創建一個按鈕列表是創建一個適配器。你爲什麼要使用圖片按鈕,你會做什麼列表ButtonListAdapter buttonListAdapter = new ButtonListAdapter(context, List<"of whatever you send in">);.使用然後用列表​​

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>