如何在ListView中的每一行添加按钮?

时间:2018-02-01 12:54:07

标签: java android listview

我试图在ListView中的每一行上实现按钮,但我看到了很多主题,但我没有成功将代码添加到我的。这是我的MainActivity:

public class MainActivity extends AppCompatActivity {

private ArrayAdapter<String> itemsAdapter;
private ArrayList<String> items;
private ImageButton formButton;
private ListView lvMain;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    commonFunction();
}

public void commonFunction() {
    lvMain = (ListView) findViewById(R.id.lvMain);
    items = new ArrayList<String>();
    readItems();
    itemsAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            items);
    lvMain.setAdapter(itemsAdapter);
    formButton = (ImageButton) findViewById(R.id.btnPlus);
    formButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setLayoutActivity();
        }
    });
}
}

这是我的activity_main.xml:

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lvMain"
    android:layout_above="@+id/btnPlus" />

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:id="@+id/btnPlus"
    android:layout_alignParentBottom="true"
    app:srcCompat="@mipmap/ic_plus_foreground" />

有人知道怎么做吗?

3 个答案:

答案 0 :(得分:1)

使用自定义行布局文件创建自定义适配器,并在该行文件上添加按钮并将其绑定到List / Recycler视图。所以它会在所有行中膨胀。

在row_list.xml文件中添加以下代码。

<ImageButton
android:layout_width="match_parent"
android:layout_height="65dp"
android:id="@+id/btnPlus"
android:layout_alignParentBottom="true"
app:srcCompat="@mipmap/ic_plus_foreground" />

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

private ArrayList data;
private static LayoutInflater inflater = null;


/*************  CustomAdapter Constructor *****************/
public CustomAdapter(ArrayList d) {

    /********** Take passed values **********/
    data = d;

    /***********  Layout inflater to call external xml layout () ***********/
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

/******** What is the size of Passed Arraylist Size ************/
public int getCount() {

    if (data.size() <= 0)
        return 1;
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder {
    public ImageButton button;

}

/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder;

    if (convertView == null) {

        /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
        vi = inflater.inflate(R.layout.row_list, null);

        /****** View Holder Object to contain tabitem.xml file elements ******/

        holder = new ViewHolder();
        holder.button = (ImageView) vi.findViewById(R.id.btnPlus);

        /************  Set holder with LayoutInflater ************/
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();

    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Button click
        }
    });
    return vi;
}}

希望它能解决问题。

快乐的编码!!

答案 1 :(得分:1)

Crate自定义适配器并将其与ListView绑定。 为适配器中的每一行增加自定义布局。 您可以将按钮放在自定义布局文件中。

public class CustomAdapter extends ArrayAdapter<String> {

private Context context;

private int singleRowLayoutId;

public CustomAdapter(Context context, int singleRowLayoutId, String []titles, String []desc, int []images ) {
    super(context, singleRowLayoutId,titles);
    this.context=context;
    this.singleRowLayoutId=singleRowLayoutId; //custom row layout for list view item
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View row=convertView;
    CustomViewHolder holder=null;
    if(row==null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //view object of single row of list view
        row = inflater.inflate(singleRowLayoutId, parent, false);
        //by using holder, we make sure that findViewById() is not called every time.
        holder=new CustomViewHolder(row);
        row.setTag(holder);
    }
    else
    {
        holder=(CustomViewHolder)row.getTag();
    }

    return row;
}

private class CustomViewHolder {
    private ImageButton mbtn;

    CustomViewHolder(View view)
    {
        mbtn=(ImageButton)view.findViewById(R.id.btn);

        mbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //handle button click here
        }
    });
    }
}

答案 2 :(得分:0)

我认为这就是您所需要的 - 为列表视图中的行创建单独的布局。然后创建一个自定义适配器,您将此布局添加为行布局,然后将此适配器设置为列表视图。以下是带有示例的详细信息:

Android custom Row Item for ListView