Android在Fragment中的ListView上设置图像

时间:2014-08-21 19:10:26

标签: android listview android-fragments android-listview

在我的应用程序的简单幻灯片菜单中,我可以将数组设置为Adapter,这是正常的。但我想为ListView数组的每一行设置Icon。在下面的代码我的listview数组和那些图像是好的,但我不能设置图标。

public class NavigationDrawerFragment extends Fragment {
       .
       .
       .

    public NavigationDrawerFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
       .
       .
       .
       .

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Indicate that this fragment would like to influence the set of actions in the action bar.
        setHasOptionsMenu(true);
        ImageView iv = (ImageView) getActivity().findViewById(R.id.icon);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);


        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });
        String[] values = new String[]{
                "Android",
                "iPhone",
                "Blackberry",
                "WebOS",
                "Ubuntu",
                "Max OS X",
                "Linux",
                "OS/2"
        };
    Integer[] imgid = new Integer[]{
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher
    };
        iv.setImageResource(R.drawable.abc_ab_solid_dark_holo);
        mDrawerListView.
                setAdapter(new ArrayAdapter<String>(
                                getActionBar().getThemedContext(),
                                android.R.layout.simple_list_item_1,
                                android.R.id.text1,
                                values)
                );
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }

       .
       .
       .
       .
}

XML:

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

    <ImageView
            android:id="@+id/icon"
            android:layout_width="22px"
            android:layout_height="22px"
            android:layout_marginLeft="4px"
            android:layout_marginRight="10px"
            android:layout_marginTop="4px"
            android:src="@drawable/abc_list_pressed_holo_light">
    </ImageView>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@android:id/text1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceListItemSmall"
              android:gravity="center_vertical"
              android:paddingStart="?android:attr/listPreferredItemPaddingStart"
              android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
              android:minHeight="?android:attr/listPreferredItemHeightSmall"
            />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

为Listview制作自定义适配器&amp;在其getView方法中:

drawerItem_Logo = (ImageView) convertView.findViewById(R.id.icon);

drawerItem_Logo.setImageResource(logos[intPosition]);

这是你的Icon阵列:

private int[] logos = {
            R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5 
        };

答案 1 :(得分:1)

您需要自定义布局和自定义适配器

以下是代码

自定义布局 - &gt;的 item_layout

<ImageView
    android:id="@+id/icon"
      android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/abc_list_pressed_holo_light">
</ImageView>

<TextView
    android:id="@+id/text1"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/text1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceListItemSmall"
          android:gravity="center_vertical"
          android:paddingStart="?android:attr/listPreferredItemPaddingStart"
          android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
          android:minHeight="?android:attr/listPreferredItemHeightSmall"
        />

现在是自定义适配器

class CustomAdapter extends BaseAdapter 
{

private LayoutInflater inflater;
private String values[];
Integer[] imgid;
private class ViewHolder {
      ImageView imgView;
      TextView textView;
   }

public CustomAdapter(Context context,String[]values,Integer[] imgid)
{

    this.values=values;
    this.imgid=imgid;
    inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
    return values.length;
}

@Override
public Object getItem(int index) {
    return values[index];
}

@Override
public long getItemId(int arg0) {
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder = null;
    if(convertView ==null){
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.item_layout, null);
        holder.imgView = (ImageView)convertView.findViewById(R.id.icon);
        holder.textView = (TextView)convertView.findViewById(R.id.text1);
        convertView.setTag(holder);
    }
    holder = (ViewHolder) convertView.getTag();
    holder.textView.setText(values[position]);
    holder.imgView.setBackgroundResource(imgid[position]);

    return convertView;
}

}