之前一直没有更新,原因很简单,懒!
最近在做网络书城部分,发现很多待解决的问题。今天模仿某app的排行榜界面就碰到一个问题。先看看排行榜界面怎么实现的把
public class ExpandablelistAdapter extends BaseExpandableListAdapter {
private Context mContext;
private LayoutInflater inflater;
//設置組視圖的圖片
private int[] groupImags = new int[]{R.drawable.rank_hot,R.drawable.rank_leaving,R.drawable.rank_end,R.drawable.rank_monthly,R.drawable.rank_potential,R.drawable.rank_collapse};
//设置组视图的显示文字
private String[] groupTexts = new String[]{"追书最热榜","读者留存率top 100","追书完结榜","包月排行榜","本周潜力榜","别人家的排行榜"};
private String[][] childTexts = new String[][]{{},{},{},{},{},{"圣诞热搜榜","百度热搜榜","掌阅热销榜","书旗热销榜","17K鲜花榜"}};
public ExpandablelistAdapter(Context context){
mContext=context;
inflater=LayoutInflater.from(context);
}
public ExpandablelistAdapter(){
}
@Override
public int getGroupCount() {
return groupImags.length;
}
@Override
public int getChildrenCount(int i) {
return childTexts[i].length;
}
@Override
public Object getGroup(int i) {
return groupTexts[i];
}
@Override
public Object getChild(int i, int i1) {
return childTexts[i][i1];
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.list_item, null);
ImageView iv1 = (ImageView) convertView.findViewById(R.id.image1);
iv1.setImageResource(groupImags[groupPosition]);
TextView textView1 = (TextView) convertView.findViewById(R.id.text1);
textView1.setText(getGroup(groupPosition).toString());
//箭頭
ImageView iv2 = (ImageView) convertView.findViewById(R.id.image2);
if (groupPosition==5) {//第六个(别人家的排行榜)
if (isExpanded) {
iv2.setImageResource(R.drawable.up);
} else {
iv2.setImageResource(R.drawable.down);
}
} else {
iv2.setVisibility(View.GONE);
}
/* convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,"组视图: "+groupPosition,Toast.LENGTH_SHORT).show();
}
});*/
return convertView;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
convertView = inflater.inflate(R.layout.list_item1,null);
TextView tvName = (TextView)convertView.findViewById(R.id.tvRankChildName);
tvName.setText(getChild(groupPosition,childPosition).toString());
/* convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,"子视图: "+childPosition,Toast.LENGTH_SHORT).show();
}
});*/
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
setContentView(R.layout.activity_main);
final ExpandableListView MaleListView = (ExpandableListView) findViewById(R.id.expandableMaleListView);
final ExpandableListView FeMaleListView = (ExpandableListView) findViewById(R.id.expandableFeMaleListView);
//不顯示默認的
MaleListView.setGroupIndicator(null);
FeMaleListView.setGroupIndicator(null);
ExpandablelistAdapter adapter1 = new ExpandablelistAdapter(this);
MaleListView.setAdapter(adapter1);
FeMaleListView.setAdapter(adapter1);
public class CustomExpandableListView extends ExpandableListView {
public CustomExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
/**
* 重寫該方法,達到expandablelistview適應scrollView的效果
*/
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
替换实例(简单爆了 )
然后数据成功的得到了,当然这只是一个小Demo,以后图片、文字都应该从网络中获取,最后的效果图如下(很像吧 )