有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

expandableList下的java文本视图消失

当ListView的数据列表不多时,可以很好地看到TextView。但当有大量数据且列表填满屏幕时,TextView笔试就会消失。请看下面的图片 enter image description here

enter image description here

下面是代码源代码。 https://github.com/hardcodingJeon/ExpandList_recyclerView

谢谢!

主要活动。java

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Display;
import 安卓.view.View;
import 安卓.widget.ExpandableListView;
import java.util.ArrayList;

public class MainActivity extends Activity {
    private ExpandableListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Display newDisplay = getWindowManager().getDefaultDisplay();
        int width = newDisplay.getWidth();

        ArrayList<GroupItem> DataList = new ArrayList<>();
        listView = (ExpandableListView)findViewById(R.id.mylist);

        for (int i=1;i<25;i++) {
            GroupItem temp = new GroupItem("A"+i);
            temp.childItems.add(new GroupItem.ChildItem("1","2"));
            temp.childItems.add(new GroupItem.ChildItem("2","3"));
            temp.childItems.add(new GroupItem.ChildItem("4","5"));
            DataList.add(temp);
        }



        ExpandAdapter adapter = new ExpandAdapter(getApplicationContext(), R.layout.group_row, R.layout.activity_child, DataList);
        listView.setIndicatorBounds(width - 50, width); 
        listView.setAdapter(adapter);

    }

}

扩展适配器。java

import 安卓.content.Context;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.BaseExpandableListAdapter;
import 安卓.widget.TextView;

import 安卓x.recyclerview.widget.LinearLayoutManager;
import 安卓x.recyclerview.widget.RecyclerView;

import java.util.ArrayList;


public class ExpandAdapter extends BaseExpandableListAdapter {
    private Context context;
    private int groupLayout = 0;
    private int chlidLayout = 0;
    private ArrayList<GroupItem> DataList;
    private LayoutInflater myinf = null;

    public ExpandAdapter(Context context,int groupLay,int chlidLay,ArrayList<GroupItem> DataList){
        this.context = context;
        this.groupLayout = groupLay;
        this.chlidLayout = chlidLay;
        this.DataList = DataList;
        this.myinf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView == null){
            convertView = myinf.inflate(this.groupLayout, parent, false);
        }
        TextView groupName = (TextView)convertView.findViewById(R.id.groupName);
        groupName.setText(DataList.get(groupPosition).groupTitle);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView == null){
            convertView = myinf.inflate(this.chlidLayout, parent, false);
        }
        RecyclerView recyclerView = convertView.findViewById(R.id.recyclerView);
        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(context,DataList.get(groupPosition).childItems);
        LinearLayoutManager mManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(mManager);
        recyclerView.setAdapter(recyclerAdapter);

        return convertView;
    }


    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return DataList.get(groupPosition).childItems.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public GroupItem getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return DataList.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return DataList.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

}

GroupItem。java

import java.util.ArrayList;
public class GroupItem {
    public String groupTitle;
    public ArrayList<ChildItem> childItems = new ArrayList<>();

    public GroupItem(String groupTitle) {
        this.groupTitle = groupTitle;
    }

    public static class ChildItem {
        String beforePrice;
        String afterPrice;

        public ChildItem(String beforePrice, String afterPrice) {
            this.beforePrice = beforePrice;
            this.afterPrice = afterPrice;
        }
    }
}

回收适配器。java

import 安卓.content.Context;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

import 安卓x.annotation.NonNull;
import 安卓x.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class RecyclerAdapter extends RecyclerView.Adapter {

    Context context;
    ArrayList<GroupItem.ChildItem> items;

    public RecyclerAdapter(Context context, ArrayList<GroupItem.ChildItem> items) {
        this.context = context;
        this.items = items;
    }

    //뷰id를 참조함
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View itemView = inflater.inflate(R.layout.child_item,parent,false);  
        VH holder = new VH(itemView);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VH vh = (VH)holder; 

        
        GroupItem.ChildItem item = items.get(position);

       
        vh.beforePrice.setText( item.beforePrice );
        vh.afterPrice.setText( item.afterPrice );
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    class VH extends RecyclerView.ViewHolder{

        TextView beforePrice;
        TextView afterPrice;
        Button btn;

        public VH(@NonNull View itemView) {
            super(itemView);
            beforePrice = itemView.findViewById(R.id.child_item_beforePrice);
            afterPrice = itemView.findViewById(R.id.child_item_afterPrice);
            btn = itemView.findViewById(R.id.btn);

            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, items.get(getLayoutPosition()).beforePrice+"\n"+items.get(getLayoutPosition()).afterPrice, Toast.LENGTH_SHORT).show();
                }
            });

        }
    }
}

