在Grid-view中动态添加多个按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Grid-view中动态添加多个按钮相关的知识,希望对你有一定的参考价值。

任何人都可以告诉我在Grid-View项目中动态设置按钮数量的方法。我的意思是,假设我有3X3的grid-view(由不同产品的类别组成),当我点击它的第一项时,我得到按钮列表其子产品在网格视图中的第一次和数量根据子产品的可用性而不同。

网格视图项目内显示的按钮列表可能因每个项目点击而有所不同。我是android新手,所以任何帮助将不胜感激

这是我的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="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <com.mss.skyjack.custom.views.SkyjackCustomTextview
                android:id="@+id/tv_product"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawablePadding="5dp"
                android:gravity="center"
                android:text="@string/product"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/black_color" />

            <ImageView
                android:id="@+id/img_product"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="60dp"
                android:src="@drawable/ic_launcher" />

            

                <LinearLayout
                    android:id="@+id/lnr_test"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="30dp"
                    android:orientation="vertical"                    android:visibility="gone">
                </LinearLayout>
         
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#000000" />
    </LinearLayout>

    <View
        android:id="@+id/view_right_line"
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="#000000" />

</LinearLayout>

这是我的适配器类: -

public class ProductSelectorAdapter extends BaseAdapter {

	Activity activity;
	List<SelectorTest> listSelector;
	private Button btn;
	List<catagoriesListModal> _categories;

	public ProductSelectorAdapter(Activity activity,
			List<SelectorTest> listSelector) {
		this.activity = activity;
		this.listSelector = listSelector;
	}

	@Override
	public int getCount() {
		return listSelector.size();
	}

	@Override
	public SelectorTest getItem(int position) {
		return listSelector.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		_categories = listSelector.get(position).getCategories();
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.selector_items, parent,
					false);
			viewHolder = new ViewHolder();
			viewHolder.tvProductName = (TextView) convertView
					.findViewById(R.id.tv_product);
			viewHolder.imgView = (ImageView) convertView
					.findViewById(R.id.img_product);
			viewHolder.viewRightLine = (View) convertView
					.findViewById(R.id.view_right_line);
			viewHolder.lnrLayout = (LinearLayout) convertView
					.findViewById(R.id.lnr_test);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (!activity.getResources().getBoolean(R.bool.landscape_only)) {
			if (position % 2 != 0) {
				viewHolder.viewRightLine.setVisibility(View.GONE);
			} else {
				viewHolder.viewRightLine.setVisibility(View.VISIBLE);
			}
		} else {
			if (position % 3 != 0 & position % 3 != 1) {
				viewHolder.viewRightLine.setVisibility(View.GONE);
			} else {
				viewHolder.viewRightLine.setVisibility(View.VISIBLE);
			}
		}

		SelectorTest selectorTest = listSelector.get(position);
		viewHolder.tvProductName.setText(selectorTest.getName());
		viewHolder.imgView.setImageResource(selectorTest.getImage());
		viewHolder.imgView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (activity.getResources().getBoolean(R.bool.landscape_only)) {
					viewHolder.lnrLayout.setVisibility(View.VISIBLE);
					viewHolder.imgView.setVisibility(View.GONE);

					for (int i = 0; i < _categories.size(); i++) {

						btn = new Button(activity);
						btn.setText(_categories.get(i).getName());

						viewHolder.lnrLayout.addView(btn);
						int idx = viewHolder.lnrLayout.indexOfChild(btn);
						btn.setTag(Integer.toString(idx));

						btn.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								String idxStr = (String) v.getTag();
								Toast.makeText(activity, idxStr,
										Toast.LENGTH_SHORT).show();

							}
						});
					}
				} else {
					Session.ChangeFragmentWithBack(new ProductDetailFragment(),
							"ProductDetailFragment", "ProductDetailFragment", 0);
				}

			}
		});

		return convertView;
	}

	static class ViewHolder {
		TextView tvProductName, tvTest;
		ImageView imgView;
		View viewRightLine;
		ListView lstroduct;
		ScrollView scrlLstItems;
		LinearLayout lnrLayout;
		Button btn1, btn2, btn3;
	}

这是我的SelectorTest模型类: -

import java.util.List;



public class SelectorTest {
	int id;
	String name;
	int image;
	List<catagoriesListModal> categories;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getImage() {
		return image;
	}

	public void setImage(int image) {
		this.image = image;
	}

