android可扩展列表视图未滚动
我正在尝试实现可扩展列表视图。当列表展开时,它将不再可滚动。头球放错位置了 以下是我的主要活动中ListView的布局:android可扩展列表视图未滚动,android,expandablelistview,Android,Expandablelistview,我正在尝试实现可扩展列表视图。当列表展开时,它将不再可滚动。头球放错位置了 以下是我的主要活动中ListView的布局: <RelativeLayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_below="@+id/dashboard_scrollview"> <ExpandableList
<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>
点击列表视图后,它会展开,但无法滚动:
我想知道为什么标题和第一个元素没有显示在列表视图中
主要活动:
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;
}
});
}
public void设置排行榜列表视图(){
LeadboardDetail=ExpandableLeadboardData.getData();
排行榜列表视图。setStackFromBottom(真);
LeadboardTitle=新的ArrayList(leadboardDetail.keySet());
leaderboardAdapter=新的leaderboardAdapter(此,leaderboardtitle,leaderboardDetail);
排行榜ListView.setAdapter(排行榜适配器);
排行榜ListView.setOnGroupExpandListener(新的ExpandableListView.OnGroupExpandListener(){
@凌驾
public void onGroupExpand(int groupPosition){
Toast.makeText(getApplicationContext(),
排行榜标题。获取(groupPosition)+“列表已展开”,
吐司。长度(短)。show();
}
});
排行榜ListView.setOnGroupCollapseListener(新的ExpandableListView.OnGroupCollapseListener(){
@凌驾
公共void-onGroupCollapse(int-groupPosition){
Toast.makeText(getApplicationContext(),
排行榜标题。获取(groupPosition)+“列表已折叠。”,
吐司。长度(短)。show();
}
});
排行榜ListView.setOnChildClickListener(新的ExpandableListView.OnChildClickListener(){
@凌驾
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
Toast.makeText(
getApplicationContext(),
排行榜标题。获取(groupPosition)
+ " -> "
+排行榜详情(
排行榜标题。获取(groupPosition))。获取(
childPosition)、Toast.LENGTH\u SHORT
).show();
返回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");
List cricket=new ArrayList();
添加(“印度”);
添加(“巴基斯坦”);
添加(“澳大利亚”);
板球。添加(“英格兰”);
添加(“南非”);
添加(“印度”);
添加(“巴基斯坦”);
添加(“澳大利亚”);
板球。添加(“英格兰”);
添加(“南非”);
我尝试为可展开列表设置layout_height=match_parent,但它不起作用当您的
ExpandableListView
具有宽度和高度的wrap_内容时,可能会发生这种情况。若要修复,请将其与父级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" />
首先:ListView已经过时了,它早就应该死了,但是,我一直在上面看到帖子
第二:
转到recyclerView,以下是一些有助于您的资源:
1.很好的介绍:
2.我的dialog cheetSheet应用程序(类似于演示应用程序),其中一个对话框使用可扩展的可回收Verview库,请尝试一下使用此代码,它工作正常。
这是主要的活动布局:
<?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;
}
}
import java.util.HashMap;
导入java.util.List;
导入com.example.expandablelistview.R;
导入com.nostra13.universalimageloader.core.DisplayImageOptions;
导入com.nostra13.universalimageloader.core.ImageLoader;
导入com.nostra13.universialimageloader.core.imageloader配置;
导入com.nostra13.universalimageloader.core.listener.simplemageloadinglistener;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.Typeface;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.ExpandableListAdapter;
导入android.widget.ExpandableListView;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类ExpandableAdapter扩展了BaseExpandableListAdapter{
私人语境;
私人名单父母名单;
私有HashMap子列表;
ImageLoader ImageLoader=ImageLoader.getInstance();
显示图像选项;
公共可扩展适配器(){
}
公共可扩展适配器(上下文、列表父列表、,
HashMap子列表)
{
this.context=context;
this.parentList=parentList;
this.childList=childList;
选项=新建DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_启动器)
.showImageForEmptyUri(R.drawable.ic_发射器)
.showImageOnFail(R.drawable.ic_启动器)
.cacheInMemory(真)
.cacheOnDisk(真)
.considerExifParams(真)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoaderConfiguration配置=新建ImageLoaderConfiguration.Builder(上下文)
.defaultDisplayImageOptions(选项)
.build();
imageLoader.getInstance().init(配置);
}
@凌驾
public int getGroupCount(){
//TODO自动生成的方法存根
返回此.parentList.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
//TODO自动生成的方法存根
返回this.childList.get(this.parentList.get(groupPosition)).size();
}
@凌驾
公共对象getGroup(int-groupPosition){
//TODO自动生成的方法存根
返回此.parentList.get(groupPosition);
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
//TODO自动生成的方法存根
返回此.childList。
@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;
}
});
}