ListView点击Item更换显示内容图片

先上效果图

ListView点击Item更换显示内容图片_第1张图片

布局文件(就是一个listView)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">

    <ListView  android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"/>
</RelativeLayout>

activity中的代码,模拟的假数据设置listView

public class MainActivity extends Activity {

private ListView lv;
private ArrayList<String> list;
private MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initLayout();

}

private void initLayout() {
    if(list == null){
        list = new ArrayList<>();
    }

    for (int i = 0; i < 20; i++) {
        list.add("正常显示.....");
    }

    lv = (ListView) findViewById(R.id.lv);
    adapter = new MyAdapter(list,this);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            adapter.setCurrentPosition(position);
            adapter.notifyDataSetChanged();
        }
    });
}

}

设置Adapter,主要的点击和切换事件都在这里面,这里是通过动态来添加布局内容的

public class MyAdapter extends BaseAdapter {

    int mCurrentPos;
    ArrayList<String> list;
    Context mContext;

    public MyAdapter(ArrayList<String> list,Context context) {
        this.list = list;
        this.mContext = context;
    }

    public void setCurrentPosition(int position){
        this.mCurrentPos = position;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);

        //说明点击的是当前的位置,设置为获取焦点
        if(mCurrentPos == position){
            layout.addView(addFocusView());
        }else{
            layout.addView(addNormalView(position));
        }
        layout.setGravity(Gravity.CENTER);

        return layout;
    }


    /** * 获取焦点的位置 */
    public View addFocusView(){
        ImageView iv = new ImageView(mContext);
        iv.setLayoutParams(new LinearLayout.LayoutParams(150,150));
        iv.setImageResource(R.drawable.focus);
        return iv;
    }


    /** * 正常情况下listView的内容显示 */
    public View addNormalView(int position){
        LinearLayout layout  = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        ImageView iv = new ImageView(mContext);
        iv.setImageResource(R.drawable.nomal);
        layout.addView(iv, new LinearLayout.LayoutParams(150,150));
        TextView tv  = new TextView(mContext);
        tv.setText(list.get(position));
        tv.setTextSize(30);
        layout.addView(tv,new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        layout.setGravity(Gravity.CENTER);
        return layout;
    }
}

项目源码地址:https://github.com/GimiZhang/ListViewFocus.git

你可能感兴趣的:(android,ListView)