自定义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; } }