兩種方法動(dòng)態(tài)改變ListView布局
在getView()時(shí),通過判斷選擇加載不同的布局。
點(diǎn)擊某個(gè)item的時(shí)候,變?yōu)閒oucus狀態(tài),其他的items還原。
下面用兩個(gè)方法給item設(shè)置兩個(gè)不同的布局。
//foucus狀態(tài),顯示一個(gè)ImageView ????private?View?addFocusView(int?i)?{ ????????ImageView?iv?=?new?ImageView(mContext); ????????iv.setImageResource(R.drawable.ic_launcher); ????????return?iv; ????} //normal狀態(tài)顯示一個(gè)ImageView和一個(gè)TextView ????private?View?addNormalView(int?i)?{ ????????LinearLayout?layout?=?new?LinearLayout(mContext); ????????layout.setOrientation(LinearLayout.HORIZONTAL); ????????ImageView?iv?=?new?ImageView(mContext); ????????iv.setImageResource(R.drawable.ic); ????????layout.addView(iv,?new?LinearLayout.LayoutParams( ????????????????LinearLayout.LayoutParams.WRAP_CONTENT, ????????????????LinearLayout.LayoutParams.WRAP_CONTENT)); ????????TextView?tv?=?new?TextView(mContext); ????????tv.setText(mData.get(i)); ????????layout.addView(tv,?new?LinearLayout.LayoutParams( ????????????????LinearLayout.LayoutParams.WRAP_CONTENT, ????????????????LinearLayout.LayoutParams.WRAP_CONTENT)); ????????layout.setGravity(Gravity.CENTER); ????????return?layout; ????}
?
FocusListViewAdapter.java
package?sunny.example.ahfourlistviewfocus; 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.TextView; import?java.util.List; public?class?FocusListViewAdapter?extends?BaseAdapter?{ ????private?ListmData; ????private?Context?mContext; ????private?int?mCurrentItem?=?0; ????public?FocusListViewAdapter(Context?context,?Listdata)?{ ????????this.mContext?=?context; ????????this.mData?=?data; ????} ????public?int?getCount()?{ ????????return?mData.size(); ????} ????public?Object?getItem(int?position)?{ ????????return?mData.get(position); ????} ????public?long?getItemId(int?position)?{ ????????return?position; ????} ????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{ ????????LinearLayout?layout?=?new?LinearLayout(mContext); ????????layout.setOrientation(LinearLayout.VERTICAL); ????????if?(mCurrentItem?==?position)?{ ????????????layout.addView(addFocusView(position)); ????????}?else?{ ????????????layout.addView(addNormalView(position)); ????????} ????????return?layout; ????} ????public?void?setCurrentItem(int?currentItem)?{ ????????this.mCurrentItem?=?currentItem; ????} //foucus狀態(tài),顯示一個(gè)ImageView ????private?View?addFocusView(int?i)?{ ????????ImageView?iv?=?new?ImageView(mContext); ????????iv.setImageResource(R.drawable.ic_launcher); ????????return?iv; ????} //normal狀態(tài)顯示一個(gè)ImageView和一個(gè)TextView ????private?View?addNormalView(int?i)?{ ????????LinearLayout?layout?=?new?LinearLayout(mContext); ????????layout.setOrientation(LinearLayout.HORIZONTAL); ????????ImageView?iv?=?new?ImageView(mContext); ????????iv.setImageResource(R.drawable.ic); ????????layout.addView(iv,?new?LinearLayout.LayoutParams( ????????????????LinearLayout.LayoutParams.WRAP_CONTENT, ????????????????LinearLayout.LayoutParams.WRAP_CONTENT)); ????????TextView?tv?=?new?TextView(mContext); ????????tv.setText(mData.get(i)); ????????layout.addView(tv,?new?LinearLayout.LayoutParams( ????????????????LinearLayout.LayoutParams.WRAP_CONTENT, ????????????????LinearLayout.LayoutParams.WRAP_CONTENT)); ????????layout.setGravity(Gravity.CENTER); ????????return?layout; ????} }
?
activity_main.xml
?
MainActivity.java
package?sunny.example.ahfourlistviewfocus; import?android.support.v7.app.ActionBarActivity; import?android.os.Bundle; import?android.view.Menu; import?android.view.MenuItem; import?android.app.Activity; import?android.os.Bundle; import?android.view.View; import?android.widget.AdapterView; import?android.widget.ListView; import?java.util.ArrayList; import?java.util.List; public?class?MainActivity?extends?ActionBarActivity?{ @Override protected?void?onCreate(Bundle?savedInstanceState)?{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ?ListView?listView?=?(ListView)?findViewById(R.id.focus_listView); ????????Listdata?=?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?adapter?=?new?FocusListViewAdapter(this,?data); ?????? ????????/*?public?void?setAdapter?(ListAdapter?adapter)? ???????Sets?the?data?behind?this?ListView.*/ ????????listView.setAdapter(adapter); ????????listView.setOnItemClickListener(new?AdapterView.OnItemClickListener()?{ ????????????@Override ????????????public?void?onItemClick(AdapterView?parent,?View?view, ????????????????????????????????????int?position,?long?id)?{ ????????????????adapter.setCurrentItem(position);/// ????????????????/*void?android.widget.BaseAdapter.notifyDataSetChanged() Notifies?the?attached?observers?that?the?underlying?data?has?been?changed?and?any?View?reflecting?the?data?set?should?refresh?itself. */ //刷新布局 ????????????????adapter.notifyDataSetChanged(); ????????????} ????????}); ????} }
foucus item3的情況