Добавить кнопку внизу ExpandableListView? - PullRequest
0 голосов
/ 22 мая 2018

Я делал приложение, я использовал этот ExpandableListView в Fragment Activity.Я хочу добавить кнопку в нижней части ListView.Я пытался, но когда я добавил Button, он не работал должным образом, Button был добавлен в каждый заголовок списка.Мой код приведен ниже.

enter image description here Это текущий снимок экрана

enter image description here Я хочу вот так

Это мой xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".TurbooneFragment"
    android:background="@color/gray_50">

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:id="@+id/turboone_view">
    </ExpandableListView>


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="35dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
            android:id="@+id/turboone_item"
            android:textColor="@android:color/black"/>

    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/blue_500"
            android:textSize="14sp"
            android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
            android:id="@+id/listturbooneheader"/>

    </LinearLayout>

</RelativeLayout>

// Это мой код Java:

    public class TurbooneFragment extends Fragment {

    private TurbooneListAdapter listadapter;
    private ExpandableListView listView;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHash;

    public TurbooneFragment() {
        // Required empty public constructor
    }

    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_turboone, container, false);
        listView = view.findViewById(R.id.turboone_view);
        initData();
        listadapter = new TurbooneListAdapter(this.getContext(),listDataHeader,listHash);
        listView.setAdapter(listadapter);

        return view;
    }

    private void initData(){
        listDataHeader = new ArrayList<>();
        listHash = new HashMap<>();

        listDataHeader.add("first");
        listDataHeader.add("second");
        listDataHeader.add("third");

        List<String> first= new ArrayList<>();
        first.add("blabla");

        List<String> second= new ArrayList<>();
        second.add("bla");

        List<String> third= new ArrayList<>();
        third.add("blabla");
        third.add("blabla");

        listHash.put(listDataHeader.get(0),first);
        listHash.put(listDataHeader.get(1),second);
        listHash.put(listDataHeader.get(2),third);

    }

}

// Мой ListViewAdapter:

    public class TurbooneListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listDataHeader;
    private HashMap<String,List<String >> listHashMap;

    public TurbooneListAdapter (Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap){
        this.context=context;
        this.listDataHeader=listDataHeader;
        this.listHashMap=listHashMap;
    }

    @Override
    public int getGroupCount() {
        return listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return listHashMap.get(listDataHeader.get(i)).size();
    }

    @Override
    public Object getGroup(int i) {
        return listDataHeader.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return listHashMap.get(listDataHeader.get(i)).get(i1); // i= child item, i1= group item
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        String headerTitle = (String)getGroup(i);
        if (view == null){
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.fragment_turboone,null);
        }
        TextView list1header =  (TextView)view.findViewById(R.id.listturbooneheader);
        list1header.setTypeface(null, Typeface.BOLD);
        list1header.setText(headerTitle);
        return view;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        final String childText = (String)getChild(i,i1);
        if (view== null){
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.fragment_turboone,null);
        }
        TextView txtListChild = (TextView) view.findViewById(R.id.turboone_item);
        txtListChild.setTypeface(null, Typeface.NORMAL);
        txtListChild.setText(childText);
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

1 Ответ

0 голосов
/ 22 мая 2018

Вы используете тот же layout (frag_turboone) для ExpandableListView и для каждой строки группы и дочерней строки.Вы должны предоставить различные макеты для каждого из 3 компонентов.Один макет для вашего fragment, который содержит ExpandableListView, другой для вашего группового макета (заголовки) и другой для вашего дочернего представления (строки внутри группы).

Поскольку вы используете один и тот же макет для всех компонентов, любой View, добавленный в этот макет, добавляется также в список, в группы и дочерние элементы.Вот почему вы должны создавать отдельные макеты.

Фрагмент

Итак, ваш макет фрагмента должен быть следующим: fragment_turboone.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 tools:context=".TurbooneFragment"
 android:background="@color/gray_50">

  <Button
    android:id="+@id/buttonId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="your text"/>

  <ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:id="@+id/turboone_view"
    android:layout_above="@id/buttonId">
  </ExpandableListView>
</RelativeLayout>

Представление группы

Затем вам нужно создать другой макет (файл XML внутри layout папки в ресурсах) для представления вашей группы: group_layout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/blue_500"
        android:textSize="14sp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:id="@+id/listturbooneheader"/>
</LinearLayout>

Детский вид

И еще один макет для вашего дочернего вида: child_layout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:id="@+id/turboone_item"
        android:textColor="@android:color/black"/>
</LinearLayout>

Наконец, вы должны правильно раздувать каждый макет.onCreateView() вашего TurbooneFragment является правильным, так как он надувается fragment_turboone.

В вашем адаптере вы должны изменить макет, накачанный для вашей группы и ребенка.

В вашем getGroupView() вашего TurbooneListAdapter вам нужно изменить строку:

view = inflater.inflate(R.layout.fragment_turboone, null);

для

view = inflater.inflate(R.layout.group_layout, null);

А в вашем getChildView() вашего TurbooneListAdapter вы должны изменить строку:

view = inflater.inflate(R.layout.fragment_turboone, null);

для

view = inflater.inflate(R.layout.child_layout, null);

И тогда ваш список должен работать.

...