在片段的Lis​​tView Android的图像集片段、图像、tView、Lis

2023-09-08 08:43:56 作者:许你一世倾城

在我的应用程序只需简单的滑动菜单,我可以设置阵​​列适配器,这是正常的工作。但我想设置的图标的ListView 阵列的每一行。在这低于code我的列表视图和多数民众的图像阵列确定,但我不能设置图标。

 公共类NavigationDrawerFragment扩展片段{       。       。       。    公共NavigationDrawerFragment(){    }    @覆盖    公共无效的onCreate(捆绑savedInstanceState){       。       。       。       。    @覆盖    公共无效onActivityCreated(捆绑savedInstanceState){        super.onActivityCreated(savedInstanceState);        //表明该片段想影响的一组操作栏中的行动。        setHasOptionsMenu(真);        。ImageView的IV =(ImageView的)getActivity()findViewById(R.id.icon);    }    @覆盖    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,                             捆绑savedInstanceState){        mDrawerListView =(ListView控件)inflater.inflate(                R.layout.fragment_navigation_drawer,集装箱,FALSE);        mDrawerListView.setOnItemClickListener(新AdapterView.OnItemClickListener(){            @覆盖            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){                选择信息(位置);            }        });        的String []值=新的String [] {                Android的,                iPhone,                黑莓,                WebOS的                Ubuntu的                最大OS X                Linux的,                OS / 2        };    整数[] = imgid新的整数[] {        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(新ArrayAdapter<串GT;(                                getActionBar()。getThemedContext(),                                android.R.layout.simple_list_item_1,                                android.R.id.text1,                                值)                );        mDrawerListView.setItemChecked(mCurrentSelectedPosition,真);        返回mDrawerListView;    }       。       。       。       。} 

XML:

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android              机器人:layout_width =WRAP_CONTENT              机器人:layout_height =WRAP_CONTENT>    < ImageView的            机器人:ID =@ + ID /图标            机器人:layout_width =的22px            机器人:layout_height =的22px            机器人:layout_marginLeft =递四方            机器人:layout_marginRight =10px的            机器人:layout_marginTop =递四方            机器人:SRC =@绘制/ abc_list_ pressed_holo_light>    < / ImageView的>    < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android              机器人:ID =@机器人:ID / text1中              机器人:layout_width =match_parent              机器人:layout_height =WRAP_CONTENT              机器人:textAppearance =机器人:ATTR / textAppearanceListItemSmall              机器人:重力=center_vertical              机器人:paddingStart =机器人:ATTR /列表preferredItemPaddingStart              机器人:paddingEnd =机器人:ATTR /列表preferredItemPaddingEnd              安卓了minHeight =机器人:ATTR /列表preferredItemHeightSmall            />< / LinearLayout中> 

解决方案

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

下面是code

自定义布局 - > item_layout

 < ImageView的    机器人:ID =@ + ID /图标      机器人:layout_width =的22px        机器人:layout_height =的22px        机器人:layout_marginLeft =递四方        机器人:layout_marginRight =10px的        机器人:layout_marginTop =递四方        机器人:SRC =@绘制/ abc_list_ pressed_holo_light>< / ImageView的><的TextView    机器人:ID =@ + ID / text1中          的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android          机器人:ID =@机器人:ID / text1中          机器人:layout_width =match_parent          机器人:layout_height =WRAP_CONTENT          机器人:textAppearance =机器人:ATTR / textAppearanceListItemSmall          机器人:重力=center_vertical          机器人:paddingStart =机器人:ATTR /列表preferredItemPaddingStart          机器人:paddingEnd =机器人:ATTR /列表preferredItemPaddingEnd          安卓了minHeight =机器人:ATTR /列表preferredItemHeightSmall        /> 

现在定制适配器

 类CustomAdapter延伸BaseAdapter{私人LayoutInflater吹气;私人字符串值[];整数[] imgid;私有类ViewHolder {      ImageView的imgView;      TextView中的TextView;   }公共CustomAdapter(上下文的背景下,的String []值,整数[] imgid){    this.values​​ =值;    this.imgid = imgid;    充气= LayoutInflater.from(上下文);}@覆盖公众诠释的getCount(){    返回values​​.length;}@覆盖公共对象的getItem(INT指数){    返回值[指数]}@覆盖众长getItemId(INT为arg0){    返回将arg0;}@覆盖公共查看getView(INT位置,查看convertView,父母的ViewGroup){    ViewHolder支架=无效;    如果(convertView == NULL){        持有人=新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(保持器);    }    支架=(ViewHolder)convertView.getTag();    holder.textView.setText(值[位置]);    holder.imgView.setBackgroundResource(imgid [位置]);    返回convertView;}} 

in simple Slide menu of my application i can set array to Adapter and this is work correctly. but i want to set Icon for each row of ListView array. in this below code my array of listview and images of thats is ok but i can not set icon.

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>

解决方案

You need a custom layout and a custom adapter

Below is the code

Custom layout-> 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"
        />

Now the custom adapter

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;
}

}