編輯:關於Android編程
public class Bean {
private boolean isChecked;
private String msg = "這是一條測試數據";
public Bean(){
}
public Bean(boolean isCheched) {
this.isChecked = isCheched;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
然後在getView的時候判斷狀態
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if(convertView == null){
convertView = mInfater.inflate(R.layout.list_item, null);
mViewHolder = new ViewHolder(convertView);
convertView.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mTextView.setText(mDatas.get(position).getMsg());
if(mDatas.get(position).isChecked()){//狀態選中
mViewHolder.mCheckBox.setChecked(true);
}else{
mViewHolder.mCheckBox.setChecked(false);
}
return convertView;
}
public class MainActivity extends ActionBarActivity {
private ListView mListView;
private ListAdapter mAdapter;
private List mDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.main_listView);
mDatas = new ArrayList();
for (int i = 0; i < 20; i++) {
if(i == 0){//默認選中第一個
mDatas.add(new Bean(true));
continue;
}
mDatas.add(new Bean(false));
}
mAdapter = new ListAdapter(this, mDatas);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(mOnItemClickListener);
}
private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int position, long arg3) {
for (Bean bean : mDatas) {//全部設為未選中
bean.setChecked(false);
}
mDatas.get(position).setChecked(true);//點擊的設為選中
mAdapter.notifyDataSetChanged();
}
};
}
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <textview android:id="@+id/main_list_item_text" android:layout_width="0dp" android:layout_height="match_parent" android:textcolor="@android:color/black" android:layout_weight="1" android:gravity="center_vertical"> <checkbox android:id="@+id/main_list_item_checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:backgroundtintmode="multiply" android:layout_marginright="10dp" android:clickable="false" android:focusableintouchmode="false" android:focusable="false"> </checkbox></textview></linearlayout>
這種辦法不推薦使用
public class MainActivity extends ActionBarActivity {
private ListView mListView;
private ListAdapter mAdapter;
private List mDatas;
public static int pos;//記錄選中的position
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.main_listView);
mDatas = new ArrayList();
for (int i = 0; i < 20; i++) {
if(i == 0){
mDatas.add(new Bean(true));
continue;
}
mDatas.add(new Bean(false));
}
mAdapter = new ListAdapter(this, mDatas);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(mOnItemClickListener);
}
private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int position, long arg3) {
pos = position;
mAdapter.notifyDataSetChanged();
}
};
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if(convertView == null){
convertView = mInfater.inflate(R.layout.list_item, null);
mViewHolder = new ViewHolder(convertView);
convertView.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mTextView.setText(mDatas.get(position).getMsg());
if(MainActivity.pos == position){//MainActivity的pos是否等於position
mViewHolder.mCheckBox.setChecked(true);
}else{
mViewHolder.mCheckBox.setChecked(false);
}
return convertView;
}
之前網上看了下自定義消息欄,通知欄,了解到了Notification這個控件,發現UC浏覽器等都是這種類型,今天寫個demo實現下,如圖:其中每個按鈕都有不同的功能,代碼
添加LeakCanary依賴包在主模塊app下的build.gradle下添加如下依賴:debugCompile 'com.squar
這裡講一下React Native中的一個組件——ActivityIndicator,這是一個加載指示器,俗稱菊花,很常見的,效果如下所示:imp
前言在日常開發APP 的過程中,隨著業務的擴展,規模的變化。我們的代碼規模也會逐漸變得龐大,每一個類裡的代碼也會逐漸增多。尤其是Activity和Fragment ,由於