GridView具有动态数量的TextViews

时间:2018-12-20 19:53:52

标签: android android-layout gridview android-gridview

我正在尝试为每个项目创建一个动态数量为GridView的{​​{1}}。

这是我要做什么的模型:

TextView

请注意,|-----------------| | Text | Text | | | Text | |-----------------| | Text | Text | | Text | | | Text | | |-----------------| | Text | Text | | Text | Text | |-----------------| 中的每个GridView都有一个动态编号,具体取决于从我的API返回的数据。

我的问题是,如何修改下面的代码,为要在TextView项目中添加的每个文本添加一个TextView

这是我用来调用适配器的代码(已删除的无关代码):

GridView

这是我的private GridView gridView; // Create a map to hold the text for each GridView item // The Integer is simply the section number private List<Map<Integer, List<String>>> items = new ArrayList<>(); // Get the GridView view gridView = findViewById(R.id.grid_view); // Add sample items to the list Map<Integer, List<String>> map = new HashMap<>(); map.put(1, Collections.singletonList("Item 1")); Map<Integer, List<String>> map2 = new HashMap<>(); map2.put(2, Arrays.asList("Item 1", "Item 2")); Map<Integer, List<String>> map3 = new HashMap<>(); map3.put(3, Arrays.asList("Item 1", "Item 2", "Item 3")); Map<Integer, List<String>> map4 = new HashMap<>(); map4.put(4, Collections.singletonList("Item 1")); // Add the items to the list items.add(map); items.add(map2); items.add(map3); items.add(map4); // Initialize the adapter GridViewAdapter gridViewAdapter = new GridViewAdapter(getContext(), items); gridView.setAdapter(gridViewAdapter); 适配器:

GridView

这是我的public class GridViewAdapter extends BaseAdapter { private final Context mContext; private final List<Map<Integer, List<String>>> items; private LayoutInflater layoutInflater; public GridViewAdapter(Context context, List<Map<Integer, List<String>>> items) { this.mContext = context; this.items = items; layoutInflater = LayoutInflater.from(mContext); } @Override public int getCount() { return items.size(); } @Override public long getItemId(int position) { return 0; } @Override public Object getItem(int position) { return null; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; Map<Integer, List<String>> item = items.get(position); if (convertView == null) { convertView = layoutInflater.inflate(R.layout.gridview_item, parent, false); viewHolder = new ViewHolder(); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } for (Map.Entry<Integer, List<String>> entry : item.entrySet()) { Integer key = entry.getKey(); List<String> values = entry.getValue(); // For each value, inflate a TextView layout... for (String value : values) { // What goes here? // How do I inflate the TextView layout (shown below) for each value and add it to the parent view? } } return convertView; } static class ViewHolder { // What goes here? } } 布局:

gridview_item

最后,这是我要填充并添加到上述布局中的<?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="match_parent" android:orientation="vertical"> </LinearLayout> 布局:

gridview_textview

我将如何实现这一目标,代码将是什么样?

2 个答案:

答案 0 :(得分:1)

我建议将RecyclerViewGridLayoutManager一起使用,而不是GridView

在任何一种情况下,您都希望ViewHolder执行以下操作(这对我来说很难模拟代码,因此我将编写步骤:

  • 您的ViewHolder将使gridview_item布局膨胀并获得对LinearLayout的引用。
  • 您将在Viewholder上拥有一个像bindStrings(List<String> strings)这样的方法,该方法将为每个视图添加一个textview。

代码如下:

public void bindStrings(List<String> strings) {
   Context context = myItemView.getContext();
   LinearLayout parentLayout = (LinearLayout) myItemView;

   for(String item : strings) {
      TextView textView = TextView(context);
      textView.setText(item);
      parentLayout.addView(textView);
   }
}

答案 1 :(得分:0)

// For each value, inflate a TextView layout...
for (String value : values) {
    // What goes here?
    // How do I inflate the TextView layout (shown below) for each value and add it to the parent view?
}

由于要回收视图,因此必须确保以下几点是正确的:

  • 您有足够个视图来显示所有字符串
  • 对于该单元格中的字符串数,您没有太多个视图

我建议,当LinearLayout的视图过多时,您只需隐藏它们(而不是删除它们)。这将增加您的应用程序的内存消耗,但也将改善整体性能(因为您将花费更少的时间来充实视图)。您可以稍后再访问。

因此,我们需要确保对您的字符串具有足够的视图。让我们先这样做:

LinearLayout linear = (LinearLayout) convertView;

while (linear.getChildCount() < values.size()) {
    inflater.inflate(R.layout.gridview_textview, linear);
}

现在让我们填充每个TextView:

for (int i = 0; i < values.size(); ++i) {
    String text = values.get(i);
    TextView textView = (TextView) linear.getChildAt(i);
    textView.setText(text);
    textView.setVisibility(View.VISIBLE);
}

让我们隐藏所有其他视图:

for (int i = values.size(); i < linear.getChildCount(); ++i) {
    View v = linear.getChildAt(i);
    v.setVisibility(View.GONE);
}

所有代码在一起:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.gridview_item, parent, false);
    }

    LinearLayout linear = (LinearLayout) convertView;

    Map<Integer, List<String>> item = items.get(position);

    for (Map.Entry<Integer, List<String>> entry : item.entrySet()) {
        Integer key = entry.getKey();
        List<String> values = entry.getValue();

        for (String value : values) {
            while (linear.getChildCount() < values.size()) {
                inflater.inflate(R.layout.gridview_textview, linear);
            }

            for (int i = 0; i < values.size(); ++i) {
                String text = values.get(i);
                TextView textView = (TextView) linear.getChildAt(i);
                textView.setText(text);
                textView.setVisibility(View.VISIBLE);
            }

            for (int i = values.size(); i < linear.getChildCount(); ++i) {
                View v = linear.getChildAt(i);
                v.setVisibility(View.GONE);
            }
        }
    }

    return convertView;
}