简体   繁体   English

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

[英]How to add button on each row in ListView?

I'm trying to implement button on each row in ListView, but I saw many topics and I don't succeeded to add code to mine. 我正在尝试在ListView的每一行上实现按钮,但是我看到了很多主题,但没有成功向我的代码添加代码。 Here is my MainActivity : 这是我的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();
        }
    });
}
}

Here is my activity_main.xml : 这是我的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" />

Does someone any idea how to do ? 有人知道怎么做吗?

Create custom adapter with custom row layout file and add button on that row file and bind it to List/Recycler view. 使用自定义行布局文件创建自定义适配器,然后在该行文件上添加按钮,并将其绑定到“列表/回收器”视图。 So it will inflate in all row. 因此它将在所有行中膨胀。

Add below code in row_list.xml file. 在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 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;
}}

Hope it will solve problem. 希望它能解决问题。

Happy coding!! 编码愉快!!

Crate custom adapter and bind it with the ListView. 创建自定义适配器并将其与ListView绑定。 Inflate custom layout for each row in the adapter. 为适配器中的每一行填充自定义布局。 You can put your button in the custom layout file. 您可以将按钮放在自定义布局文件中。

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
        }
    });
    }
}

I think that this is what you need - create a separate layout for the rows in the list view. 我认为这就是您需要的-为列表视图中的行创建单独的布局。 Then create a custom adapter, where you are adding this layout as row layout and then set this adapter to your list view. 然后创建一个自定义适配器,在其中将此布局添加为行布局,然后将该适配器设置为列表视图。 Here are the details with examples: 以下是带有示例的详细信息:

Android custom Row Item for ListView Android的ListView自定义行项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM