android expandable listview不滚动

问题描述 投票:0回答:4

我正在尝试实现可扩展列表视图。列表展开后,它不再可滚动。标题错位了。

这是我主要活动中lisltview的布局:

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/dashboard_scrollview">
        <ExpandableListView
            android:id="@+id/leaderboard_list_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:transcriptMode="alwaysScroll" />
    </RelativeLayout>

enter image description here

点击列表视图后,它展开但无法滚动:

enter image description here

我想知道为什么标题和第一个元素没有出现在列表视图中。

主要内容:

public void setupLeaderboardListView(){

        leaderboardDetail = ExpandableLeaderboardData.getData();
        leaderboardListView.setStackFromBottom(true);
        leaderboardtitle = new ArrayList<String>(leaderboardDetail.keySet());
        leaderboardAdapter = new LeaderboardAdapter(this, leaderboardtitle, leaderboardDetail);
        leaderboardListView.setAdapter(leaderboardAdapter);
        leaderboardListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        leaderboardtitle.get(groupPosition) + " List Expanded.",
                        Toast.LENGTH_SHORT).show();
            }
        });

        leaderboardListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        leaderboardtitle.get(groupPosition) + " List Collapsed.",
                        Toast.LENGTH_SHORT).show();

            }
        });

        leaderboardListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        leaderboardtitle.get(groupPosition)
                                + " -> "
                                + leaderboardDetail.get(
                                leaderboardtitle.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT
                ).show();
                return false;
            }
        });
    }

列出数据源:

 List<String> cricket = new ArrayList<String>();
        cricket.add("India");
        cricket.add("Pakistan");
        cricket.add("Australia");
        cricket.add("England");
        cricket.add("South Africa");
        cricket.add("India");
        cricket.add("Pakistan");
        cricket.add("Australia");
        cricket.add("England");
        cricket.add("South Africa");

我尝试为可扩展列表设置layout_height = match_parent,但它不起作用

android expandablelistview
4个回答
1
投票

当你的ExpandableListView有宽度和高度的wrap_content时,就会发生这种情况。要修复,请使其与父RelativeLayout相同

<ExpandableListView
        android:id="@+id/leaderboard_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:transcriptMode="alwaysScroll" />

1
投票

首先:ListView已被弃用,它应该已经很久以前已经死了但是,我一直看到帖子!

第二步:转到recyclerView,这里有一些资源可以帮助你:1。很好的介绍:https://developer.android.com/training/material/lists-cards.html 2.我的对话框cheetSheet应用程序(类似于演示应用程序),其中一个对话框使用可扩展的recyclverview库,试一试https://github.com/WithoutCaps/DialogsCheatSheet


0
投票

使用此代码,它工作正常。这是主要的活动布局:

<?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" >


  <ExpandableListView
    android:id="@+id/expandable_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ExpandableListView>

</LinearLayout>

您的父项布局:

<?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" >


<TextView 
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:id="@+id/tv_parent_item"
   android:layout_marginLeft="10dp"
   android:layout_gravity="center_vertical"
   android:textSize="20sp"
    />

</LinearLayout>

您的子项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
  >


  <ImageView 
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:id="@+id/iv_child_item"
   android:background="@drawable/ic_launcher"
    />

<LinearLayout 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:layout_marginLeft="100dp"
       android:layout_marginTop="5dp" >


<TextView 
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:id="@+id/tv_child_item1"
   android:layout_marginLeft="10dp"
   android:textSize="15sp"
   android:text="Test"
    />

<TextView 
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:id="@+id/tv_child_item2"
   android:layout_marginLeft="10dp"
   android:layout_marginTop="10dp"
   android:textSize="15sp"
   android:text="Test"
    />


 </LinearLayout>

自定义适配器类:

import java.util.HashMap;
import java.util.List;

import com.example.expandablelistview.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;






import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;



import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;



public class ExpandableAdapter extends BaseExpandableListAdapter{

private Context context;
private List<String> parentList;
private HashMap<String , List<Songs>> childList;
ImageLoader imageLoader = ImageLoader.getInstance();

DisplayImageOptions options;

public ExpandableAdapter(){

}


public ExpandableAdapter(Context context,List<String> parentList,
        HashMap<String,List<Songs>> childList)
{
    this.context=context;
    this.parentList=parentList;
    this.childList=childList;

    options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.ic_launcher)
    .showImageForEmptyUri(R.drawable.ic_launcher)
    .showImageOnFail(R.drawable.ic_launcher)
    .cacheInMemory(true)
    .cacheOnDisk(true)
    .considerExifParams(true)
    .bitmapConfig(Bitmap.Config.RGB_565)
    .build();
     ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
     .defaultDisplayImageOptions(options)
     .build();
     imageLoader.getInstance().init(config);



}

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

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return this.childList.get(this.parentList.get(groupPosition)).size();

}

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return this.childList.get(this.parentList.get(groupPosition)).get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    String headerTitle = (String) getGroup(groupPosition);

     if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.parent_item, null);



        }
     TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.tv_parent_item);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);




        ExpandableListView eLV = (ExpandableListView) parent;
        eLV.expandGroup(groupPosition);



        return convertView;





}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    Songs childText = (Songs) getChild(groupPosition, childPosition);


    if(convertView==null)
    {
         LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = infalInflater.inflate(R.layout.child_item , null);


    }

    TextView artist = (TextView) convertView.findViewById(R.id.tv_child_item1);
    TextView song = (TextView) convertView.findViewById(R.id.tv_child_item2);
    ImageView image = (ImageView) convertView.findViewById(R.id.iv_child_item);


    artist.setText(childText.getArtist());
    song.setText(childText.getSong());
    String imgUrl=childText.getArtwork();

    imageLoader.displayImage(imgUrl, image);




    return convertView;


}

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



}

对于Main Activity OnCreate()方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    expListView = (ExpandableListView) findViewById(R.id.expandable_listview);

    listParent = new ArrayList<String>();
    listChild = new HashMap<String, List<Songs>>();

    new GetData().execute();

        listAdapter = new ExpandableAdapter(this, listParent, listChild);
        expListView.setAdapter(listAdapter);


        expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {

                Toast.makeText(getApplicationContext(),
                "Group Clicked " + listParent.get(groupPosition),
                Toast.LENGTH_SHORT).show();

                return false;


            }




        });


        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {


            }
        });


        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {



            }
        });


        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getApplicationContext(),
                        listParent.get(groupPosition)
                                + " : "
                                + listChild.get(
                                        listParent.get(groupPosition)).get(
                                        childPosition), Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });


}

-1
投票

在可扩展列表视图中,xml使用了android:nestedScrollingEnabled="true"

© www.soinside.com 2019 - 2024. All rights reserved.