	public List<catagoriesListModal> getCategories() {
		return categories;
	}

	public void setCategories(List<catagoriesListModal> categories) {
		this.categories = categories;
	}

}

这是我的catagoriesListModal类: -

public class catagoriesListModal {
	String name;
	int id;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

谢谢

答案

您的方法是正确的,但您的模型本身并没有考虑类别。将模型修改为下方。

import java.util.List;


public class SelectorTest {
int id;
String name;
int image;
List<String> categories;


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}



public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

public List<String> getCategories() {
    return categories;
}

public void setCategories(List<String> categories) {
    this.categories = categories;
}
}

现在相应地修改适配器类

public class ProductSelectorAdapter extends BaseAdapter {

Activity activity;
List<SelectorTest> listSelector;
private Button btn;

public ProductSelectorAdapter(Activity activity,
                              List<SelectorTest> listSelector) {
    this.activity = activity;
    this.listSelector = listSelector;

}

@Override
public int getCount() {
    return listSelector.size();
}

@Override
public SelectorTest getItem(int position) {
    return listSelector.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.selector_items, parent,
                false);
        viewHolder = new ViewHolder();
        viewHolder.tvProductName = (TextView) convertView
                .findViewById(R.id.tv_product);
        viewHolder.imgView = (ImageView) convertView
                .findViewById(R.id.img_product);
        viewHolder.viewRightLine = (View) convertView
                .findViewById(R.id.view_right_line);
        viewHolder.lnrLayout = (LinearLayout) convertView
                .findViewById(R.id.lnr_test);

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    if (!activity.getResources().getBoolean(R.bool.landscape_only)) {
        if (position % 2 != 0) {
            viewHolder.viewRightLine.setVisibility(View.GONE);
        } else {
            viewHolder.viewRightLine.setVisibility(View.VISIBLE);
        }
    } else {
        if (position % 3 != 0 & position % 3 != 1) {
            viewHolder.viewRightLine.setVisibility(View.GONE);
        } else {
            viewHolder.viewRightLine.setVisibility(View.VISIBLE);
        }
    }

    SelectorTest selectorTest = listSelector.get(position);
    viewHolder.tvProductName.setText(selectorTest.getName());
    viewHolder.imgView.setImageResource(selectorTest.getImage());


    convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (activity.getResources().getBoolean(R.bool.landscape_only)) {

                viewHolder.lnrLayout.setVisibility(View.VISIBLE);
                viewHolder.imgView.setVisibility(View.GONE);
                List<String> _categories = listSelector.get(position).getCategories();
                for (int i = 0; i < _categories.size(); i++) {

                    btn = new Button(activity);
                    btn.setText(_categories.get(i));
                    viewHolder.lnrLayout.addView(btn);
                    int idx = viewHolder.lnrLayout.indexOfChild(btn);
                    btn.setTag(Integer.toString(idx));
                }
                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String idxStr = (String) v.getTag();
                        Toast.makeText(activity, idxStr, Toast.LENGTH_SHORT).show();

                    }
                });

            }
        }
    });

    return convertView;
}

static class ViewHolder {
    TextView tvProductName, tvTest;
    ImageView imgView;
    View viewRightLine;
    ListView lstroduct;
    ScrollView scrlLstItems;
    LinearLayout lnrLayout;

}
}

你必须看到我已经将变量位置更改为final。(只是为了更快的响应),如果你不想这样做。您可以创建一个自定义View.OnclickListener,它将位置作为参数。这样的事情

class MyOwnClickListener implements OnClickListener {
    int position;

    public MyOwnClickListener(int position) {
        this.position = position;
    }

    @Override
    public void onClick(View v) {
....

然后你可以使用convertView

convertView.setOnClickListener(new MyOwnClickListener(position) {

如果有任何疑问,请伸出援手。谢谢

更新

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:fillViewport="true" > 
    <LinearLayout
                android:id="@+id/lnr_test"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginTop="30dp"
                android:orientation="vertical"                        android:visibility="gone">
            </LinearLayout>
   </ScrollView>

以上是关于在Grid-view中动态添加多个按钮的主要内容,如果未能解决你的问题,请参考以下文章

在单个片段事务中添加多个返回堆栈条目

使用片段的android studio中不显示动态按钮

从单个按钮从多个片段中提取数据

ViewPager2 无法动态添加删除片段

moveclips中如何添加sprite

在片段中动态添加文本视图