在从适配器中选择的项目上更改listview的背景图像

时间:2013-02-22 05:33:51

标签: android

在我的listadapter的getView方法中,当我在列表中选择一个项目时,我希望此选定项目背景图像采用特定图像,列表中的其他项目采用默认背景图像。我实现了一个for循环来做这个,但是当我从行中选择一个项目时,背景图像不会改变..我们可以通过按钮点击更改背景图像吗?我错了,因为我相信我的代码还可以。以下是选中所选项目的部分。

pos = position;

    size = main.items.size()-1;

    holder.row.setOnClickListener(new OnClickListener() {

        @Override

        public void onClick(View v) {

            int x = position + 1;

               for (int i=0;i<size;i++){

                   holder.row.setBackgroundResource(R.drawable.layer_unselected);

                   holder.text.setTextColor(Color.WHITE);

                   if (x==main.items.size()-1){

                       holder.row.setBackgroundResource(R.drawable.layer_selected);

                       holder.text.setTextColor(Color.BLACK);

                   }    

               }

               System.out.println("selected layer: "+ x );

               System.out.println("selected layer pos: "+pos);

               System.out.println("selected layer size: "+size);


               main.selectItem(position+1);

        }
    });

3 个答案:

答案 0 :(得分:1)

替换

holder.row.setBackgroundResource(R.drawable.layer_unselected);
holder.row.setBackgroundResource(R.drawable.layer_selected);

使用

  1. 对于按钮背景

    holder.row.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_unselected));
    holder.row.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_selected));
    
  2. 对于ImageView背景

    holder.row.setImageDrawable(getResources().getDrawable(R.drawable.layer_unselected));
    holder.row.setImageDrawable(getResources().getDrawable(R.drawable.layer_selected));
    

答案 1 :(得分:0)

MyListAdapter

private class MyListAdapter extends BaseAdapter 
{

    private List<HashMap<String,String>> List;
    private LayoutInflater mInflater;
    private int layoutresource;

    public MyListAdapter(Context context,int resource,List<HashMap<String, String>> myList) {
        List = myList;
        mInflater = LayoutInflater.from(context);
        layoutresource = resource;
    }

    @Override
    public int getCount() {
        return List.size();
    }

    @Override
    public Object getItem(int position) {
        return List.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(layoutresource,null);
        }    

        final View rootView = convertView.findViewById(R.id.root);

        Button btnFav = (Button) convertView.findViewById(R.id.btnFav); 
        MyTextView name = (MyTextView) convertView.findViewById(R.id.textName);
        MyTextView variety = (MyTextView) convertView.findViewById(R.id.textVariety);
        MyTextView price = (MyTextView) convertView.findViewById(R.id.textPrice);

        btnFav.setBackgroundDrawable(getResources().getDrawable(R.drawable.remove_fav_button_selector));
        name.setText( List.get(position).get(TAG_NAME));
        variety.setText(List.get(position).get(TAG_VARIETY));
        price.setText(List.get(position).get(TAG_PRICE));

        rootView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                Intent in = new Intent(MainActivity.this, DetailsActivity.class);
                in.putExtra("ID", List.get(position).get(TAG_ID));
                startActivity(in);
            }
        });

        return convertView;
    }
}

答案 2 :(得分:0)

text_color_selector.xml

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

<item android:state_pressed="true" android:color="#ff8800"/>
 <!-- pressed -->
<item android:state_focused="true" android:color="#ff8800"/>
 <!-- focused -->
<item android:color="@color/textColor"/>
<!-- default -->

</selector>

main.xml中

<Button
       android:id="@+id/button1"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:text="Button"
       android:textColor="@drawable/text_color_selector" />