gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:20:37 24 4
gpt4 key购买 nike

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

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" />

有人知道该怎么做吗?

最佳答案

使用自定义行布局文件创建自定义适配器,并在该行文件上添加按钮并将其绑定(bind)到列表/回收器 View 。所以它会在所有行中膨胀。

在 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" />

自定义适配器.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;
}}

希望能解决问题。

编码愉快!!

关于java - 如何在ListView的每一行添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48563267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com