-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mahao
committed
May 17, 2016
1 parent
191dcb0
commit 39912ed
Showing
10 changed files
with
237 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
include ':app', ':systemwidgetdemo', ':customviewdemo', ':utils', ':animationdemo', ':designpattern', ':arithmetic' | ||
include ':app', ':systemwidgetdemo', ':customviewdemo', ':utils', ':animationdemo', ':designpattern', ':arithmetic', ':architecture' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
systemwidgetdemo/src/main/java/com/mahao/alex/systemwidgetdemo/listView/BaseAppAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.mahao.alex.systemwidgetdemo.listView; | ||
|
||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by Alex_MaHao on 2016/5/17. | ||
*/ | ||
public abstract class BaseAppAdapter<T> extends BaseAdapter { | ||
|
||
/** | ||
* 泛型,保存数据 | ||
*/ | ||
protected List<T> datas; | ||
|
||
/** | ||
* 构造方法,子类必须实现其构造方法,并初始化数据 | ||
* @param datas | ||
*/ | ||
public BaseAppAdapter(List<T> datas) { | ||
this.datas = datas; | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return datas==null?0:datas.size(); | ||
} | ||
|
||
@Override | ||
public Object getItem(int position) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
|
||
BaseViewHolder vHolder; | ||
|
||
if(convertView==null){ | ||
convertView = LayoutInflater.from(parent.getContext()).inflate(getItemLayoutId(),parent,false); | ||
|
||
vHolder = new BaseViewHolder(convertView); | ||
|
||
convertView.setTag(vHolder); | ||
}else{ | ||
vHolder = (BaseViewHolder) convertView.getTag(); | ||
} | ||
|
||
/** | ||
* 数据绑定的回调 | ||
*/ | ||
bindData(vHolder,datas.get(position)); | ||
|
||
return convertView; | ||
} | ||
|
||
/** | ||
* 子类实现,获取item布局文件的id | ||
* @return | ||
*/ | ||
protected abstract int getItemLayoutId(); | ||
|
||
/** | ||
* 子类实现,绑定数据 | ||
* @param vHolder 对应position的ViewHolder | ||
* @param data 对应的数据绑定 | ||
*/ | ||
protected abstract void bindData(BaseViewHolder vHolder,T data); | ||
|
||
|
||
/** | ||
* ViewHolder类 | ||
*/ | ||
class BaseViewHolder{ | ||
|
||
/** | ||
* 保存view,以id-view的形式 | ||
*/ | ||
Map<Integer,View> mapView; | ||
|
||
/** | ||
* 根视图 | ||
*/ | ||
View rootView; | ||
|
||
public BaseViewHolder(View rootView){ | ||
this.rootView = rootView; | ||
mapView = new HashMap<Integer,View>(); | ||
} | ||
|
||
/** | ||
* 通过id查找控件 | ||
* @param id | ||
* @return | ||
*/ | ||
public View getView(Integer id){ | ||
View view = mapView.get(id); | ||
if(view==null){ | ||
view = rootView.findViewById(id); | ||
mapView.put(id,view); | ||
} | ||
|
||
return view; | ||
} | ||
|
||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...emwidgetdemo/src/main/java/com/mahao/alex/systemwidgetdemo/listView/ListViewActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.mahao.alex.systemwidgetdemo.listView; | ||
|
||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.widget.ListView; | ||
|
||
import com.mahao.alex.systemwidgetdemo.R; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
* | ||
* ListView使用总结 | ||
* Created by Alex_MaHao on 2016/5/17. | ||
*/ | ||
public class ListViewActivity extends AppCompatActivity { | ||
|
||
private ListView mLv; | ||
|
||
private List<String> datas = new ArrayList<>(); | ||
|
||
private SimpleBaseAdapter mAdapter; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
setContentView(R.layout.activity_listview); | ||
|
||
mLv = ((ListView) findViewById(R.id.listview_lv)); | ||
|
||
initDatas(); | ||
|
||
mAdapter = new SimpleBaseAdapter(datas); | ||
|
||
mLv.setAdapter(mAdapter); | ||
|
||
} | ||
|
||
private void initDatas() { | ||
for(int i = 0;i<20;i++){ | ||
datas.add("添加数据~~"+i); | ||
} | ||
} | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...mwidgetdemo/src/main/java/com/mahao/alex/systemwidgetdemo/listView/SimpleBaseAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mahao.alex.systemwidgetdemo.listView; | ||
|
||
import android.widget.TextView; | ||
|
||
import com.mahao.alex.systemwidgetdemo.R; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 最基础的adapter | ||
* Created by Alex_MaHao on 2016/5/17. | ||
*/ | ||
|
||
public class SimpleBaseAdapter extends BaseAppAdapter<String> { | ||
|
||
public SimpleBaseAdapter(List<String> datas) { | ||
super(datas); | ||
} | ||
|
||
@Override | ||
protected int getItemLayoutId() { | ||
return R.layout.item_listview_sample; | ||
} | ||
|
||
@Override | ||
protected void bindData(BaseViewHolder vHolder, String data) { | ||
|
||
((TextView) vHolder.getView(R.id.listview_sample_tv)).setText(data); | ||
|
||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
systemwidgetdemo/src/main/res/layout/activity_listview.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<ListView | ||
android:id="@+id/listview_lv" | ||
android:divider="#f00" | ||
android:scrollbars="none" | ||
android:listSelector="#0000" | ||
android:dividerHeight="3dp" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"></ListView> | ||
|
||
|
||
</LinearLayout> |
13 changes: 13 additions & 0 deletions
13
systemwidgetdemo/src/main/res/layout/item_listview_sample.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="horizontal" android:layout_width="wrap_content" | ||
android:layout_height="wrap_content"> | ||
<TextView | ||
android:id="@+id/listview_sample_tv" | ||
android:layout_margin="10dp" | ||
android:textColor="#000" | ||
android:textSize="16sp" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
|
||
</LinearLayout> |