編輯:關於Android編程
View headerView = getLayoutInflater().inflate(R.layout.listhead, null); Button btn = (Button) headerView.findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(MainActivity.this, bn, Toast.LENGTH_SHORT) .show(); } });
lv.addHeaderView(headerView);
List> list = new ArrayList >(); HashMap map = new HashMap (); map.put(tv, tv); list.add(map); list.add(map); list.add(map); list.add(map); list.add(map); list.add(map);
SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.item_lv, new String[] { tv }, new int[] { R.id.tv });
lv.setAdapter(sa);
lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int position, long id) { //position 如果有headview position =0 的第一個為headview Toast.makeText(MainActivity.this, position: + position, Toast.LENGTH_SHORT).show(); //id:如果有headview或者footview 則這兩個view的id為-1 if (id == -1) { Toast.makeText(MainActivity.this, head: + id, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, id: + id, Toast.LENGTH_SHORT).show(); } } });
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listView1); // 頭部view:設置view的點擊時間,用於屏蔽listview的item的點擊事件 View headerView = getLayoutInflater().inflate(R.layout.listhead, null); Button btn = (Button) headerView.findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(MainActivity.this, bn, Toast.LENGTH_SHORT) .show(); } }); // 添加頭部view:必須放在adapter前面不然會報錯 lv.addHeaderView(headerView); // 假數據 List> list = new ArrayList >(); HashMap map = new HashMap (); map.put(tv, tv); list.add(map); list.add(map); list.add(map); list.add(map); list.add(map); list.add(map); SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.item_lv, new String[] { tv }, new int[] { R.id.tv }); // 設置適配器:如果listview不設置適配器,headview也不會顯示的 lv.setAdapter(sa); // 設置item點擊事件: lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int position, long id) { //position 如果有headview position =0 的第一個為headview Toast.makeText(MainActivity.this, position: + position, Toast.LENGTH_SHORT).show(); //id:如果有headview或者footview 則這兩個view的id為-1 if (id == -1) { Toast.makeText(MainActivity.this, head: + id, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, id: + id, Toast.LENGTH_SHORT).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
本文實例為大家分享了PopupWindow實現自定義overflow的具體代碼,供大家參考,具體內容如下當Action Bar的Action放不下時,系統會將其收集在ov
兩周廢寢忘食的創作終於成功了,現在拿出來分享一下。先不說別的看一下程序運行效果圖,我沒怎麼設計ui所以界面不是很好看但是能說明問題~~~現在我們來看看實現這個功能需要些什
要求根據給定的頭部模板,截取資源圖。背景圖可支持拖動,縮放,拖動旋轉分析定義剪切浮層,實現背景資源圖的操作view實現繪制剪切浮層/** * 初始化繪制筆
關於Android View 事件分發過程的文章網絡上可以搜到一把大,這裡貼一篇代碼性的文章,作者也是個牛人:Android事件分發機制完全解析,帶你從源碼的角度徹底理解