如何设置一个背景上点击一个列表视图项,动态地更改其他的点击 [英] How to set a background to a listview item on click and change dynamically on other clicks

查看:113
本文介绍了如何设置一个背景上点击一个列表视图项,动态地更改其他的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个列表视图并点击我要显示一个背景图像选中的列表项,并更改背景,如果选择改变。我一直在试图实现这一点,但没能做到这一点。请帮我这一点,将不胜感激,这样

请找到code我使用

  @覆盖
    公共查看getView(最终诠释的立场,观点convertView,
            ViewGroup中父){
        查看查看= convertView;
        最终诠释getstartedItemPos =位置;
        资源RES = getResources();

        如果(convertView == NULL){
            鉴于= LayoutInflater.from(parent.getContext())。膨胀(
                    R.layout.getting_starteditem,NULL);
        }
        同步(视图){
            TextView的textTopic =(TextView中)查看
                    .findViewById(R.id.indexItems);
            textTopic.setText(getStartedItems [getstartedItemPos]);
            textTopic.setTypeface(TF);
            view.setBackgroundColor(Color.TRANSPARENT);

        }
        返回查看;
    }

};

私人OnItemClickListener getStartedListItem =新OnItemClickListener(){

    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,
            长ARG3){
        // TODO自动生成方法存根
         意图myintent =新的意图(getApplicationContext(),GetStartedWebview.class);
         myintent.putExtra(的SelectedItem,getStartedItems [位置]);
         startActivity(myintent);
    }
};
 

解决方案

我无法从你的code看看你是否已经连接了 OnItemClickListener 您的ListView ,你应该做的,如果你还没有。在 onItemClick ,你可以抓住被点击(ARG1)的视图,并使用 .setBackgroundResource(INT渣油)就可以了改变它的背景

编辑::您可以使用保存当前列表项显示背景的局部变量,所以做这样的事情:

 私人查看pressedView = NULL;

私人OnItemClickListener getStartedListItem =新OnItemClickListener(){

    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,
            长ARG3){
        // TODO自动生成方法存根
         意图myintent =新的意图(getApplicationContext(),GetStartedWebview.class);
         myintent.putExtra(的SelectedItem,getStartedItems [位置]);
         startActivity(myintent);

         如果(pressedView!= NULL){
             pressedView.setBackgroundResource(..); //重新设置老项目背景
             pressedView = ARG1; //点​​pressedView新项目
         }
    }
};
 

hi i am having a list view and on click i want to show a background image to the selected list item, and change the background if the selection is changed. i had been trying to implement this but was not able to do so. please help me with this and would be grateful so

please find the code i am using

 @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View view = convertView;
        final int getstartedItemPos = position;
        Resources res = getResources();

        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.getting_starteditem, null);
        }
        synchronized (view) {
            TextView textTopic = (TextView) view
                    .findViewById(R.id.indexItems);
            textTopic.setText(getStartedItems[getstartedItemPos]);
            textTopic.setTypeface(tf);
            view.setBackgroundColor(Color.TRANSPARENT);

        }
        return view;
    }

};

private OnItemClickListener getStartedListItem = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub
         Intent myintent = new  Intent(getApplicationContext(),GetStartedWebview.class);
         myintent.putExtra("SelectedItem", getStartedItems[position]);
         startActivity(myintent);
    }
};

解决方案

I can't see from your code if you have attached the OnItemClickListener to your ListView, you should do that if you haven't. In onItemClick you can grab the View that is clicked (arg1) and use .setBackgroundResource(int resID) on it to change it's background

EDIT: You can use a local variable that holds the current List item showing the background, so do something like:

private View pressedView = null; 

private OnItemClickListener getStartedListItem = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub
         Intent myintent = new  Intent(getApplicationContext(),GetStartedWebview.class);
         myintent.putExtra("SelectedItem", getStartedItems[position]);
         startActivity(myintent);

         if(pressedView != null) {
             pressedView.setBackgroundResource(..); // reset background of old item
             pressedView = arg1; // Point pressedView to new item
         }
    }
};

这篇关于如何设置一个背景上点击一个列表视图项,动态地更改其他的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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