代码库/点击可变换item View的ListView/FocusListView

自定义ListViewAdapter

    重写getView方法,判断item位置,变换对应的View

    在MainActivity item点击事件中调用notifyDataSetChanged变动view


MainAc

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView= (ListView) findViewById(R.id.listview);
    List<String> data=new ArrayList<>();
    data.add("I am item 1");
    data.add("I am item 2");
    data.add("I am item 3");
    data.add("I am item 4");
    data.add("I am item 5");
    final FocusListViewAdapter focusListViewAdapter=new FocusListViewAdapter(this,data);
    listView.setAdapter(focusListViewAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            focusListViewAdapter.setCurrentItem(position);
            focusListViewAdapter.notifyDataSetChanged();
        }
    });
}

FocusListViewAdapter

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by YRC on 2017/10/20.
 */

public class FocusListViewAdapter extends BaseAdapter{
    private int mCurrentItem=0;
    private Context mContext;
    private List<String> mData;
    private ImageView iv;


    public FocusListViewAdapter(Context context, List<String> Data) {
        this.mContext=context;
        this.mData=Data;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout linearLayout=new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        if (mCurrentItem==position){
            linearLayout.addView(addFocusView(position));

        }else {
            linearLayout.addView(addNormalView(position));
        }
        return linearLayout;
    }

    public void setCurrentItem(int currentItem) {
        this.mCurrentItem = currentItem;
    }

    private View addFocusView(int i){
        iv=new ImageView(mContext);
        iv.setImageResource(R.mipmap.ic_launcher);
        return iv;
    }


    private View addNormalView(int i){
        LinearLayout linearLayout=new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        iv=new ImageView(mContext);
        iv.setImageResource(R.mipmap.in_icon);
        linearLayout.addView(iv, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        TextView tv=new TextView(mContext);
        tv.setText(mData.get(i));
        linearLayout.addView(tv, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.setGravity(Gravity.CENTER);
        return linearLayout;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值