码迷,mamicode.com
首页 > 移动开发 > 详细

Android 点击ListView(或GridView)的一个item,使其里面textview变色,点击另一个这个恢复原来颜色

时间:2016-09-22 10:02:39      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

今天作一个项目,就是做视频app,如果电视剧的话有许多剧集,点击一个item,播放不同的剧集,要有点击效果,并且默认是选择第一个。花费了一段时间,自己觉得有点难

度,现在和大家分享一下,下面是效果显示:

技术分享

 

这个类是例子主类:

 

<span style="font-size:14px;">public final class MainActivity extends Activity {
    private GridView gridView;
    private ArrayList< String> numberList=new ArrayList<String>();;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // "mMediaItem.mDirector---------------"+VideoDetailActivity_Phone.mediaItem.mDes);
        gridView = (GridView) findViewById(R.id.video_detail_series_gridview);
        
        for (int j = 0; j < 10; j++) {
            
            numberList.add("" + (j+1));
        }
        final SeriesFragmentDataAdapter seriesAdapter = new SeriesFragmentDataAdapter(this,numberList);
        gridView.setAdapter(seriesAdapter);
        
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
//这句是通知adapter改变选中的position
                    seriesAdapter.clearSelection(position);
//关键是这一句,激情了,它可以让listview改动过的数据重新加载一遍,以达到你想要的效果
                    seriesAdapter.notifyDataSetChanged();
            }
        });
    }

    }

这个类是例子adapter:

public class SeriesFragmentDataAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<String> numberList=new ArrayList<String>();
    private int selectedPosition=0;
    public SeriesFragmentDataAdapter(Context context,
            ArrayList<String> numberList) {
        inflater = LayoutInflater.from(context);
        this.numberList = numberList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return numberList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return numberList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
//这句是把listview的点击position,传递过来
    public void clearSelection(int position) {
        selectedPosition = position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.global_series_item, null);
            holder.textview = (TextView) convertView
                    .findViewById(R.id.video_detail_series_item_text);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        if (numberList.get(position).length() > 0) {
            holder.textview.setText(numberList.get(position));
        }
        //判断点击了哪个item,然后判断,让他的textview变色
        if(selectedPosition==position){
            holder.textview.setTextColor(Color.parseColor("#ff6501"));
                }else{
            holder.textview.setTextColor(Color.parseColor("#404040"));
        }
        return convertView;
    }

     public class ViewHolder {
        public  TextView textview;
    }
}</span>

Android 点击ListView(或GridView)的一个item,使其里面textview变色,点击另一个这个恢复原来颜色

标签:

原文地址:http://www.cnblogs.com/hsbd-zyb/p/5894959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!