莫道桑榆晚,为霞尚满天。这篇文章主要讲述Android 点击ListView(或GridView)的一个item,使其里面textview变色,点击另一个这个恢复原来颜色相关的知识,希望能为你提供帮助。
今天作一个项目,就是做视频app,如果电视剧的话有许多剧集,点击一个item,播放不同的剧集,要有点击效果,并且默认是选择第一个。花费了一段时间,自己觉得有点难
度,现在和大家分享一下,下面是效果显示:
文章图片
这个类是例子主类:
【Android 点击ListView(或GridView)的一个item,使其里面textview变色,点击另一个这个恢复原来颜色】
< 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 { publicTextView textview; } }< /span>
推荐阅读
- Android ReentrantLock
- Android 多渠道打包方式详解
- Android ButterKnife注入框架
- android FrameLayout详解
- android 学习随笔八(网络)
- android入门——BroadCast
- Java4Android基础学习之包与访问权限
- Android Studio第十二期 - Activity+Fragment模块化管理方式
- Skipping 'Android SDK Tools, revision 24.0.2'; it depends on 'Android SDK Platform-tools