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

转载请标明原地址:http://write.blog.csdn.net/postedit/39084159

今天作一个项目,就是做视频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>




                                                资源地址,点击下载......

展开阅读全文

通过按钮点击改变 TextView 的背景颜色

09-22
我想通过点击按钮改变一个 textview 的背景。 例如,如果点击 textview,它的背景变成黄色,直到再次点击颜色又变成默认的背景颜色。 目前 textview 的背景颜色是按下点击按钮时改变,但是释放时又变成默认的背景颜色。 Drawable/selector.xml: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/circle_on" android:state_enabled="true" android:state_pressed="true"/> <item android:drawable="@drawable/circle_on" android:state_enabled="true" android:state_focused="true"/> <item android:drawable="@drawable/circle_off"/> </selector> Drawable/circle_on: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <stroke android:width="2dp" android:color="@color/Gray" > </stroke> <solid android:color="@color/LightBlue" /> </shape> Drawable/circle_off: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <stroke android:width="2dp" android:color="@color/Gray" > </stroke> <solid android:color="@color/WhiteSmoke" /> </shape> TextView: <TextView style="@style/RoundText" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/repeat_selector" android:clickable="true" android:text="Sun" > </TextView> Text Style: <style name="RoundText"> <item name="android:textColor">#555555</item> <item name="android:gravity">center_vertical|center_horizontal</item> <item name="android:textSize">15sp</item> <item name="android:textStyle">bold</item> <item name="android:fontFamily">sans-serif-thin</item> </style> 请问哪里出错了呢?
©️2020 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值