主要活动。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:orientation="vertical"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:padding="15dp">

    <ScrollView
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:fillViewport="true">

        <LinearLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="match_parent"
            安卓:orientation="vertical">

            <ExpandableListView
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:indicatorRight="?安卓:attr/expandableListPreferredItemIndicatorRight"
                安卓:id="@+id/mylist" />

            <TextView
                安卓:layout_width="wrap_content"
                安卓:layout_height="wrap_content"
                安卓:text="TEST"
                安卓:textSize="20sp"/>

        </LinearLayout>

    </ScrollView>



</LinearLayout>

孩子的活动。xml

<?xml version="1.0" encoding="utf-8"?>

<安卓x.recyclerview.widget.RecyclerView
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/recyclerView"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"/>

子项。xml

<?xml version="1.0" encoding="utf-8"?>
<安卓x.cardview.widget.CardView
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    app:cardCornerRadius="8dp"
    app:contentPaddingTop="4dp"
    安卓:layout_marginTop="4dp"
    安卓:layout_marginBottom="4dp"
    安卓:layout_marginLeft="5dp"
    安卓:layout_marginRight="5dp">

    <RelativeLayout
        安卓:layout_width="250dp"
        安卓:layout_height="wrap_content"
        安卓:padding="15dp">

        <TextView
            安卓:id="@+id/child_item_title"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="월간유니콘\nPLUS 31"
            安卓:textColor="@color/black"
            安卓:textSize="20sp"
            安卓:layout_centerHorizontal="true"
            安卓:gravity="center"/>

        <TextView
            安卓:id="@+id/child_item_period"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="1 개월간 매일 2회"
            安卓:layout_below="@id/child_item_title"
            安卓:layout_centerHorizontal="true"
            安卓:layout_marginTop="15dp"/>

        <LinearLayout
            安卓:id="@+id/child_item_Linear"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_below="@id/child_item_period"
            安卓:orientation="vertical"
            安卓:layout_marginTop="10dp">

            <TextView
                安卓:layout_width="wrap_content"
                安卓:layout_height="wrap_content"
                安卓:text="정기구독"
                安卓:layout_below="@id/child_item_period"/>

            <!--data-->
            <TextView
                安卓:id="@+id/child_item_beforePrice"
                安卓:layout_width="wrap_content"
                安卓:layout_height="wrap_content"
                安卓:text="62000원"/>

            <!--data-->
            <TextView
                安卓:id="@+id/child_item_afterPrice"
                安卓:layout_width="wrap_content"
                安卓:layout_height="wrap_content"
                安卓:text="29000/월"/>
        </LinearLayout>

        <Button
            安卓:id="@+id/btn"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="구매"
            安卓:layout_toRightOf="@id/child_item_Linear"
            安卓:layout_below="@id/child_item_period"
            安卓:layout_marginLeft="30dp"
            安卓:layout_marginTop="15dp"
            />


    </RelativeLayout>


</安卓x.cardview.widget.CardView>

儿童街。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent">
    <TextView
        安卓:id="@+id/childName"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:textSize="25dip"
        安卓:paddingLeft="40dip"
        />
    <CheckBox
        安卓:textColor="#111111"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="New CheckBox"
        安卓:id="@+id/checkBox" />
</LinearLayout>

分组。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent">
    <TextView 安卓:id="@+id/groupName"
        安卓:layout_width="wrap_content"
        安卓:layout_height="40dp"
        安卓:textSize="20dp"
        安卓:paddingLeft="5px"
        安卓:textColor = "#FFA500"
        />
</LinearLayout>

共 (1) 个答案

  1. # 1 楼答案

    您已将ExpandableListView高度设置为包裹内容。因此,当元素数量增加时,它会将文本视图推到可用屏幕下方。我建议 1.使用ConstraintLayout或RelativeLayout 2.将文本视图粘贴在最底部,然后将可扩展列表视图相对于该视图进行排列

    否则,将可扩展列表视图的高度设置为特定的预定值(不建议),它应该可以正常工作