如何添加在文本旁边的图像在一个 arrayList Android ?

发布时间: 2016/6/29 21:30:03
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
 private void initDrawerList() {
    // initialize drawer content
    // need to determine selected item according to the currently selected sensor type
    drawerItemList = new ArrayList();

    drawerItemList.add(new DrawerItem("*1st text", R.drawable.my_sensz_normal, R.drawable.my_sensz_selected, true));
    drawerItemList.add(new DrawerItem("*2nd text", R.drawable.friends_normal, R.drawable.friends_selected, false));
    drawerItemList.add(new DrawerItem("*3rd text", R.drawable.friends_normal, R.drawable.friends_selected, false));

    drawerAdapter = new DrawerAdapter(HomeActivity.this, drawerItemList);
    drawerListView = (ListView) findViewById(R.id.drawer);

    if (drawerListView != null)
        drawerListView.setAdapter(drawerAdapter);

    drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

我想要替换 * 的形象,但我不知道如何去做。有人能帮我吗?谢谢你 !:)

解决方法 1:

起初你创建 layout 文件 navigation_row.xml 的自定义行

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="55dp"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="10dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Icon"
        android:src="@drawable/order_64" />

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Example application"
        android:textColor="@color/whiteColor"
        android:paddingLeft="10dp"
        android:textSize="20sp" />

</LinearLayout>

在你 activity 类使两个列表

int [] icon = {R.drawable.img1,
                   R.drawable.img2,
                   R.drawable.img3,
                   R.drawable.img4,
                   R.drawable.img5};

    String [] drawerTitle ={"1st text",
            "2 text",
            "3 text",
            "4 text",                               
            "5 text"};

创建 Adapter

public class NavigationAdapter extends ArrayAdapter <String> {
private final Context context;
private final String[] titles;
private final int[] icon;

public NavigationAdapter(Context context, int[] icon, String[] titles){
    super(context, R.layout.navigation_row,titles);
    this.context=context;
    this.icon=icon;
    this.titles=titles;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.navigation_row,parent,false);

    ImageView imageView=(ImageView)rowView.findViewById(R.id.icon);
    TextView textView = (TextView)rowView.findViewById(R.id.name);

    imageView.setImageResource(icon[position]);
    textView.setText(titles[position]);     

    return rowView;
}   

}

和现在设置此 adapter 反对你listview

 NavigationAdapter onAdapter=new NavigationAdapter(Navigation.this,icon,drawerTitle);
drawerListView.setAdapter(onAdapter);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
官方微信
官方QQ群
31647020