我制作了一个ExpandableListView,现在我想在每个组中设置不同的图像。例如,在着装组中,我想要在鞋子上设置一件衣服的照片,一张鞋子的照片等。
这是我的布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ffffff"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
android:paddingStart="40dp"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="39dp"
android:gravity="center_vertical">
<ImageView android:id="@+id/image1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="25dp"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textIsSelectable="true"
android:textColor="#f30f30"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
代码:
package com.example.user.expandablelist;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group, null);
}
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
}
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return parentItems.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
代码:
package com.example.user.expandablelist;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;
import java.util.ArrayList;
public class MainActivity extends ExpandableListActivity {
private ArrayList<String> parentItems = new ArrayList<>();
private ArrayList<Object> childItems = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListView expandableList = getExpandableListView();
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
setGroupParents();
setChildData();
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
public void setGroupParents() {
parentItems.add("Shorts" );
parentItems.add("Trousers");
parentItems.add("Skirts");
parentItems.add("Dresses");
parentItems.add("Shoes");
}
public void setChildData() {
// Shirts
ArrayList<String> child = new ArrayList<>();
child.add("Basic T-Shirts" );
child.add("Printed T-shirts");
child.add("Tops");
child.add("Long-Sleeved Shirts");
childItems.add(child);
// Trousers
child = new ArrayList<>();
child.add("Jeans");
child.add("Casual");
child.add("Business");
child.add("Shorts");
child.add("Leggings");
childItems.add(child);
// Skirts
child = new ArrayList<>();
child.add("Mini");
child.add("Maxi");
child.add("Midi");
childItems.add(child);
// Dresses
child = new ArrayList<>();
child.add("Mini");
child.add("Maxi");
child.add("Midi");
child.add("Weddings & Parties");
childItems.add(child);
// Shoes
child = new ArrayList<>();
child.add("Sneakers");
child.add("High-Heels");
child.add("Wedges");
child.add("Sandals");
childItems.add(child);
}
}
答案 0 :(得分:0)
对自定义行使用线性布局。
此视频详细说明https://youtu.be/AT_FvelNE_Q
Public view getView (int position, View convert, ViewGroup parent){
View customView = [INSERT INFLATER METHOD);
string single =getItem(position);
TextView yourtext =(TextView) customView.findViewById (R.id.text);
ImageView image = (ImageView)customView.findViewById (R.id.image);
Image.setImageResource (R.drawable.picofcat);
yourtext.setText ("ayy lmao");
}