【问题标题】:Android set image on ListView in FragmentAndroid在Fragment中的ListView上设置图像
【发布时间】:2014-08-21 19:10:26
【问题描述】:

在我的应用程序的简单幻灯片菜单中,我可以将数组设置为Adapter,这可以正常工作。但我想为ListView 数组的每一行设置图标。在下面的代码中,我的 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>

【问题讨论】:

  • 您需要为 listView 创建一个自定义的 Adapter 并膨胀项目布局。
  • 下面的xml你在哪里用的?
  • @tuxWorld:查看答案

标签: android listview android-fragments android-listview


【解决方案1】:

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

下面是代码

自定义布局-> 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;
}

}

【讨论】:

  • android:id="@+id/text1"android:id="@android:id/text1" 是什么?需要女巫吗?
  • 使用 android:id="@+id/text1",因为在代码中 holder.textView = (TextView)convertView.findViewById(R.id.text1); 同时做同样的事情,使用第一个
  • onCreateView 函数中评论mDrawerListView 后,我正在使用此操作来使用CustomAdapter 这不起作用。请帮我使用它。我的代码:SetCustomListViewItems customListView = new SetCustomListViewItems(context,values,imgid);
  • pastebin.com/Gj4USanw
【解决方案2】:

在其 getView 方法中为您的 Listview 制作一个自定义适配器:

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

drawerItem_Logo.setImageResource(logos[intPosition]);

这是你的图标数组:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2018-07-31
    • 2013-08-30
    • 1970-01-01
    相关资源
    最近更新 更多