在getView()时,通过判断选择加载不同的布局。
点击某个item的时候,变为foucus状态,其他的items还原。
下面用两个方法给item设置两个不同的布局。
//foucus状态,显示一个ImageViewprivate View addFocusView(int i) {ImageView iv = new ImageView(mContext);iv.setImageResource(R.drawable.ic_launcher);return iv;}
//normal状态显示一个ImageView和一个TextViewprivate 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 List<String> mData;private Context mContext;private int mCurrentItem = 0;public FocusListViewAdapter(Context context, List<String> data) {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状态,显示一个ImageViewprivate View addFocusView(int i) {ImageView iv = new ImageView(mContext);iv.setImageResource(R.drawable.ic_launcher);return iv;}
//normal状态显示一个ImageView和一个TextViewprivate 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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ListViewandroid:id="@+id/focus_listView"android:layout_width="match_parent"android:layout_height="wrap_content" />
</LinearLayout>
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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView listView = (ListView) findViewById(R.id.focus_listView);List<String> data = new ArrayList<String>();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() {@Overridepublic 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的情况