更改ListView的背景 - 奇怪的行为 [英] Change ListView background - strange behaviour

查看:127
本文介绍了更改ListView的背景 - 奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有改变视图的背景在ListView的一个问题。

I have a problem with changing the background of a view in a ListView.

我需要什么:
更改行的背景图像的onClick()

What I need:
Change the background image of a row onClick()

究竟发生了:
背景被改变(选择)pressing如后的第一个条目。但向下滚动第8项之后选择了。 滚动回第一未选择了顶部。第二项将被选中了。 继续滚动,它将继续跳跃...

What actually happens:
The background gets changed (selected) after pressing e.g. the first entry. But after scrolling down the 8th entry is selected too. Scroll back to the top the first isn't selected anymore. The second entry is selected now. Continue scrolling and it continues jumping...

我是什么洞在code:
我有渠道,和的onClick()我切换频道的属性布尔选择 然后我换了背景。 我这样做的的onClick()这就是为什么我不明白为什么它的actuelly发生在其他项目了。 有一件事我告示是:这似乎是唯一的绘图-part因为其中获得选择由它的自我的项目仍具有在选择值在错误

What i'm dong in the Code:
I have channels, and onClick() I toggle an attribute of channel boolean selected and then I change the background. I'm doing this only onClick() thats why I don't get why it's actuelly happening on other entries too. One thing I notices is: It seems to be only the "drawing"-part because the item which get selected "by it self" has still the selected value on false

我觉得好像有什么做的定制ListAdapters getView(...)

I think it seems to have something to do with the reuse of the views in the custom ListAdapters getView(...)

的onClick()在ListActivity中的 code:

Code of onClick() in ListActivity:

@Override
    protected ViewHolder createHolder(View v) {

        // createHolder will be called only as long, as the ListView is not
        // filled

        TextView title = (TextView) v
                .findViewById(R.id.tv_title_channel_list_adapter);
        TextView content = (TextView) v
                .findViewById(R.id.tv_content_channel_list_adapter);

        ImageView icon = (ImageView) v
                .findViewById(R.id.icon_channel_list_adapter);

        if (title == null || content == null || icon == null) {
            Log.e("ERROR on findViewById",
                    "Couldn't find Title, Content or Icon");
        }
        ViewHolder mvh = new MyViewHolder(title, content, icon);

        // We make the views become clickable
        // so, it is not necessary to use the android:clickable attribute in
        // XML

        v.setOnClickListener(new ChannelListAdapter.OnClickListener(mvh) {

            public void onClick(View v, ViewHolder viewHolder) {
                // we toggle the enabled state and also switch the the
                // background
                MyViewHolder mvh = (MyViewHolder) viewHolder;
                Channel ch = (Channel) mvh.data;
                ch.setSelected(!ch.getSelected()); // toggle

                if (ch.getSelected()) {
                    v.setBackgroundResource(R.drawable.row_blue_selected);
                } else {
                    v.setBackgroundResource(R.drawable.row_blue);
                }
                // TESTING
                Log.d("onClick() Channel", "onClick() Channel: "
                        + ch.getTitle() + " selected: " + ch.getSelected());
            }
        });
return mvh;
    }

getView(...)的 code:

@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;

    // When view is not null, we can reuse it directly, there is no need
    // to reinflate it.
    // We only inflate a new View when the view supplied by ListView is
    // null.
    if (view == null) {
        view = mInflater.inflate(mViewId, null);

        // call own implementation
        holder = createHolder(view);

        // TEST
        // we set the holder as tag
        view.setTag(holder);

    } else {
        // get holder back...much faster than inflate
        holder = (ViewHolder) view.getTag();
    }

    // we must update the object's reference
    holder.data = getItem(position);

            // <EDIT SOLUTION>

    if(getItem(position).get_id() == channelList.get(position).get_id()){
        if(getItem(position).getSelected())
        {
            view.setBackgroundResource(R.drawable.row_blue_selected);
        }
        else{
            view.setBackgroundResource(R.drawable.row_blue);
        }
    }

            // </EDIT SOLUTION>

    // call the own implementation
    bindHolder(holder);

    return view;
}

我真的AP preciate任何想法如何解决这个问题! :)

I really would appreciate any idea how to solve this! :)

如果需要了解更多信息,请告诉我。

If more information is needed please tell me.

在此先感谢!

推荐答案

让我告诉你,我使用的每一个的ListView 和适当控制点击code事件,用于改变背景,并进一步做任何

Let me show you the code that I use for every ListView and properly controlling the click event for changing the background and doing anything further

public class Offices extends Activity {

    private ListView listView;

    /* selectedListItem will contain the number of items to be selected.
     * Your list item OnOlickListener will simply change this variable
     * to the position of the clicked item. The Adapter will do the rest 
     * because you need to refresh the ListView.
     */ 
    private int selectedListItem = -1;
    private Handler mHandler = new Handler();
    private Vector<String> data; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.officeslayout);
        data = new Vector<String>();

        // Add data as per your requirement   
        data.add("one");
        data.add("two");
        data.add("three");
        data.add("four");
        data.add("Five");
        data.add("Six");
        data.add("Seven");
        data.add("Eight");
        data.add("Nine");
        data.add("Ten");

        listView = (ListView)findViewById(R.id.ListView01);
        listView.setDivider(null);

        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                selectedListItem = position;
                ((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();

                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // call any new activity here or do any thing you want here         

                    }
               }, 200L);
            }
        });

        listView.setAdapter(new EfficientAdapter(getApplicationContext()));   
    }

    private class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public EfficientAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return data.size();
        }

        public Object getItem(int position) {
            return position;
        }

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

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

            ViewHolder holder;

            if (convertView == null || convertView.getTag() == null) {
                convertView = mInflater.inflate(R.layout.officeslistitemlayout, null);
                holder = new ViewHolder();
                holder.backgroundView = (ImageView) convertView
                    .findViewById(R.id.OfficesBackground);
                holder.officesTitle = (TextView) convertView
                    .findViewById(R.id.OfficesName);

                convertView.setTag(holder);
            } else {
                 holder = (ViewHolder) convertView.getTag();
            }

            if(position == selectedListItem) {  
                 holder.backgroundView.setBackgroundResource(R.drawable.and_gray_bg_listing_selected);
            } else {
                 holder.backgroundView.setBackgroundResource(R.drawable.and_gray_bg_listing);
            }

            holder.officesTitle.setText(data.get(position)); 

            return convertView;
        }
    }

    static class ViewHolder {
        TextView officesTitle;
        ImageView backgroundView;
    }

}


officeslistitemlayout.xml文件就会像下面的添加可绘制,并根据它的设计给你把下面的code在RelativeLayout的


officeslistitemlayout.xml file will be like following add drawable and design it according to you put the following code in RelativeLayout

 <ImageView android:id="@+id/OfficesBackground" android:layout_width="fill_parent"       
            android:layout_height="45dip"
            android:layout_alignParentTop="true"   
            android:background="@drawable/and_gray_bg_listing"
            android:scaleType="fitXY"
            ></ImageView>


         <TextView android:id="@+id/OfficesName" android:layout_width="wrap_content" 
                   android:text="Offices Name" 
                   android:textColor="#000000" android:textStyle="bold"  
                   android:layout_height="wrap_content"
                   android:layout_centerVertical="true" android:layout_marginLeft="5dip"
            ></TextView>

希望这将有助于:)

Hope it will help :)

这篇关于更改ListView的背景 - 奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