gpt4 book ai didi

android - 在 GridView 中动态添加多个按钮

转载 作者:行者123 更新时间:2023-11-30 01:55:55 26 4
gpt4 key购买 nike

谁能告诉我在 Grid-View 项目中动态设置按钮数量的方法。我的意思是,假设我有 grid-view(包括不同产品的类别)的 3X3,当我点击它的第一个项目时,我第一次在 GridView 中获得其子产品的按钮列表,并且每个项目的数量根据子产品的可用性而不同。

GridView 项目中显示的按钮列表可能因每次单击项目而异。我是 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;

}
}

你必须看到我已经将变量位置更改为最终位置。(只是为了更快的响应)如果你不想那样做。您可以创建一个自定义的 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>

关于android - 在 GridView 中动态添加多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245893/

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