赞
踩
这是我的第一篇博文,如有什么不妥的地方请大家多多包涵,好了,进入今天的主题,前段时间有个朋友问了我一个效果,该效果的具体实现是点击Listview,然后Listview中的item项的背景颜色和item项中字体的颜色的变化,即点击item改变他们的颜色并记录下来.好了,表达能力可能需要提高现在我先给大家截一个最终效果图:
效果图如上.
具体代码如下:
- <span style="font-size:24px;">package leoli.com.focuslistview;
-
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class MainActivity extends AppCompatActivity {
- private ListView mListView;
- private List<String> list=new ArrayList<String>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mListView= (ListView) this.findViewById(R.id.listview);
- for (int i=0;i<10;i++){
- list.add("你好"+i);
- }
- final MyAdapter adapter=new MyAdapter();
- mListView.setAdapter(adapter);
-
-
- mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
- adapter.setCurrentItem(i);
- adapter.setClick(true);
- adapter.notifyDataSetChanged();
- }
- });
- }
-
- class MyAdapter extends BaseAdapter{
- private int mCurrentItem=0;
- private boolean isClick=false;
-
- @Override
- public int getCount() {
- return list.size();
- }
-
- @Override
- public Object getItem(int i) {
- return list.get(i);
- }
-
- @Override
- public long getItemId(int i) {
- return i;
- }
-
- @Override
- public View getView(int i, View view, ViewGroup viewGroup) {
- if (view==null){
- view= LayoutInflater.from(MainActivity.this).inflate(R.layout.listview_item,null);
- }
-
- TextView textView= (TextView) view.findViewById(R.id.test);
- LinearLayout parent= (LinearLayout) view.findViewById(R.id.ll_parent);
-
- textView.setText(list.get(i));
-
- if (mCurrentItem==i&&isClick){
- parent.setBackgroundColor(Color.parseColor("#3F51B5"));
- textView.setTextColor(Color.parseColor("#ff6600"));
- }else{
- parent.setBackgroundColor(Color.parseColor("#ffffff"));
- textView.setTextColor(Color.parseColor("#000000"));
- }
-
- return view;
- }
-
-
-
- public void setCurrentItem(int currentItem){
- this.mCurrentItem=currentItem;
- }
-
- public void setClick(boolean click){
- this.isClick=click;
- }
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。