如何将Adapter获取视图添加到另一个父布局中

时间:2017-08-14 15:50:54

标签: android

我使用适配器显示带有flowlayout的自动建议下拉框

例如

<FlowLayout>
    <TextView1>
    <TextView2>
    <TextView3>
</FlowLayout>

但使用适配器看起来像

<FlowLayout>
    <TextView1>
</FlowLayout>
<FlowLayout>
    <TextView2>
</FlowLayout>
<FlowLayout>
    <TextView3>
</FlowLayout>

如何解决这个问题

我的代码如下: JAVA:

public class TagFilterAdapter extends BaseAdapter implements Filterable {

    private static final int MAX_RESULTS = 10;
    private Context mContext;
    private JSONArray tags;
    private float scale;
    private ArrayList<Pair<Integer,String>> resultList = new ArrayList<Pair<Integer, String>>();

    public TagFilterAdapter(Context context) {
        mContext = context;
    }

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

    @Override
    public String getItem(int index) {
//        return resultList.get(index);
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.tag_autosuggest_dropdown, parent, false);
        }
        scale = mContext.getResources().getDisplayMetrics().density;
        Pair<Integer, String> tagData = resultList.get(position);
                CheckedTextView t = (CheckedTextView) convertView.findViewById(R.id.checkListNew);
                t.setText(tagData.second);
        t.setTextColor(ContextCompat.getColor(mContext,R.color.textDark));
        t.setBackgroundResource(R.drawable.selectable);
        t.setChecked(false);
        t.setClickable(true);
        t.setFocusable(true);
        int padding = (int)(10 * scale + 0.5f);
        t.setPadding(padding,padding,padding,padding);
        return convertView;
    }

tag_autosuggest_dropdown.XML:

<?xml version="1.0" encoding="utf-8"?>
<com.nex3z.flowlayout.FlowLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tagsgroup1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:paddingBottom="20dp"
    app:childSpacing="8dp"
    app:childSpacingForLastRow="align"
    app:rowSpacing="8dp">
    <CheckedTextView
        android:id="@+id/checkListNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></CheckedTextView>
    </com.nex3z.flowlayout.FlowLayout>

1 个答案:

答案 0 :(得分:0)

适配器中充气的确切做法==&gt;我制作已经包含FlowLayout的设计版本(将layout -tag_autosuggest_dropdown-转换为对象),因此它将针对每个项目重复 解决方案==&gt; flowlayout将在主布局中,并且将膨胀的项目(CheckedTextView)将在tag_autosuggest_dropdown中:)