两种方法动态改变ListView布局
扫描二维码
随时随地手机看文章
在getView()时,通过判断选择加载不同的布局。
点击某个item的时候,变为foucus状态,其他的items还原。
下面用两个方法给item设置两个不同的布局。
//foucus状态,显示一个ImageView private View addFocusView(int i) { ImageView iv = new ImageView(mContext); iv.setImageResource(R.drawable.ic_launcher); return iv; } //normal状态显示一个ImageView和一个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状态,显示一个ImageView private View addFocusView(int i) { ImageView iv = new ImageView(mContext); iv.setImageResource(R.drawable.ic_launcher); return iv; } //normal状态显示一个ImageView和一个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的情况