Java 如何在ListView中的每一行上添加按钮?
我试图在ListView中的每一行上实现按钮,但我看到了许多主题,并且没有成功地将代码添加到我的主题中。以下是我的主要活动:Java 如何在ListView中的每一行上添加按钮?,java,android,listview,Java,Android,Listview,我试图在ListView中的每一行上实现按钮,但我看到了许多主题,并且没有成功地将代码添加到我的主题中。以下是我的主要活动: public class MainActivity extends AppCompatActivity { private ArrayAdapter<String> itemsAdapter; private ArrayList<String> items; private ImageButton formButton; private List
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();
}
});
}
}
public类MainActivity扩展了AppCompatActivity{
专用阵列适配器项适配器;
私有ArrayList项;
私有图像按钮formButton;
私有列表视图lvMain;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
commonFunction();
}
公共功能(){
lvMain=(ListView)findViewById(R.id.lvMain);
items=newarraylist();
readItems();
itemsAdapter=新阵列适配器(此,
android.R.layout.simple\u list\u item\u 1,
项目);
lvMain.setAdapter(itemsAdapter);
formButton=(ImageButton)findViewById(R.id.btnPlus);
formButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
setLayoutActivity();
}
});
}
}
以下是我的活动_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" />
有人知道怎么做吗?使用自定义行布局文件创建自定义适配器,并在该行文件上添加按钮,然后将其绑定到列表/回收器视图。因此,它将在所有行中膨胀 在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;
}}
公共类CustomAdapter扩展了BaseAdapter{
私有数组列表数据;
专用静态充气机=空;
/*************自定义适配器构造函数*****************/
公共CustomAdapter(ArrayList d){
/**********接受传递的值**********/
数据=d;
/***********用于调用外部xml布局的布局充气器()***********/
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
/********传递的Arraylist大小是多少************/
public int getCount(){
如果(data.size()使用自定义行布局文件创建自定义适配器,并在该行文件上添加按钮,然后将其绑定到列表/回收器视图。因此,它将在所有行中膨胀
在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;
}}
公共类CustomAdapter扩展了BaseAdapter{
私有数组列表数据;
专用静态充气机=空;
/*************自定义适配器构造函数*****************/
公共CustomAdapter(ArrayList d){
/**********接受传递的值**********/
数据=d;
/***********用于调用外部xml布局的布局充气器()***********/
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
/********传递的Arraylist大小是多少************/
public int getCount(){
如果(data.size()我认为这就是您需要的-为列表视图中的行创建一个单独的布局。然后创建一个自定义适配器,将此布局添加为行布局,然后将此适配器设置为列表视图。以下是详细信息和示例:
我认为这就是您需要的-为列表视图中的行创建一个单独的布局。然后创建一个自定义适配器,将此布局添加为行布局,然后将此适配器设置为列表视图。以下是详细信息和示例:
将自定义适配器装入板条箱,并将其与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
}
});
}
}
公共类CustomAdapter扩展了ArrayAdapter{
私人语境;
私有int singleRowLayoutId;
公共CustomAdapter(上下文上下文、int-singleRowLayoutId、字符串[]标题、字符串[]说明、int[]图像){
超级(上下文、单行布局、标题);
this.context=context;
this.singleRowLayoutId=singleRowLayoutId;//列表视图项的自定义行布局
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
视图行=转换视图;
CustomViewHolder=null;
if(行==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
//列表视图的单行的视图对象
行=充气机。充气(singleRowLayoutId,父级,false);
//通过使用holder,我们确保不会每次调用findViewById()。
holder=新的CustomViewHolder(行);
row.setTag(支架);
}
其他的
{
holder=(CustomViewHolder)行。getTag();
}
返回行;
}
私有类CustomViewHolder{
专用图像按钮mbtn;
CustomViewHolder(视图)
{
mbtn=(ImageButton)view.findViewById(R.id.btn);
mbtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//把手按钮点击这里
}
});
}
}
将自定义适配器装入板条箱并与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
}
});
}
}
公共类CustomAdapter扩展了ArrayAdapter{
私人语境;
私有int singleRowLayoutId;
公共CustomAdapter(上下文上下文、int-singleRowLayoutId、字符串[]标题、字符串[]说明、int[]图像){
超级(上下文、单行布局、标题);
this.context=context;
this.singleRowLayoutId=singleRowLayoutId;//列表视图项的自定义行布局
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
视图行=转换视图;
CustomViewHolder=null;
if(行==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
//列表视图的单行的视图对象
行=充气机。充气(singleRowLayoutId,父级,false);
//通过使用holder,我们确保不会每次调用findViewById()。
holder=新的CustomViewHolder(行);
row.setTag(支架);
}
其他的
{
持有人=(C)